Documentation
¶
Overview ¶
Package itertools contains iterator functions like Zip.
Index ¶
- func At[T any](index int, seq iter.Seq[T]) (element T, ok bool)
- func Chain[T any](sequences ...iter.Seq[T]) iter.Seq[T]
- func Count(start, step int) iter.Seq[int]
- func Cycle[T any](seq iter.Seq[T]) iter.Seq[T]
- func CycleValues[T any](values ...T) iter.Seq[T]
- func Drop[T any](n int, seq iter.Seq[T]) iter.Seq[T]
- func DropWhile[T any](f Filterer[T], seq iter.Seq[T]) iter.Seq[T]
- func Enumerate[T any](seq iter.Seq[T]) iter.Seq2[int, T]
- func Filter[T any](f Filterer[T], seq iter.Seq[T]) iter.Seq[T]
- func First[T any](seq iter.Seq[T]) (first T, ok bool)
- func Map[T, U any](m Mapper[T, U], seq iter.Seq[T]) iter.Seq[U]
- func PairUp[F, S any](seq iter.Seq2[F, S]) iter.Seq[Pair[F, S]]
- func Take[T any](n int, seq iter.Seq[T]) iter.Seq[T]
- func TakeWhile[T any](f Filterer[T], seq iter.Seq[T]) iter.Seq[T]
- func Zip[F, S any](first iter.Seq[F], second iter.Seq[S]) iter.Seq2[F, S]
- type Filterer
- type Mapper
- type Pair
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func At ¶ added in v0.7.0
At returns the 0-based indexth element of seq. At returns false if seq has index or fewer elements or if index is negative.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/keep94/itertools"
)
func main() {
seq := slices.Values([]int{10, 13, 16})
fmt.Println(itertools.At(0, seq))
fmt.Println(itertools.At(1, seq))
fmt.Println(itertools.At(2, seq))
fmt.Println(itertools.At(3, seq))
}
Output: 10 true 13 true 16 true 0 false
func Chain ¶ added in v0.4.0
Chain returns all the elements in the first sequence followed by all the elements in the second sequence etc.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/keep94/itertools"
)
func main() {
notes := []string{"do", "re", "mi", "fa", "so"}
ordinals := []int{1, 2, 3}
notesIter := slices.Values(notes)
ordinalsIter := itertools.Chain(
slices.Values(ordinals), itertools.CycleValues(0))
for n, o := range itertools.Zip(notesIter, ordinalsIter) {
fmt.Println(n, o)
}
}
Output: do 1 re 2 mi 3 fa 0 so 0
func CycleValues ¶ added in v0.5.0
CycleValues returns values repeating in an infinite cycle.
func DropWhile ¶ added in v0.7.0
DropWhile returns everything but the first elements of seq for which f returns true.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/keep94/itertools"
)
func main() {
seq := slices.Values([]int{1, 2, 3, 4, 5, 1, 2, 3, 4, 5})
f := func(x int) bool { return x < 4 }
for x := range itertools.DropWhile(f, seq) {
fmt.Println(x)
}
}
Output: 4 5 1 2 3 4 5
func Enumerate ¶ added in v0.2.0
Enumerate works like enumerate in python. It is equivalent to Zip(Count(0, 1), seq)
Example ¶
package main
import (
"fmt"
"slices"
"github.com/keep94/itertools"
)
func main() {
notesIter := slices.Values([]string{"do", "re", "mi", "fa", "so"})
for i, n := range itertools.Enumerate(notesIter) {
fmt.Println(i, n)
}
}
Output: 0 do 1 re 2 mi 3 fa 4 so
func Filter ¶
Filter returns an iter.Seq[T] that contains all the T values in seq for which f returns true.
func TakeWhile ¶ added in v0.3.0
TakeWhile returns the first elements of seq for which f returns true.
Example ¶
package main
import (
"fmt"
"github.com/keep94/itertools"
)
func main() {
seq := itertools.CycleValues(1, 2, 3, 4, 5)
f := func(x int) bool { return x < 4 }
for x := range itertools.TakeWhile(f, seq) {
fmt.Println(x)
}
}
Output: 1 2 3
func Zip ¶
Zip works like zip in python.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/keep94/itertools"
)
func main() {
notesIter := slices.Values([]string{"do", "re", "mi", "fa", "so"})
ordinalsIter := slices.Values([]int{1, 2, 3})
for n, o := range itertools.Zip(notesIter, ordinalsIter) {
fmt.Println(n, o)
}
}
Output: do 1 re 2 mi 3