iterx

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package iterx provides some extra iter functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[V any](seq iter.Seq[V], predicate func(V) bool) bool

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

func Any[V any](seq iter.Seq[V], predicate func(V) bool) bool

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

func Filter[V any](seq iter.Seq[V], predicate func(V) bool) iter.Seq[V]

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

func Filter2[K, V any](seq iter.Seq2[K, V], predicate func(K, V) bool) iter.Seq2[K, V]

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

func Map[T any, R any](seq iter.Seq[T], mapper func(T) R) iter.Seq[R]

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

func Seq[K, V, T any](seq2 iter.Seq2[K, V], mapper func(K, V) T) iter.Seq[T]

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

func Seq2[T, K, V any](seq iter.Seq[T], mapper func(T) (K, V)) iter.Seq2[K, V]

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

func Sum[V any, R Number](seq iter.Seq[V], f func(V) R) R

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

Types

type Integer added in v0.12.0

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Integer is the integer type.

type Number added in v0.12.0

type Number interface {
	Integer | ~float32 | ~float64
}

Number is the integer or float type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL