Documentation ¶
Overview ¶
Package iter provides functions to work with sequences.
Index ¶
- func Accumulate[T cmp.Ordered](a []T) iter.Seq[T]
- func Append[T any](seqs ...iter.Seq[T]) iter.Seq[T]
- func Append2[T, U any](seqs ...iter.Seq2[T, U]) iter.Seq2[T, U]
- func Chain[T any](seqs ...[]T) iter.Seq[T]
- func ChainMap[T cmp.Ordered, U any](maps ...map[T]U) iter.Seq2[T, U]
- func ChainSeq[T any](seqs ...iter.Seq[T]) iter.Seq[T]
- func Compress[T any](data []T, selectors []bool) iter.Seq[T]
- func DropWhile[T any](pred func(T) bool, a []T) iter.Seq[T]
- func Equal[T comparable](seqA, seqB iter.Seq[T]) bool
- func Filter[T any](pred func(T) bool, a []T) iter.Seq[T]
- func FilterFalse[T any](pred func(T) bool, a []T) iter.Seq[T]
- func First[T, U any](seq iter.Seq2[T, U]) iter.Seq[T]
- func GroupBy[T any, K comparable](key func(T) K, a []T) iter.Seq2[K, iter.Seq[T]]
- func IZip[T, U any](seqA iter.Seq[T], seqB iter.Seq[U]) iter.Seq2[T, U]
- func Map[T, U any](f func(T) U, a []T) iter.Seq[U]
- func Map2[T, U, V any](f func(T, U) V, a []T, b []U) iter.Seq[V]
- func Permutations[T any](a []T) iter.Seq[[]T]
- func PermutationsLen[T any](a []T, length int) iter.Seq[[]T]
- func Second[T, U any](seq iter.Seq2[T, U]) iter.Seq[U]
- func TakeWhile[T any](pred func(T) bool, a []T) iter.Seq[T]
- func Values[T any](seq iter.Seq[T]) []T
- func Values2[T, U any](seq iter.Seq2[T, U]) ([]T, []U)
- func Values2Map[T comparable, U any](seq iter.Seq2[T, U]) map[T]U
- func Zip[T, U any](a []T, b []U) iter.Seq2[T, U]
- func ZipLongest[T, U any](a []T, b []U, fill either[T, U]) iter.Seq2[T, U]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Accumulate ¶
Accumulate returns a sequence of accumulated values. The first element is the same as the first element of the input sequence. The second element is the sum of the first and second elements of the input sequence. So on and so forth.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a := range iter.Accumulate([]int{1, 2, 3, 4, 5}) { fmt.Println(a) } }
Output: 1 3 6 10 15
func Append ¶ added in v0.2.0
Append returns a sequence of elements from the concatenation of the input sequences.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { i1 := iter.Chain([]int{1, 2, 3}) i2 := iter.Chain([]int{4, 5, 6}) for value := range iter.Append(i1, i2) { fmt.Println(value) } }
Output: 1 2 3 4 5 6
func Append2 ¶ added in v0.2.0
Append2 returns a sequence of elements from the concatenation of the input sequences.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { i1 := iter.ChainMap(map[int]string{1: "1", 2: "2", 3: "3"}) i2 := iter.ChainMap(map[int]string{4: "4", 5: "5", 6: "6"}) for key, value := range iter.Append2(i1, i2) { fmt.Printf("%d: %q\n", key, value) } }
Output: 1: "1" 2: "2" 3: "3" 4: "4" 5: "5" 6: "6"
func Chain ¶
Chain returns a sequence of elements from the input sequences. The resulting sequence is the concatenation of the input sequences.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a := range iter.Chain([]int{1, 2, 3}, []int{4, 5, 6}) { fmt.Println(a) } }
Output: 1 2 3 4 5 6
func ChainMap ¶
ChainMap returns a sequence of elements from the input map. The resulting sequence is ordered by the keys of the input map.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { m := map[int]string{ int('b'): "b", int('c'): "c", int('a'): "a", } for key, value := range iter.ChainMap(m) { fmt.Println(key, value) } }
Output: 97 a 98 b 99 c
func ChainSeq ¶
ChainSeq returns a sequence of elements from the input sequences. The resulting sequence is the concatenation of the input sequences.
Example ¶
package main import ( "fmt" "slices" "github.com/tommoulard/iter" ) func main() { for a := range iter.ChainSeq(slices.Values([]int{1, 2, 3}), slices.Values([]int{4, 5, 6})) { fmt.Println(a) } }
Output: 1 2 3 4 5 6
func Compress ¶
Compress returns a sequence of elements from the input sequence. The resulting sequence contains only the elements where the corresponding selector is true.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a := range iter.Compress([]int{1, 2, 3}, []bool{true, false, true}) { fmt.Println(a) } }
Output: 1 3
func DropWhile ¶
DropWhile returns a sequence of elements from the input sequence. The resulting sequence contains only the elements after the predicate is false.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a := range iter.DropWhile(func(x int) bool { return x < 5 }, []int{1, 4, 6, 3, 8}) { fmt.Println(a) } }
Output: 6 3 8
func Equal ¶ added in v0.2.0
func Equal[T comparable](seqA, seqB iter.Seq[T]) bool
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { fmt.Println(iter.Equal(iter.Chain([]int{1, 2, 3}), iter.Chain([]int{1, 2, 3}))) // true fmt.Println(iter.Equal(iter.Chain([]int{1, 2, 3}), iter.Chain([]int{1, 2}))) // true fmt.Println(iter.Equal(iter.Chain([]int{1, 2, 5}), iter.Chain([]int{1, 2, 3}))) // false fmt.Println(iter.Equal(iter.Chain([]int{1, 2, 5}), iter.Chain([]int{1, 2}))) // true }
Output: true true false true
func Filter ¶
Filter returns a sequence of elements from the input sequence. The resulting sequence contains only the elements where the predicate is true.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a := range iter.Filter(func(x int) bool { return x%2 == 0 }, []int{1, 2, 3, 4, 5}) { fmt.Println(a) } }
Output: 2 4
func FilterFalse ¶
FilterFalse returns a sequence of elements from the input sequence. The resulting sequence contains only the elements where the predicate is false.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a := range iter.FilterFalse(func(x int) bool { return x%2 == 0 }, []int{1, 2, 3, 4, 5}) { fmt.Println(a) } }
Output: 1 3 5
func First ¶ added in v0.2.0
First returns a sequence of elements from the input sequence. The resulting sequence contains only the first element of the input sequence.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { i := iter.ChainMap(map[int]string{1: "2", 2: "4", 3: "6"}) for key := range iter.First(i) { fmt.Println(key) } }
Output: 1 2 3
func GroupBy ¶
GroupBy returns a sequence of groups of elements from the input sequence. The resulting sequence contains groups of elements where the key function returns the same value.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { out := make(map[int][]int) for k, v := range iter.GroupBy(func(x int) int { return x % 2 }, []int{1, 2, 3, 4, 5}) { out[k] = iter.Values(v) } fmt.Println(out[0]) fmt.Println(out[1]) }
Output: [2 4] [1 3 5]
func IZip ¶ added in v0.2.0
IZip returns a sequence of pairs of elements from the input sequences. The resulting sequence is as long as the shortest input sequence.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a, b := range iter.IZip(iter.Chain([]int{1, 2, 3}), iter.Chain([]int{4, 5, 6})) { fmt.Println(a, b) } }
Output: 1 4 2 5 3 6
func Map ¶
Map returns a sequence of elements from the input sequence. The resulting sequence contains the elements after applying the function to each element of the input sequence.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a := range iter.Map(func(x int) int { return x * 2 }, []int{1, 2, 3, 4, 5}) { fmt.Println(a) } }
Output: 2 4 6 8 10
func Map2 ¶
Map2 returns a sequence of elements from the input sequences. The resulting sequence contains the elements after applying the function to each pair of elements from the input sequences.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a := range iter.Map2(func(x, y int) int { return x + y }, []int{1, 2, 3}, []int{4, 5, 6}) { fmt.Println(a) } }
Output: 5 7 9
func Permutations ¶ added in v0.2.0
Permutations returns a sequence of permutations of the input sequence. The resulting sequence contains all possible permutations of the input sequence. This function is a helper for `PermutationsLen(a, len(a))`.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for value := range iter.Permutations([]int{1, 2, 3}) { fmt.Println(value) } }
Output: [1 2 3] [2 1 3] [3 1 2] [1 3 2] [2 3 1] [3 2 1]
func PermutationsLen ¶ added in v0.2.0
PermutationsLen returns a sequence of permutations of the input sequence. The resulting sequence contains all possible permutations of the input sequence with the specified length. It uses the Heap's algorithm to generate the permutations.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for value := range iter.PermutationsLen([]int{1, 2, 3}, 2) { fmt.Println(value) } }
Output: [1 2] [2 1] [3 1] [1 3] [2 3] [3 2]
func Second ¶ added in v0.2.0
Second returns a sequence of elements from the input sequence. The resulting sequence contains only the second element of the input sequence.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { i := iter.ChainMap(map[int]string{1: "2", 2: "4", 3: "6"}) for Values := range iter.Second(i) { fmt.Printf("%q\n", Values) } }
Output: "2" "4" "6"
func TakeWhile ¶
TakeWhile returns a sequence of elements from the input sequence. The resulting sequence contains only the elements before the predicate is false.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a := range iter.TakeWhile(func(x int) bool { return x < 5 }, []int{1, 4, 6, 3, 8}) { fmt.Println(a) } }
Output: 1 4
func Values ¶
Values returns a slice of elements from the input sequence.
Example ¶
package main import ( "fmt" "slices" "github.com/tommoulard/iter" ) func main() { intSeq := slices.Values([]int{1, 2, 3}) fmt.Println(iter.Values(intSeq)) }
Output: [1 2 3]
func Values2 ¶
Values2 returns a pair of slices of elements from the input sequence.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { intSeq2 := iter.Zip([]int{1, 2, 3}, []int{4, 5, 6}) fmt.Println(iter.Values2(intSeq2)) }
Output: [1 2 3] [4 5 6]
func Values2Map ¶ added in v0.2.0
func Values2Map[T comparable, U any](seq iter.Seq2[T, U]) map[T]U
Values2Map returns a map of elements from the input sequence.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { intSeq2 := iter.Zip([]int{1, 2, 3}, []int{4, 5, 6}) fmt.Println(iter.Values2Map(intSeq2)) }
Output: map[1:4 2:5 3:6]
func Zip ¶
Zip returns a sequence of pairs of elements from the input sequences. The resulting sequence is as long as the shortest input sequence.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a, b := range iter.Zip([]int{1, 2, 3}, []int{4, 5, 6}) { fmt.Println(a, b) } }
Output: 1 4 2 5 3 6
func ZipLongest ¶
ZipLongest returns a sequence of pairs of elements from the input sequences. The resulting sequence is as long as the longest input sequence. If one sequence is shorter than the other, the missing values are filled with the provided fill value.
Example ¶
package main import ( "fmt" "github.com/tommoulard/iter" ) func main() { for a, b := range iter.ZipLongest([]int{1, 2, 3}, []int{4}, 0) { fmt.Println(a, b) } }
Output: 1 4 2 0 3 0
Types ¶
This section is empty.