Documentation ¶
Overview ¶
Iters package provides a set of functions that operate on iterators.
Index ¶
- func All[V any](seq Seq[V], predicate ...func(V) bool) bool
- func Any[V any](seq Seq[V], predicate ...func(V) bool) bool
- func ForEach[V any](seq IIterable[V], do func(V))
- func ForEach2[K, V any](seq IIterable2[K, V], do func(K, V))
- func Max[V Ord](seq Seq[V]) V
- func Min[V Ord](seq Seq[V]) V
- func None[V any](seq Seq[V], predicate ...func(V) bool) bool
- func Product[V Integer](seq Seq[V]) V
- func Reduce[V any](seq IIterable[V], reduce func(V, V) V, start ...V) V
- func Reduce2[K, V any](seq IIterable2[K, V], reduce func(V, V) V, start ...V) V
- func Sum[V Ord](seq Seq[V]) V
- type IIterable
- type IIterable2
- type Integer
- type ItMap
- type ItSlice
- type Ord
- type Pair
- type Seq
- func Backwards[V any](seq IIterable[V]) Seq[V]
- func Chain[V any](seqs ...IIterable[V]) Seq[V]
- func Count[I Integer](i I) Seq[I]
- func CountDown[I Integer](i I) Seq[I]
- func Cycle[V any](seq IIterable[V]) Seq[V]
- func Drop[I Integer, V any](n I, seq IIterable[V]) Seq[V]
- func DropWhile[V any](seq IIterable[V], predicate func(V) bool) Seq[V]
- func Filter[V any](seq IIterable[V], filter func(V) bool) Seq[V]
- func Map[V any](seq IIterable[V], mapf func(V) V) Seq[V]
- func OnEmpty[V any](seq IIterable[V], callback func()) Seq[V]
- func Range[S Signed](val S, vals ...S) Seq[S]
- func Repeat[I Integer, V any](val V, n I) Seq[V]
- func Rotate[I Integer, V any](n I, seq IIterable[V], len ...I) Seq[V]
- func Surf[V any](seq iter.Seq[V]) Seq[V]
- func Take[I Integer, V any](n I, seq IIterable[V]) Seq[V]
- func TakeBetween[I Integer, V any](start, end I, seq IIterable[V]) Seq[V]
- func TakeWhile[V any](seq IIterable[V], predicate func(V) bool) Seq[V]
- func With[V any](seq IIterable[V], process func(V)) Seq[V]
- type Seq2
- func Backwards2[K, V any](seq IIterable2[K, V]) Seq2[K, V]
- func Chain2[K, V any](seqs ...IIterable2[K, V]) Seq2[K, V]
- func Cycle2[K, V any](seq IIterable2[K, V]) Seq2[K, V]
- func Drop2[I Integer, K, V any](n I, seq IIterable2[K, V]) Seq2[K, V]
- func DropWhile2[K, V any](seq IIterable2[K, V], predicate func(K, V) bool) Seq2[K, V]
- func Enumerate[I Integer, V any](start I, seq IIterable[V]) Seq2[I, V]
- func Enumerate2[I Integer, K, V any](start I, seq IIterable2[K, V]) Seq2[I, Pair[K, V]]
- func Filter2[K, V any](seq IIterable2[K, V], filter func(K, V) bool) Seq2[K, V]
- func Map2[K, V any](seq IIterable2[K, V], mapf func(K, V) (K, V)) Seq2[K, V]
- func OnEmpty2[K, V any](seq IIterable2[K, V], callback func()) Seq2[K, V]
- func Repeat2[I Integer, K, V any](key K, val V, n I) Seq2[K, V]
- func Rotate2[I Integer, K, V any](n I, seq IIterable2[K, V], len ...I) Seq2[K, V]
- func Split[K, V any](seq IIterable[Pair[K, V]]) Seq2[K, V]
- func Surf2[K, V any](seq iter.Seq2[K, V]) Seq2[K, V]
- func SwapKV[K, V any](seq IIterable2[K, V]) Seq2[V, K]
- func Take2[I Integer, K, V any](n I, seq IIterable2[K, V]) Seq2[K, V]
- func TakeBetween2[I Integer, K, V any](start, end I, seq IIterable2[K, V]) Seq2[K, V]
- func TakeWhile2[K, V any](seq IIterable2[K, V], predicate func(K, V) bool) Seq2[K, V]
- func With2[K, V any](seq IIterable2[K, V], process func(K, V)) Seq2[K, V]
- func Zip[V1, V2 any](seq1 IIterable[V1], seq2 IIterable[V2]) Seq2[V1, V2]
- func Zip2[K1, V1, K2, V2 any](seq1 IIterable2[K1, V1], seq2 IIterable2[K2, V2]) Seq2[Pair[K1, V1], Pair[K2, V2]]
- type Signed
Examples ¶
- All
- Any
- Backwards
- Backwards2
- Chain
- Chain (Second)
- Chain2
- Count
- CountDown
- Cycle
- Cycle2
- Drop
- Drop (Less)
- Drop2
- Drop2 (Less)
- DropWhile
- DropWhile2
- Enumerate
- Enumerate2
- Filter
- Filter (Inline)
- Filter2
- Filter2 (Inline)
- ForEach
- ForEach2
- ItMap.All
- ItMap.Keys
- ItMap.Len
- ItMap.String
- ItMap.Values
- ItSlice.All
- ItSlice.Len
- ItSlice.String
- ItSlice.Values
- Iterable
- Iterable (Custom)
- Iterable2
- Iterable2 (Custom)
- Map
- Map (Inline)
- Map2
- Map2 (Inline)
- Max
- Min
- None
- OnEmpty
- OnEmpty (True)
- OnEmpty2
- OnEmpty2 (True)
- Pair.String
- Product
- Range
- Range (Reverse)
- Range (StartEnd)
- Range (StartEndStep)
- Reduce
- Reduce (Start)
- Reduce2
- Reduce2 (Start)
- Repeat
- Repeat2
- Rotate
- Rotate (Less)
- Rotate2
- Rotate2 (Less)
- Seq.All
- Seq.Len
- Seq.Sink
- Seq.String
- Seq.Values
- Seq2.All
- Seq2.Keys
- Seq2.Len
- Seq2.Sink
- Seq2.String
- Seq2.Values
- Split
- Sum
- Surf
- Surf2
- SwapKV
- Take
- Take (Less)
- Take2
- Take2 (Less)
- TakeWhile
- TakeWhile2
- With
- With2
- Zip
- Zip2
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if all values in the iterator satisfy the predicate. If no predicate is provided, it defaults to checking if a value is a zero value for its type.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]bool{true, true, true, true, true}) fmt.Println(it.All(slice.Values())) }
Output: true
func Any ¶ added in v0.1.1
Any returns true if any value in the iterator satisfies the predicate. If no predicate is provided, it defaults to checking if a value is a zero value for its type.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]bool{true, false, true, false, true}) fmt.Println(it.Any(slice.Values())) }
Output: true
func ForEach ¶ added in v0.1.1
ForEach applies a function to each value in the iterator. This function is not lazy and will consume the entire iterator. Note that break, continue, and return statements are not supported in the function.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) it.ForEach(slice, func(v int) { fmt.Print(2*v, " ") }) fmt.Println() }
Output: 2 4 6 8 10
func ForEach2 ¶ added in v0.1.1
func ForEach2[K, V any](seq IIterable2[K, V], do func(K, V))
ForEach2 applies a function to each key-value pair in the iterator. This function is not lazy and will consume the entire iterator. Note that break, continue, and return statements are not supported in the function.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Split(it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}})) it.ForEach2(seq2, func(k, v int) { fmt.Printf("%d:%d ", k, v*2) }) fmt.Println() }
Output: 1:4 3:8 5:12
func Max ¶ added in v0.1.1
func Max[V Ord](seq Seq[V]) V
Max returns the maximum value in the iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) fmt.Println(it.Max(slice.Values())) }
Output: 5
func Min ¶ added in v0.1.1
func Min[V Ord](seq Seq[V]) V
Min returns the minimum value in the iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) fmt.Println(it.Min(slice.Values())) }
Output: 1
func None ¶ added in v0.1.1
None returns true if no value in the iterator satisfies the predicate. If no predicate is provided, it defaults to checking if a value is a zero value for its type.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]bool{true, false, true, false, true}) fmt.Println(it.None(slice.Values())) }
Output: false
func Product ¶ added in v0.1.1
func Product[V Integer](seq Seq[V]) V
Product returns the product of all values in the iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) fmt.Println(it.Product(slice.Values())) }
Output: 120
func Reduce ¶ added in v0.1.1
Reduce returns a single value that is the result of applying the reduce function to all values in the iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { sum := func(acc, v int) int { return acc + v } result := it.Reduce(it.Range(5), sum) fmt.Println(result) }
Output: 10
Example (Start) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { sum := func(acc, v int) int { return acc + v } result := it.Reduce(it.Range(5), sum, 10) fmt.Println(result) }
Output: 20
func Reduce2 ¶ added in v0.1.1
func Reduce2[K, V any](seq IIterable2[K, V], reduce func(V, V) V, start ...V) V
Reduce returns a single value that is the result of applying the reduce function to all values in the iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { sum := func(acc, v int) int { return acc + v } seq2 := it.Range(5).All() result := it.Reduce2(seq2, sum) fmt.Println(result) }
Output: 10
Example (Start) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { sum := func(acc, v int) int { return acc + v } seq2 := it.Range(5).All() result := it.Reduce2(seq2, sum, 10) fmt.Println(result) }
Output: 20
Types ¶
type IIterable ¶ added in v0.1.1
IIterable is an interface that defines the methods for a sequence of values or index-value pairs.
type IIterable2 ¶ added in v0.1.1
IIterable2 is an interface that defines the methods of Iterable and adds a method to get the keys.
type Integer ¶ added in v0.1.1
type Integer constraints.Integer // Integer constraint
type ItMap ¶ added in v0.1.1
type ItMap[K comparable, V any] map[K]V // Generic map type
func Iterable2 ¶
func Iterable2[M ~map[K]V, K comparable, V any](m M) ItMap[K, V]
Iterable2 returns a new ItMap. ItMaps can be passed to any function that also accepts Seq2[K, V] iterators, while preserving the underlying map, which means they can still be indexed with keys.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3}) for k, v := range m { fmt.Printf("%s:%d\n", k, v) } }
Output: a:1 b:2 c:3
Example (Custom) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { type StringIntMap map[string]int m := it.Iterable2(StringIntMap{"a": 1, "b": 2, "c": 3}) for k, v := range m { fmt.Printf("%s:%d\n", k, v) } fmt.Println() }
Output: a:1 b:2 c:3
func (ItMap[K, V]) All ¶ added in v0.1.1
func (m ItMap[K, V]) All() Seq2[K, V]
All returns a Seq2[K, V] iterator over the key-value pairs of the map.
Example ¶
package main import ( "fmt" "slices" it "github.com/elordeiro/go/iters" ) func main() { m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3}) all := m.All() keys := slices.Collect(all.Keys().Sink()) // map order is not guaranteed slices.Sort(keys) values := slices.Collect(all.Values().Sink()) // map order is not guaranteed slices.Sort(values) fmt.Println(keys, values) }
Output: [a b c] [1 2 3]
func (ItMap[K, V]) Keys ¶ added in v0.1.1
func (m ItMap[K, V]) Keys() Seq[K]
Keys returns a Seq[K] iterator over the keys of the map.
Example ¶
package main import ( "fmt" "slices" it "github.com/elordeiro/go/iters" ) func main() { m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3}) slice := slices.Collect(m.Keys().Sink()) // map order is not guaranteed slices.Sort(slice) fmt.Println(slice) }
Output: [a b c]
func (ItMap[K, V]) Len ¶ added in v0.1.1
func (m ItMap[K, V]) Len() int
Len returns the length of the underlying map.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3}) fmt.Println(m.Len()) }
Output: 3
func (ItMap[K, V]) String ¶ added in v0.1.1
func (m ItMap[K, V]) String() string
String returns a string representation of a ItMap[K, V] iterator.
Example ¶
package main import ( "fmt" "strings" it "github.com/elordeiro/go/iters" ) func main() { m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3}) mapStr := fmt.Sprint(m) typeInfoIdx := strings.Index(mapStr, "]") + 2 typeInfo := mapStr[:typeInfoIdx] fmt.Println(typeInfo) parts := strings.Split(mapStr[typeInfoIdx:len(mapStr)-1], " ") for _, part := range parts { fmt.Println(part) } fmt.Println("]") }
Output: ItMap[string,int][ a:1 b:2 c:3 ]
func (ItMap[K, V]) Values ¶ added in v0.1.1
func (m ItMap[K, V]) Values() Seq[V]
Values returns a Seq[V] iterator over the values of the map.
Example ¶
package main import ( "fmt" "slices" it "github.com/elordeiro/go/iters" ) func main() { m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3}) slice := slices.Collect(m.Values().Sink()) // map order is not guaranteed slices.Sort(slice) fmt.Println(slice) }
Output: [1 2 3]
type ItSlice ¶ added in v0.1.1
type ItSlice[V any] []V // Generic slice type
func Iterable ¶
func Iterable[S ~[]V, V any](s S) ItSlice[V]
Iterable returns a new ItSlice. ItSlices can be passed to any function that also accepts Seq[V] iterators, while preserving the underlying slice, which means they can still be indexed and sliced.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) fmt.Println(slice) }
Output: ItSlice[int][1 2 3 4 5]
Example (Custom) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { type IntSlice []int slice := it.Iterable(IntSlice{1, 2, 3, 4, 5}) fmt.Println(slice) }
Output: ItSlice[int][1 2 3 4 5]
func (ItSlice[V]) All ¶ added in v0.1.1
func (s ItSlice[V]) All() Seq2[int, V]
All returns a Seq2[int, V] iterator over index-value pairs of the slice.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) fmt.Println(slice.All()) }
Output: Seq2[int,int][0:1 1:2 2:3 3:4 4:5]
func (ItSlice[V]) Len ¶ added in v0.1.1
func (s ItSlice[V]) Len() int
Len returns the length of the underlying slice.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) fmt.Println(slice.Len()) }
Output: 5
func (ItSlice[V]) String ¶ added in v0.1.1
func (s ItSlice[V]) String() string
String returns a string representation of a ItSlice[V] iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) fmt.Println(slice) }
Output: ItSlice[int][1 2 3 4 5]
func (ItSlice[V]) Values ¶ added in v0.1.1
func (s ItSlice[V]) Values() Seq[V]
Values returns a Seq[V] iterator over the values of the slice.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) fmt.Println(slice.Values()) }
Output: Seq[int][1 2 3 4 5]
type Ord ¶ added in v0.1.1
type Ord constraints.Ordered // Ordered constraint
type Pair ¶
type Pair[K, V any] struct { Key K Value V }
Pair is a key-value pair for Seq2 iterators that need to return more than 2 values.
type Seq ¶
func Backwards ¶ added in v0.1.1
Backwards returns a Seq[V] iterator that iterates over the values in reverse order. If seq is an iterator, it takes O(n) time and space to collect all the values.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Backwards(it.Range(5)) { fmt.Print(v, " ") } fmt.Println() }
Output: 4 3 2 1 0
func Chain ¶
Chain returns a Seq[V] iterator that chains the values of multiple input iterators.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Chain(it.Range(2), it.Range(2, 5), it.Range(5, 10, 2)) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 1 2 3 4 5 7 9
Example (Second) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice1 := it.Iterable([]int{0, 1, 2, 3}) slice2 := it.Iterable([]int{4, 5, 6, 7}) for v := range it.Chain(slice1, slice2) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 1 2 3 4 5 6 7
func Count ¶
func Count[I Integer](i I) Seq[I]
Count returns an iterator that counts up from a given number. The iterator is infinite unless it is stopped by the caller.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { max := 5 for v := range it.Count(0) { if v >= max { break } fmt.Print(v, " ") } fmt.Println() }
Output: 0 1 2 3 4
func CountDown ¶ added in v0.1.1
func CountDown[I Integer](i I) Seq[I]
CountDown returns an iterator that counts down from a given number. The iterator is infinite unless it is stopped by the caller.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.CountDown(5) { if v <= 0 { break } fmt.Print(v, " ") } fmt.Println() }
Output: 5 4 3 2 1
func Cycle ¶ added in v0.1.1
Cycle returns a Seq[V] iterator that cycles through the values of the input iterator. The iterator is infinite unless it is stopped by the caller.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { max := 5 i := 0 for v := range it.Cycle(it.Range(3)) { if i >= max { break } fmt.Print(v, " ") i++ } fmt.Println() }
Output: 0 1 2 0 1
func Drop ¶ added in v0.1.1
Drop returns a Seq[V] iterator that skips the first n values of the input iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Drop(3, it.Range(10)) { fmt.Print(v, " ") } fmt.Println() }
Output: 3 4 5 6 7 8 9
Example (Less) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Drop(10, it.Range(3)) { fmt.Print(v, " ") } fmt.Println() }
Output:
func DropWhile ¶ added in v0.1.1
DropWhile returns a Seq[V] iterator that skips values from the input iterator until the predicate is false.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { isLessThan5 := func(v int) bool { return v < 5 } for v := range it.DropWhile(it.Range(10), isLessThan5) { fmt.Print(v, " ") } fmt.Println() }
Output: 5 6 7 8 9
func Filter ¶ added in v0.1.1
Filter returns a Seq[V] iterator over values that satisfy the filter function.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { isEven := func(v int) bool { return v%2 == 0 } for v := range it.Filter(it.Range(5), isEven) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 2 4
Example (Inline) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Filter(it.Range(5), func(v int) bool { return v%2 == 0 }) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 2 4
func Map ¶
Map returns a Seq[V] iterator over values that are transformed by the map function.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { double := func(v int) int { return v * 2 } for v := range it.Map(it.Range(5), double) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 2 4 6 8
Example (Inline) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Map(it.Range(5), func(v int) int { return v * v }) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 1 4 9 16
func OnEmpty ¶ added in v0.1.1
OnEmpty returns a Seq[V] iterator that calls an else function only if the iterator is exhausted. Similar to a for ... else block in Python.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { result := "" i := 31 Else := func() { result = fmt.Sprint(i, " is prime") } for range it.OnEmpty(it.Range(2, i), Else) { if i%2 == 0 { result = fmt.Sprint(i, " is not prime") break } } fmt.Println(result) }
Output: 31 is prime
Example (True) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) Else := func() { slice = []int{100} } for i, v := range it.Enumerate(0, it.OnEmpty(slice, Else)) { if v == 10 { break } slice[i] = v * i } fmt.Println(slice) }
Output: ItSlice[int][100]
func Range ¶
func Range[S Signed](val S, vals ...S) Seq[S]
Range returns an iterator over a range of integers. If only one argument is provided, it is the end of the range. If two arguments are provided, they are the start and end of the range. If three arguments are provided, they are the start, end, and step of the range. If an infinite loop is detected, the function panics.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Range(5) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 1 2 3 4
Example (Reverse) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Range(5, 0) { fmt.Print(v, " ") } fmt.Println() }
Output: 5 4 3 2 1
Example (StartEnd) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Range(2, 5) { fmt.Print(v, " ") } fmt.Println() }
Output: 2 3 4
Example (StartEndStep) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Range(2, 10, 2) { fmt.Print(v, " ") } fmt.Println() }
Output: 2 4 6 8
func Repeat ¶
Repeat returns a Seq[V] iterator that yields the same value n times. If n is negative, the iterator is infinite.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Repeat(3, 5) { fmt.Print(v, " ") } fmt.Println() }
Output: 3 3 3 3 3
func Rotate ¶ added in v0.1.1
Rotate returns a Seq[V] iterator that rotates the values of the input iterator by n steps. A positive n rotates the values to the left, and a negative n rotates the values to the right. If seq is an iterator, it is advised to pass the length of it as the third argument to avoid a call to Len() which is an O(n) operation.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Rotate(2, it.Range(5)) { fmt.Print(v, " ") } fmt.Println() }
Output: 2 3 4 0 1
Example (Less) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Rotate(-2, it.Range(5)) { fmt.Print(v, " ") } fmt.Println() }
Output: 3 4 0 1 2
func Surf ¶
Surf Surfaces a Seq[V] iterator. It takes an iter.Seq[V] iterator and returns a Seq[V] iterator.
Example ¶
package main import ( "fmt" "slices" it "github.com/elordeiro/go/iters" ) func main() { slice := []int{1, 2, 3, 4, 5} iterSeq := slices.Values(slice) seq := it.Surf(iterSeq) fmt.Println(seq) }
Output: Seq[int][1 2 3 4 5]
func Take ¶ added in v0.1.1
Take returns a Seq[V] iterator that yields the first n values of the input iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Take(9, it.Cycle(it.Range(0, 3))) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 1 2 0 1 2 0 1 2
Example (Less) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for v := range it.Take(2, it.Cycle(it.Range(0, 3))) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 1
func TakeBetween ¶ added in v0.1.1
TakeBetween returns a Seq[V] iterator that yields values between start and end. The start and end values are inclusive.
func TakeWhile ¶ added in v0.1.1
TakeWhile returns a Seq[V] iterator that yields values from the input iterator until the predicate is false.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { isLessThan5 := func(v int) bool { return v < 5 } for v := range it.TakeWhile(it.Range(10), isLessThan5) { fmt.Print(v, " ") } fmt.Println() }
Output: 0 1 2 3 4
func With ¶ added in v0.1.1
With returns a Seq[V] iterator that calls a function with each element before yielding it.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) result := 0 process := func(v int) { if v%2 != 0 { result += v } } for range it.With(slice, process) { } fmt.Println(result) }
Output: 9
func (Seq[V]) All ¶
All returns a Seq2[int, V] iterator over index-value pairs of the Seq.
Example ¶
package main import ( "fmt" "slices" it "github.com/elordeiro/go/iters" ) func main() { slice := []int{1, 2, 3, 4, 5} iterSeq2 := slices.All(slice) seq2 := it.Surf2(iterSeq2) fmt.Println(seq2) }
Output: Seq2[int,int][0:1 1:2 2:3 3:4 4:5]
func (Seq[V]) Len ¶ added in v0.1.1
Len returns the length of the Seq.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq := it.Range(5) fmt.Println(seq.Len()) }
Output: 5
func (Seq[V]) Sink ¶
Sink sinks a Seq[V] iterator Returns an iter.Seq[V] iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]int{1, 2, 3, 4, 5}) seq := slice.Values() iterSeq := seq.Sink() for v := range iterSeq { fmt.Print(v, " ") } fmt.Println() }
Output: 1 2 3 4 5
func (Seq[V]) String ¶
String returns a string representation of a Seq[V] iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq := it.Iterable([]int{1, 2, 3, 4, 5}) fmt.Println(seq.Values()) }
Output: Seq[int][1 2 3 4 5]
type Seq2 ¶
func Backwards2 ¶ added in v0.1.1
func Backwards2[K, V any](seq IIterable2[K, V]) Seq2[K, V]
Backwards returns a Seq2[K, V] iterator that iterates over the values in reverse order. If seq is an iterator, it takes O(n) time and space to collect all the values. If seq is ItMap, the function return the Seq2 from All() since map ordering is not guaranteed
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Range(5).All() for k, v := range it.Backwards2(seq2) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 4:4 3:3 2:2 1:1 0:0
func Chain2 ¶
func Chain2[K, V any](seqs ...IIterable2[K, V]) Seq2[K, V]
Chain2 returns a Seq2[K, V] iterator that chains the values of multiple input iterators.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq1 := it.Split(it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}}).Values()) seq2 := it.Split(it.Iterable([]it.Pair[int, int]{{5, 6}, {7, 8}}).Values()) for k, v := range it.Chain2(seq1, seq2) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 1:2 3:4 5:6 7:8
func Cycle2 ¶ added in v0.1.1
func Cycle2[K, V any](seq IIterable2[K, V]) Seq2[K, V]
Cycle returns a Seq2[K, V] iterator that cycles through the values of the input iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { max := 5 i := 0 for k, v := range it.Cycle2(it.Range(3).All()) { if i >= max { break } fmt.Printf("%d:%d ", k, v) i++ } fmt.Println() }
Output: 0:0 1:1 2:2 0:0 1:1
func Drop2 ¶ added in v0.1.1
func Drop2[I Integer, K, V any](n I, seq IIterable2[K, V]) Seq2[K, V]
Drop returns a Seq2[K, V] iterator that skips the first n values of the input iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Range(10).All() for k, v := range it.Drop2(3, seq2) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 3:3 4:4 5:5 6:6 7:7 8:8 9:9
Example (Less) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Range(3).All() for k, v := range it.Drop2(10, seq2) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output:
func DropWhile2 ¶ added in v0.1.1
func DropWhile2[K, V any](seq IIterable2[K, V], predicate func(K, V) bool) Seq2[K, V]
DropWhile returns a Seq2[K, V] iterator that skips values from the input iterator until the predicate is false.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { isLessThan5 := func(k, v int) bool { return v < 5 } seq2 := it.Range(10).All() for k, v := range it.DropWhile2(seq2, isLessThan5) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 5:5 6:6 7:7 8:8 9:9
func Enumerate ¶
Enumerate returns a Seq2[int, V] iterator over index-value pairs in the Iterable. The start argument specifies the starting index.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]string{"a", "b", "c"}) for i, v := range it.Enumerate(1, slice) { fmt.Printf("%d:%s ", i, v) } fmt.Println() }
Output: 1:a 2:b 3:c
func Enumerate2 ¶
func Enumerate2[I Integer, K, V any](start I, seq IIterable2[K, V]) Seq2[I, Pair[K, V]]
Enumerate2 returns a Seq2[int, Pair] iterator over index-pair pairs in the Iterable. A pair is a key-value pair for Seq2 iterators that need to return more than 2 values. The start argument specifies the starting index.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Split(it.Iterable([]it.Pair[string, string]{{"x", "r"}, {"y", "g"}, {"z", "b"}})) for i, v := range it.Enumerate2(1, seq2) { fmt.Printf("%d:%s ", i, v) } fmt.Println() }
Output: 1:x:r 2:y:g 3:z:b
func Filter2 ¶ added in v0.1.1
func Filter2[K, V any](seq IIterable2[K, V], filter func(K, V) bool) Seq2[K, V]
Filter returns a Seq2[K, V] iterator over key-value pairs that satisfy the filter function.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { isEven := func(k, v int) bool { return v%2 == 0 } seq2 := it.Range(5).All() for k, v := range it.Filter2(seq2, isEven) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 0:0 2:2 4:4
Example (Inline) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Range(5).All() for k, v := range it.Filter2(seq2, func(k, v int) bool { return v%2 == 0 }) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 0:0 2:2 4:4
func Map2 ¶ added in v0.1.1
func Map2[K, V any](seq IIterable2[K, V], mapf func(K, V) (K, V)) Seq2[K, V]
Map returns a Seq2[K, V] iterator over key-value pairs that are transformed by the map function.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { double := func(k, v int) (int, int) { return k, v * 2 } seq2 := it.Range(5).All() for k, v := range it.Map2(seq2, double) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 0:0 1:2 2:4 3:6 4:8
Example (Inline) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Range(5).All() for k, v := range it.Map2(seq2, func(k, v int) (int, int) { return k, v * v }) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 0:0 1:1 2:4 3:9 4:16
func OnEmpty2 ¶ added in v0.1.1
func OnEmpty2[K, V any](seq IIterable2[K, V], callback func()) Seq2[K, V]
OnEmpty returns a Seq2[K, V] iterator that calls an else function only if the iterator is not exhausted. Similar to a for ... else block in Python.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}}) seq2 := it.Split(slice) Else := func() { slice = []it.Pair[int, int]{{100, 200}} } for i, p := range it.Enumerate2(0, it.OnEmpty2(seq2, Else)) { if p.Key == 5 { break } slice[i] = it.Pair[int, int]{p.Key, p.Value * i} } fmt.Println(slice) }
Output: ItSlice[iters.Pair[int,int]][1:0 3:4 5:6]
Example (True) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}}) seq2 := it.Split(slice) Else := func() { slice = []it.Pair[int, int]{{100, 200}} } for i, p := range it.Enumerate2(0, it.OnEmpty2(seq2, Else)) { if p.Value == 10 { break } slice[i] = it.Pair[int, int]{p.Key, p.Value * i} } fmt.Println(slice) }
Output: ItSlice[iters.Pair[int,int]][100:200]
func Repeat2 ¶
func Repeat2[I Integer, K, V any](key K, val V, n I) Seq2[K, V]
Reapeat2 returns a Seq2[K, V] iterator that yields the same key-value pair n times. If n is negative, the iterator is infinite.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { for k, v := range it.Repeat2(3, 4, 5) { fmt.Print(k, ":", v, " ") } fmt.Println() }
Output: 3:4 3:4 3:4 3:4 3:4
func Rotate2 ¶ added in v0.1.1
func Rotate2[I Integer, K, V any](n I, seq IIterable2[K, V], len ...I) Seq2[K, V]
Rotate returns a Seq2[K, V] iterator that rotates the values of the input iterator by n steps. A positive n rotates the values to the left, and a negative n rotates the values to the right. If seq is an iterator, it is advised to pass the length of it as the third argument to avoid a call to Len() which is an O(n) operation.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Range(5).All() for k, v := range it.Rotate2(2, seq2) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 2:2 3:3 4:4 0:0 1:1
Example (Less) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Range(5).All() for k, v := range it.Rotate2(-2, seq2) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 3:3 4:4 0:0 1:1 2:2
func Split ¶ added in v0.1.1
Split converts an iterator over pair values to an iterator over key-values.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]it.Pair[int, string]{{1, "a"}, {2, "b"}, {3, "c"}}) seq2 := it.Split(slice) for k, v := range seq2 { fmt.Printf("%d:%s ", k, v) } fmt.Println() }
Output: 1:a 2:b 3:c
func Surf2 ¶
Surf2 Surfaces a Seq2[K, V] iterator. It takes an iter.Seq2[K, V] iterator and returns a Seq2Seq2[K, V] iterator.
Example ¶
package main import ( "fmt" "slices" it "github.com/elordeiro/go/iters" ) func main() { slice := []int{1, 2, 3, 4, 5} iterSeq2 := slices.All(slice) seq2 := it.Surf2(iterSeq2) fmt.Println(seq2) }
Output: Seq2[int,int][0:1 1:2 2:3 3:4 4:5]
func SwapKV ¶ added in v0.1.1
func SwapKV[K, V any](seq IIterable2[K, V]) Seq2[V, K]
SwapKV returns a Seq2[V, K] iterator that swaps the keys and values of the input iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Split(it.Iterable([]it.Pair[string, int]{{"a", 1}, {"b", 2}, {"c", 3}}).Values()) for k, v := range it.SwapKV(seq2) { fmt.Printf("%d:%s ", k, v) } fmt.Println() }
Output: 1:a 2:b 3:c
func Take2 ¶ added in v0.1.1
func Take2[I Integer, K, V any](n I, seq IIterable2[K, V]) Seq2[K, V]
Take2 returns a Seq2[K, V] iterator that yields the first n values of the input iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Cycle2(it.Range(0, 3).All()) for k, v := range it.Take2(9, seq2) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 0:0 1:1 2:2 0:0 1:1 2:2 0:0 1:1 2:2
Example (Less) ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Cycle2(it.Range(0, 3).All()) for k, v := range it.Take2(2, seq2) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 0:0 1:1
func TakeBetween2 ¶ added in v0.1.1
func TakeBetween2[I Integer, K, V any](start, end I, seq IIterable2[K, V]) Seq2[K, V]
TakeBetween returns a Seq2[K, V] iterator that yields values between start and end. The start and end values are inclusive.
func TakeWhile2 ¶ added in v0.1.1
func TakeWhile2[K, V any](seq IIterable2[K, V], predicate func(K, V) bool) Seq2[K, V]
TakeWhile returns a Seq2[K, V] iterator that yields values from the input iterator until the predicate is false.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { isLessThan5 := func(k, v int) bool { return v < 5 } seq2 := it.Range(10).All() for k, v := range it.TakeWhile2(seq2, isLessThan5) { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 0:0 1:1 2:2 3:3 4:4
func With2 ¶ added in v0.1.1
func With2[K, V any](seq IIterable2[K, V], process func(K, V)) Seq2[K, V]
With returns a Seq2[K, V] iterator that calls a function on each iteration before yielding it.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]it.Pair[string, int]{{"alice", 1}, {"bob", 2}, {"charlie", 3}, {"david", 4}, {"eve", 5}}) seq2 := it.Split(slice) result := "" process := func(k string, v int) { if v > 3 { result += k + " " } } for range it.With2(seq2, process) { } fmt.Println(result) }
Output: david eve
func Zip ¶
Zip returns a Seq2[V1, V2] iterator over values from two sequences. The iteration stops when either of the sequences are exhausted. In other words, the length of the resulting sequence is the minimum of the lengths of the input sequences.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq1 := it.Iterable([]int{1, 2, 3}) seq2 := it.Iterable([]string{"a", "b", "c"}) for v1, v2 := range it.Zip(seq1, seq2) { fmt.Printf("%d:%s ", v1, v2) } fmt.Println() }
Output: 1:a 2:b 3:c
func Zip2 ¶
func Zip2[K1, V1, K2, V2 any](seq1 IIterable2[K1, V1], seq2 IIterable2[K2, V2]) Seq2[Pair[K1, V1], Pair[K2, V2]]
Zip2 returns a Seq2[Pair, Pair] iterator over values from two sequences. The iteration stops when either of the sequences are exhausted. In other words, the length of the resulting sequence is the minimum of the lengths of the input sequences.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq1 := it.Split(it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}})) seq2 := it.Split(it.Iterable([]it.Pair[string, string]{{"a", "b"}, {"c", "d"}, {"e", "f"}}).Values()) for v1, v2 := range it.Zip2(seq1, seq2) { fmt.Printf("{%d %d}:{%s %s} ", v1.Key, v1.Value, v2.Key, v2.Value) } fmt.Println() }
Output: {1 2}:{a b} {3 4}:{c d} {5 6}:{e f}
func (Seq2[K, V]) All ¶
func (s Seq2[K, V]) All() Seq2[K, V]
All returns a Seq2[K, V] iterator over the index-value pairs of the Seq2.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]it.Pair[string, int]{{"a", 1}, {"b", 2}, {"c", 3}}) seq2 := it.Split(slice).All() fmt.Println(seq2) }
Output: Seq2[string,int][a:1 b:2 c:3]
func (Seq2[K, V]) Keys ¶
func (s Seq2[K, V]) Keys() Seq[K]
Keys returns a Seq iterator over the keys of the Seq2.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]it.Pair[string, int]{{"a", 1}, {"b", 2}, {"c", 3}}) seq2 := it.Split(slice) seq := seq2.Keys() fmt.Println(seq) }
Output: Seq[string][a b c]
func (Seq2[K, V]) Len ¶ added in v0.1.1
func (s Seq2[K, V]) Len() int
Len returns the length of the Seq2.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq2 := it.Range(5).All() fmt.Println(seq2.Len()) }
Output: 5
func (Seq2[K, V]) Sink ¶ added in v0.1.1
Sink2 sinks a Seq2[K, V] iterator Returns an iter.Seq2[K, V] iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}}) seq2 := it.Split(slice) iterSeq2 := seq2.Sink() for k, v := range iterSeq2 { fmt.Printf("%d:%d ", k, v) } fmt.Println() }
Output: 1:2 3:4 5:6
func (Seq2[K, V]) String ¶
func (s Seq2[K, V]) String() string
String returns a string representation of a Seq2[K, V] iterator.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { seq := it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}}).Values() fmt.Println(it.Split(seq)) }
Output: Seq2[int,int][1:2 3:4 5:6]
func (Seq2[K, V]) Values ¶
func (s Seq2[K, V]) Values() Seq[V]
Values returns a Seq iterator over the values of the Seq2.
Example ¶
package main import ( "fmt" it "github.com/elordeiro/go/iters" ) func main() { slice := it.Iterable([]it.Pair[string, int]{{"a", 1}, {"b", 2}, {"c", 3}}) seq2 := it.Split(slice) seq := seq2.Values() fmt.Println(seq) }
Output: Seq[int][1 2 3]
type Signed ¶ added in v0.1.1
type Signed constraints.Signed // Signed constraint