Documentation ¶
Overview ¶
Package iters provides utility functions for working with iterators and sequences.
Package iters provides utility functions for working with iterators and sequences.
Package iters provides utility functions for working with iterators and sequences.
Index ¶
- func Accumulate[T constraints.Number | string](s iter.Seq[T], initial T) T
- func AccumulateFunc[T, U any](s iter.Seq[T], f func(U, T) U, initial U) U
- func AccumulateKeys[K constraints.Number | string, V any](m iter.Seq2[K, V], initial K) K
- func AccumulateKeysFunc[K, V, U any](m iter.Seq2[K, V], f func(U, K) U, initial U) U
- func AccumulateValues[K any, V constraints.Number | string](m iter.Seq2[K, V], initial V) V
- func AccumulateValuesFunc[K, V, U any](m iter.Seq2[K, V], f func(U, V) U, initial U) U
- func AppendSeq2[S ~[]pair.Pair[K, V], K, V any](s S, m iter.Seq2[K, V]) S
- func Collect2[K, V any](m iter.Seq2[K, V]) []pair.Pair[K, V]
- func Concat[T any](ss ...iter.Seq[T]) iter.Seq[T]
- func Concat2[K, V any](ms ...iter.Seq2[K, V]) iter.Seq2[K, V]
- func Contains[T comparable](s iter.Seq[T], target T) bool
- func ContainsFunc[T any](s iter.Seq[T], f func(T) bool) bool
- func Count[T any](s iter.Seq[T]) int
- func CountFunc[T any](s iter.Seq[T], f func(T) bool) int
- func Distinct[T comparable](s iter.Seq[T]) iter.Seq[T]
- func DistinctFunc[F ~func(T) K, T any, K comparable](s iter.Seq[T], f F) iter.Seq[K]
- func Enumerate[S ~[]E, E any](s S) iter.Seq[E]
- func Filter[T any](s iter.Seq[T], f func(T) bool) iter.Seq[T]
- func Filter2[K, V any](m iter.Seq2[K, V], f func(K, V) bool) iter.Seq2[K, V]
- func GroupBy[K comparable, V any](s iter.Seq[V], f func(V) K) iter.Seq2[K, []V]
- func Infinite() iter.Seq[int]
- func Keys[K any, V any](m iter.Seq2[K, V]) iter.Seq[K]
- func LessThan[T constraints.Real](end T) iter.Seq[T]
- func List[T any](values ...T) iter.Seq[T]
- func Map[T, U any](s iter.Seq[T], f func(T) U) iter.Seq[U]
- func Map2[K, V, U any](m iter.Seq2[K, V], f func(K, V) U) iter.Seq[U]
- func Max[T cmp.Ordered](s iter.Seq[T]) T
- func Max2[K, V cmp.Ordered](s iter.Seq2[K, V]) (K, V)
- func MaxFunc[T any](s iter.Seq[T], f func(T, T) int) T
- func MaxFunc2[K, V any](s iter.Seq2[K, V], f func(pair.Pair[K, V], pair.Pair[K, V]) int) (K, V)
- func MaxKey[K cmp.Ordered, V any](m iter.Seq2[K, V]) K
- func MaxValue[K any, V cmp.Ordered](m iter.Seq2[K, V]) V
- func Min[T cmp.Ordered](s iter.Seq[T]) T
- func Min2[K, V cmp.Ordered](s iter.Seq2[K, V]) (K, V)
- func MinFunc[T any](s iter.Seq[T], f func(T, T) int) T
- func MinFunc2[K, V any](s iter.Seq2[K, V], f func(pair.Pair[K, V], pair.Pair[K, V]) int) (K, V)
- func MinKey[K cmp.Ordered, V any](m iter.Seq2[K, V]) K
- func MinMax[T cmp.Ordered](s iter.Seq[T]) (min, max T)
- func MinMax2[K, V cmp.Ordered](s iter.Seq2[K, V]) (min, max pair.Pair[K, V])
- func MinMaxFunc[T any](s iter.Seq[T], f func(T, T) int) (min, max T)
- func MinMaxFunc2[K, V any](s iter.Seq2[K, V], f func(pair.Pair[K, V], pair.Pair[K, V]) int) (min, max pair.Pair[K, V])
- func MinMaxKey[K cmp.Ordered, V any](m iter.Seq2[K, V]) (min, max K)
- func MinMaxValue[K any, V cmp.Ordered](m iter.Seq2[K, V]) (min, max V)
- func MinValue[K any, V cmp.Ordered](m iter.Seq2[K, V]) V
- func Range[T cmp.Ordered](start, end, step T) iter.Seq[T]
- func Repeat[T any](v T, n int) iter.Seq[T]
- func Sort[T cmp.Ordered](s iter.Seq[T]) iter.Seq[T]
- func Sort2[K, V cmp.Ordered](m iter.Seq2[K, V]) iter.Seq2[K, V]
- func SortFunc[T any](s iter.Seq[T], cmp func(T, T) int) iter.Seq[T]
- func SortFunc2[K, V any](m iter.Seq2[K, V], cmp func(pair.Pair[K, V], pair.Pair[K, V]) int) iter.Seq2[K, V]
- func SortKeys[K cmp.Ordered, V any](m iter.Seq2[K, V]) iter.Seq2[K, V]
- func SortValues[K any, V cmp.Ordered](m iter.Seq2[K, V]) iter.Seq2[K, V]
- func Split[S ~[]T, T any](s S, n int) iter.Seq[[]T]
- func Steps[T constraints.Number](n int, start T, steps ...T) iter.Seq[T]
- func Sum[T constraints.Number | string](s iter.Seq[T]) T
- func SumKeys[K constraints.Number | string, V any](m iter.Seq2[K, V]) K
- func SumValues[K any, V constraints.Number | string](m iter.Seq2[K, V]) V
- func Unique[T comparable](s iter.Seq[T]) iter.Seq[T]
- func UniqueFunc[F ~func(T, T) bool, T comparable](s iter.Seq[T], eq F) iter.Seq[T]
- func Values[K any, V any](m iter.Seq2[K, V]) iter.Seq[V]
- func WithIndex[T any](s iter.Seq[T]) iter.Seq2[int, T]
- func Zip[T any, U any](s1 iter.Seq[T], s2 iter.Seq[U]) iter.Seq2[T, U]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Accumulate ¶
func Accumulate[T constraints.Number | string](s iter.Seq[T], initial T) T
Accumulate returns the result of adding all elements in the sequence s to the initial value.
func AccumulateFunc ¶
AccumulateFunc applies the function f to each element in the sequence s, accumulating the result starting from the initial value.
func AccumulateKeys ¶
AccumulateKeys returns the result of adding all keys in the key-value sequence m to the initial value.
func AccumulateKeysFunc ¶
AccumulateKeysFunc applies the function f to each key in the key-value sequence m, accumulating the result starting from the initial value.
func AccumulateValues ¶
AccumulateValues returns the result of adding all values in the key-value sequence m to the initial value.
func AccumulateValuesFunc ¶
AccumulateValuesFunc applies the function f to each value in the key-value sequence m, accumulating the result starting from the initial value.
func AppendSeq2 ¶
AppendSeq2 appends the key-value pairs from seq to the slice and returns the extended slice.
func Concat ¶
Concat returns an iterator that generates a sequence of elements from all input sequences.
func Concat2 ¶
Concat2 returns an iterator that generates a sequence of key-value pairs from all input sequences.
func Contains ¶
func Contains[T comparable](s iter.Seq[T], target T) bool
Contains returns true if the sequence s contains the target element.
func ContainsFunc ¶
ContainsFunc returns true if the sequence s contains an element for which the function f returns true.
func CountFunc ¶
CountFunc returns the number of elements in the sequence s for which the function f returns true.
func Distinct ¶
func Distinct[T comparable](s iter.Seq[T]) iter.Seq[T]
Distinct returns an iterator that generates a sequence of distinct elements from s.
func DistinctFunc ¶
DistinctFunc returns an iterator that generates a sequence of distinct elements from s.
func Enumerate ¶
Enumerate returns an iterator that generates a sequence of values for each element in the slice. The index of the element is not provided. If you need the index, use slices.All instead.
Example:
for v := range Enumerate([]string{"a", "b", "c"}) { fmt.Println(v) // Output: a \n b \n c }
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { fruits := []string{"apple", "banana", "cherry"} for v := range iters.Enumerate(fruits) { fmt.Println(v) } }
Output: apple banana cherry
func Filter ¶
Filter returns an iterator that generates a sequence of elements from s for which the function f returns true.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { s := iters.List(1, 2, 3, 4, 5, 6) evenNumbers := iters.Filter(s, func(i int) bool { return i%2 == 0 }) for v := range evenNumbers { fmt.Printf("%d ", v) } }
Output: 2 4 6
func Filter2 ¶
Filter2 returns an iterator that generates a sequence of key-value pairs from m for which the function f returns true.
func GroupBy ¶
GroupBy returns an iterator that generates a sequence of key-value pairs, where the key is the result of applying the function f to each element in s, and the value is a slice of all elements in s that produced that key.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { s := iters.List(1, 2, 3, 4, 5, 6) groups := iters.GroupBy(s, func(i int) string { if i%2 == 0 { return "even" } return "odd" }) for k, v := range groups { fmt.Printf("%s: %v\n", k, v) } }
Output: even: [2 4 6] odd: [1 3 5]
func Infinite ¶
Infinite returns an iterator that generates an infinite sequence of integers starting from 0.
func Keys ¶
Keys returns an iterator that generates a sequence of keys from the key-value sequence m.
Example ¶
package main import ( "fmt" "maps" "sort" "github.com/gopherd/core/container/iters" ) func main() { m := map[string]int{"a": 1, "b": 2, "c": 3} result := make([]string, 0, 3) for k := range iters.Keys(maps.All(m)) { result = append(result, k) } // Sort the result for consistent output sort.Strings(result) fmt.Println(result) }
Output: [a b c]
func LessThan ¶
func LessThan[T constraints.Real](end T) iter.Seq[T]
LessThan returns an iterator that generates a sequence of numbers [0, end) with a step size of 1. It panics if end is negative.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { for v := range iters.LessThan(3) { fmt.Print(v, " ") } }
Output: 0 1 2
func Max ¶
Max returns the maximum element in the sequence s. It panics if s is empty.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { max := iters.Max(iters.List(3, 1, 4, 1, 5, 9)) fmt.Println(max) }
Output: 9
func MaxFunc ¶
MaxFunc returns the element in the sequence s for which the function f returns the maximum value.
func MaxFunc2 ¶
MaxFunc2 returns the key-value pair in the key-value sequence m for which the function f returns the maximum value.
func MaxKey ¶
MaxKey returns the maximum key in the key-value sequence m.
Example ¶
package main import ( "fmt" "maps" "github.com/gopherd/core/container/iters" ) func main() { data := map[int]string{3: "three", 1: "one", 4: "four", 5: "five"} maxKey := iters.MaxKey(maps.All(data)) fmt.Println(maxKey) }
Output: 5
func MaxValue ¶
MaxValue returns the maximum value in the key-value sequence m.
Example ¶
package main import ( "fmt" "maps" "github.com/gopherd/core/container/iters" ) func main() { data := map[string]int{"three": 3, "one": 1, "four": 4, "five": 5} maxValue := iters.MaxValue(maps.All(data)) fmt.Println(maxValue) }
Output: 5
func Min ¶
Min returns the minimum element in the sequence s. It panics if s is empty.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { min := iters.Min(iters.List(3, 1, 4, 1, 5, 9)) fmt.Println(min) }
Output: 1
func MinFunc ¶
MinFunc returns the element in the sequence s for which the function f returns the minimum value.
func MinFunc2 ¶
MinFunc2 returns the key-value pair in the key-value sequence m for which the function f returns the minimum value.
func MinKey ¶
MinKey returns the minimum key in the key-value sequence m.
Example ¶
package main import ( "fmt" "maps" "github.com/gopherd/core/container/iters" ) func main() { data := map[int]string{3: "three", 1: "one", 4: "four", 5: "five"} minKey := iters.MinKey(maps.All(data)) fmt.Println(minKey) }
Output: 1
func MinMax ¶
MinMax returns the minimum and maximum elements in the sequence s. It panics if s is empty.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { min, max := iters.MinMax(iters.List(3, 1, 4, 1, 5, 9)) fmt.Printf("Min: %d, Max: %d\n", min, max) }
Output: Min: 1, Max: 9
func MinMax2 ¶
MinMax2 returns the key-value pairs with the minimum and maximum values in the key-value sequence m.
func MinMaxFunc ¶
MinMaxFunc returns the elements in the sequence s for which the function f returns the minimum and maximum values.
func MinMaxFunc2 ¶
func MinMaxFunc2[K, V any](s iter.Seq2[K, V], f func(pair.Pair[K, V], pair.Pair[K, V]) int) (min, max pair.Pair[K, V])
MinMaxFunc2 returns the key-value pairs with the minimum and maximum values in the key-value sequence m.
func MinMaxValue ¶
MinMaxValue returns the key-value pairs with the minimum and maximum values in the key-value sequence m.
func MinValue ¶
MinValue returns the minimum value in the key-value sequence m.
Example ¶
package main import ( "fmt" "maps" "github.com/gopherd/core/container/iters" ) func main() { data := map[string]int{"three": 3, "one": 1, "four": 4, "five": 5} minValue := iters.MinValue(maps.All(data)) fmt.Println(minValue) }
Output: 1
func Range ¶
Range returns an iterator that generates a sequence of numbers from start to end with a step size of step. The sequence includes start but excludes end.
It panics if step is zero, or if start < end and step is negative, or if start > end and step is positive.
Example:
for v := range Range(1, 10, 2) { fmt.Println(v) // Output: 1 3 5 7 9 }
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { for v := range iters.Range(1, 10, 2) { fmt.Print(v, " ") } }
Output: 1 3 5 7 9
func Repeat ¶
Repeat returns an iterator that generates a sequence of n elements, each with the value v. It panics if n is negative.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { for v := range iters.Repeat("Go", 3) { fmt.Print(v, " ") } }
Output: Go Go Go
func Sort2 ¶
Sort2 returns an iterator that generates a sorted sequence of the key-value pairs in m.
func SortFunc ¶
SortFunc returns an iterator that generates a sorted sequence of the elements in s using the comparison function.
func SortFunc2 ¶
func SortFunc2[K, V any](m iter.Seq2[K, V], cmp func(pair.Pair[K, V], pair.Pair[K, V]) int) iter.Seq2[K, V]
SortFunc2 returns an iterator that generates a sorted sequence of the key-value pairs in m using the comparison function.
func SortKeys ¶
SortKeys returns an iterator that generates a sorted sequence of the key-value pairs in m by key.
func SortValues ¶
SortValues returns an iterator that generates a sorted sequence of the key-value pairs in m by value.
func Split ¶
Split returns an iterator that generates a sequence of chunks from s. The sequence is split into n chunks of approximately equal size. It panics if n is less than 1.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} chunks := iters.Split(slice, 3) for chunk := range chunks { fmt.Println(chunk) } }
Output: [1 2 3 4] [5 6 7] [8 9 10]
func Steps ¶
func Steps[T constraints.Number](n int, start T, steps ...T) iter.Seq[T]
Steps returns an iterator that generates a sequence of n numbers. The behavior of the sequence depends on the provided start value and optional steps.
Parameters:
- n: The number of elements to generate in the sequence.
- start: The starting value of the sequence.
- steps: Optional variadic parameter defining the increments for the sequence.
If no steps are provided, it generates a sequence starting from 'start' and incrementing by 1 each time. If steps are provided, it generates a sequence starting from 'start', then repeatedly applying the steps in a cyclic manner to generate subsequent values.
It panics if n is negative.
Examples:
// No steps provided (increment by 1): for v := range Steps(5, 10) { fmt.Print(v, " ") // Output: 10 11 12 13 14 } // Single step provided: for v := range Steps(5, 1, 2) { fmt.Print(v, " ") // Output: 1 3 5 7 9 } // Multiple steps provided: for v := range Steps(6, 1, 2, 3, 4) { fmt.Print(v, " ") // Output: 1 3 6 10 12 15 } // Using negative steps: for v := range Steps(5, 20, -1, -2, -3) { fmt.Print(v, " ") // Output: 20 19 17 14 13 }
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { for v := range iters.Steps(5, 1, 2, 3) { fmt.Print(v, " ") } }
Output: 1 3 6 8 11
func Sum ¶
func Sum[T constraints.Number | string](s iter.Seq[T]) T
Sum returns the sum of all elements in the sequence s.
func Unique ¶
func Unique[T comparable](s iter.Seq[T]) iter.Seq[T]
Unique returns an iterator that generates a sequence of unique elements from s. Adjacent duplicate elements are removed. The sequence must be sorted. If not, use the Sort function first or the Distinct function directly.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { s := iters.List(1, 2, 2, 3, 3, 3, 4) for v := range iters.Unique(s) { fmt.Printf("%d ", v) } }
Output: 1 2 3 4
func UniqueFunc ¶
UniqueFunc returns an iterator that generates a sequence of unique elements from s. Adjacent elements are considered duplicates if the function f returns the same value for them. The sequence must be sorted. If not, use the SortFunc first or the DistinctFunc directly.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { numbers := []int{1, 1, 2, 3, 3, 4, 4, 4, 5} eq := func(a, b int) bool { return a == b } uniqueSeq := iters.UniqueFunc(iters.List(numbers...), eq) for v := range uniqueSeq { fmt.Print(v, " ") } }
Output: 1 2 3 4 5
func Values ¶
Values returns an iterator that generates a sequence of values from the key-value sequence m.
Example ¶
package main import ( "fmt" "maps" "sort" "github.com/gopherd/core/container/iters" ) func main() { m := map[string]int{"a": 1, "b": 2, "c": 3} result := make([]int, 0, 3) for v := range iters.Values(maps.All(m)) { result = append(result, v) } // Sort the result for consistent output sort.Ints(result) fmt.Println(result) }
Output: [1 2 3]
func WithIndex ¶
WithIndex returns an iterator that generates a sequence of index-value pairs from s.
func Zip ¶
Zip returns an iterator that generates pairs of elements from s1 and s2. If one sequence is longer, remaining elements are paired with zero values.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/container/iters" ) func main() { s1 := iters.List(1, 2, 3) s2 := iters.List("a", "b", "c") for v1, v2 := range iters.Zip(s1, s2) { fmt.Printf("%d: %s\n", v1, v2) } }
Output: 1: a 2: b 3: c
Types ¶
This section is empty.