Skip to content

hackirby/itertools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

itertools GoDoc

Easily iterate through any slice

Install

go get github.com/hackirby/itertools

Example

import (
    "fmt"
    "github.com/hackirby/itertools"
)

func main() {
    // Create a new iterator from any slice
    iterator, _ := itertools.Iter([]any{1, "two", 3, "four", 5})

    // Some infos about the iterator
    fmt.Println("Length:", iterator.Len())
    fmt.Println("Index:", iterator.Index())
    fmt.Println("Current item:", iterator.Current()) // nil, call Next() first

    // use itertools.Cycle() to create an infinite iterator
    fmt.Println("Is Cycle ?", iterator.IsCycle())

    // set the iterator to the 2nd element (0-based)
    iterator.SetIndex(1)
    fmt.Println(iterator.Current()) // "two"

    // reset the iterator
    iterator.Reset()
    fmt.Println(iterator.Current()) // nil, call Next() first

    // iterate over the iterator
    for iterator.HasNext() {
	switch item := iterator.Next().(type) {
	case int:
            fmt.Println("int:", item) // can be used as int in this block
	case string:
            fmt.Println("string:", item) // can be used as string in this block
	}
    }

    // iterate over the iterator in reverse order
    for iterator.HasPrev() {
	fmt.Println(iterator.Prev())
    }
}

About

Iterate through any slice in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages