itertools

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2023 License: GPL-3.0 Imports: 2 Imported by: 0

README

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())
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cycle

func Cycle(items any) (*iterator, error)

Cycle creates an infinite iterator from any slice

func Iter

func Iter(items any) (*iterator, error)

Iter creates a new iterator from any slice

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL