itertools

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 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

This section is empty.

Types

type Iterator added in v1.1.1

type Iterator struct {
	// contains filtered or unexported fields
}

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

func (*Iterator) Current added in v1.1.1

func (it *Iterator) Current() any

Current returns the current item (nil if not set)

func (*Iterator) HasNext added in v1.1.1

func (it *Iterator) HasNext() bool

HasNext returns true if the Iterator has a next item (always true if cycle mode is enabled)

func (*Iterator) HasPrev added in v1.1.1

func (it *Iterator) HasPrev() bool

HasPrev returns true if the Iterator has a previous item (always true if cycle mode is enabled)

func (*Iterator) Index added in v1.1.1

func (it *Iterator) Index() int

Index returns the current index (0-based)

func (*Iterator) IsCycle added in v1.1.1

func (it *Iterator) IsCycle() bool

IsCycle returns true if the Iterator is in cycle mode

func (*Iterator) Len added in v1.1.1

func (it *Iterator) Len() int

Len returns the length of the Iterator

func (*Iterator) Next added in v1.1.1

func (it *Iterator) Next() any

Next returns the next item (nil if not set)

func (*Iterator) Prev added in v1.1.1

func (it *Iterator) Prev() any

Prev returns the previous item (nil if not set)

func (*Iterator) Reset added in v1.1.1

func (it *Iterator) Reset()

Reset resets the Iterator to the beginning (nil item)

func (*Iterator) SetCycle added in v1.1.1

func (it *Iterator) SetCycle(cycle bool)

SetCycle sets the Iterator to cycle mode (true) or not (false)

func (*Iterator) SetIndex added in v1.1.1

func (it *Iterator) SetIndex(index int) error

SetIndex sets the Iterator to the given index (0-based)

Jump to

Keyboard shortcuts

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