Documentation
¶
Overview ¶
Package iterx provides some extra iter functions.
Index ¶
- func All[V any](seq iter.Seq[V], predicate func(V) bool) bool
- func Any[V any](seq iter.Seq[V], predicate func(V) bool) bool
- func Filter[V any](seq iter.Seq[V], predicate func(V) bool) iter.Seq[V]
- func Filter2[K, V any](seq iter.Seq2[K, V], predicate func(K, V) bool) iter.Seq2[K, V]
- func Map[T any, R any](seq iter.Seq[T], mapper func(T) R) iter.Seq[R]
- func Seq[K, V, T any](seq2 iter.Seq2[K, V], mapper func(K, V) T) iter.Seq[T]
- func Seq2[T, K, V any](seq iter.Seq[T], mapper func(T) (K, V)) iter.Seq2[K, V]
- func Sum[V any, R Number](seq iter.Seq[V], f func(V) R) R
- type Integer
- type Number
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if all elements in the sequences match the predicate.
Example ¶
{
ints := []int{1, 2, 3}
seq := slices.Values(ints)
ok0 := All(seq, func(v int) bool { return v > 0 })
ok1 := All(seq, func(v int) bool { return v > 1 })
ok2 := All(seq, func(v int) bool { return v > 2 })
ok3 := All(seq, func(v int) bool { return v > 3 })
fmt.Println(ok0)
fmt.Println(ok1)
fmt.Println(ok2)
fmt.Println(ok3)
}
{
intm := map[int]int{1: 1, 2: 2, 3: 3}
seq := maps.Values(intm)
ok0 := All(seq, func(v int) bool { return v > 0 })
ok1 := All(seq, func(v int) bool { return v > 1 })
ok2 := All(seq, func(v int) bool { return v > 2 })
ok3 := All(seq, func(v int) bool { return v > 3 })
fmt.Println(ok0)
fmt.Println(ok1)
fmt.Println(ok2)
fmt.Println(ok3)
}
Output: true false false false true false false false
func Any ¶
Any returns true if any element in the sequences matches the predicate.
Example ¶
// Slice
{
ints := []int{1, 2, 3}
seq := slices.Values(ints)
ok0 := Any(seq, func(v int) bool { return v > 0 })
ok1 := Any(seq, func(v int) bool { return v > 1 })
ok2 := Any(seq, func(v int) bool { return v > 2 })
ok3 := Any(seq, func(v int) bool { return v > 3 })
fmt.Println(ok0)
fmt.Println(ok1)
fmt.Println(ok2)
fmt.Println(ok3)
}
// Map
{
intm := map[int]int{1: 1, 2: 2, 3: 3}
seq := maps.Values(intm)
ok0 := Any(seq, func(v int) bool { return v > 0 })
ok1 := Any(seq, func(v int) bool { return v > 1 })
ok2 := Any(seq, func(v int) bool { return v > 2 })
ok3 := Any(seq, func(v int) bool { return v > 3 })
fmt.Println(ok0)
fmt.Println(ok1)
fmt.Println(ok2)
fmt.Println(ok3)
}
Output: true true true false true true true false
func Filter ¶ added in v0.5.0
Filter returns a new sequence that only contains the elements that match the predicate.
Example ¶
ints := []int64{1, 2, 3, 4}
iter := Filter(slices.Values(ints), func(v int64) bool { return v%2 == 0 })
ints = slices.Collect(iter)
fmt.Println(ints)
var values []int64
iter(func(v int64) bool {
values = append(values, v)
return false
})
fmt.Println(values)
Output: [2 4] [2]
func Filter2 ¶ added in v0.5.0
Filter2 returns a new sequence that only contains the elements that match the predicate.
Example ¶
ints := []int64{1, 2, 3, 4}
iter := Filter2(slices.All(ints), func(_ int, v int64) bool { return v%2 == 0 })
ints = slices.Collect(Seq(iter, func(_ int, v int64) int64 { return v }))
fmt.Println(ints)
var values []int64
iter(func(_ int, v int64) bool {
values = append(values, v)
return false
})
fmt.Println(values)
Output: [2 4] [2]
func Map ¶ added in v0.5.0
Map returns a new sequence that contains the results of applying the mapper function to the elements.
Example ¶
ints := []int64{1, 2, 3}
iter := Map(slices.Values(ints), func(v int64) string { return strconv.FormatInt(v*v, 10) })
strs := slices.Collect(iter)
fmt.Println(strs)
var values []string
iter(func(v string) bool {
values = append(values, v)
return false
})
fmt.Println(values)
Output: [1 4 9] [1]
func Seq ¶ added in v0.5.0
Seq returns a new sequence that converts iter.Seq2 to iter.Seq.
Example ¶
ints := []int64{1, 2, 3}
iters := Seq(slices.All(ints), func(_ int, v int64) string { return strconv.FormatInt(v*v, 10) })
strs := slices.Collect(iters)
intm := map[string]int64{"a": 1, "b": 2, "c": 3}
iterm := Seq(maps.All(intm), func(_ string, v int64) string { return strconv.FormatInt(v*2, 10) })
strm := slices.Collect(iterm)
slices.Sort(strm)
fmt.Println(strs)
fmt.Println(strm)
var values []string
iters(func(v string) bool {
values = append(values, v)
return false
})
fmt.Println(values)
Output: [1 4 9] [2 4 6] [1]
func Seq2 ¶ added in v0.5.0
Seq2 returns a new sequence that converts iter.Seq to iter.Seq2.
Example ¶
ints := []int64{1, 2, 3}
iters := Seq2(slices.Values(ints), func(v int64) (int64, string) { return v, strconv.FormatInt(v*v, 10) })
strs := maps.Collect(iters)
intm := map[string]int64{"a": 1, "b": 2, "c": 3}
iterm := Seq2(maps.Values(intm), func(v int64) (int64, string) { return v, strconv.FormatInt(v*2, 10) })
strm := maps.Collect(iterm)
fmt.Println(strs)
fmt.Println(strm)
var values []string
iters(func(_ int64, v string) bool {
values = append(values, v)
return false
})
fmt.Println(values)
Output: map[1:1 2:4 3:9] map[1:2 2:4 3:6] [1]
func Sum ¶ added in v0.12.0
Sum returns the sum of the elements in the sequence.
Example ¶
ints1 := []int{1, 2, 3, 4}
sum1 := Sum(slices.Values(ints1), func(v int) int { return v })
fmt.Println(sum1)
ints2 := []int64{1, 2, 3, 4}
sum2 := Sum(slices.Values(ints2), func(v int64) int { return int(v) })
fmt.Println(sum2)
Output: 10 10