Documentation
¶
Index ¶
- func Chain[V any](iterators ...func(func(V) bool)) iter.Seq[V]
- func Chain2[V, W any](iterators ...func(func(V, W) bool)) iter.Seq2[V, W]
- func CollectErr[V any](delegate func(func(V, error) bool)) ([]V, error)
- func Cycle[V any](delegate func(func(V) bool)) iter.Seq[V]
- func Cycle2[V, W any](delegate func(func(V, W) bool)) iter.Seq2[V, W]
- func Drop[V any](delegate func(func(V) bool), count uint) iter.Seq[V]
- func Drop2[V, W any](delegate func(func(V, W) bool), count uint) iter.Seq2[V, W]
- func Enumerate[V any](delegate func(func(V) bool)) iter.Seq2[int, V]
- func Exclude[V any](delegate func(func(V) bool), predicate func(V) bool) iter.Seq[V]
- func Exclude2[V, W any](delegate func(func(V, W) bool), predicate func(V, W) bool) iter.Seq2[V, W]
- func Exhausted[V any]() iter.Seq[V]
- func Exhausted2[V, W any]() iter.Seq2[V, W]
- func Filter[V any](delegate func(func(V) bool), predicate func(V) bool) iter.Seq[V]
- func Filter2[V, W any](delegate func(func(V, W) bool), predicate func(V, W) bool) iter.Seq2[V, W]
- func Find[V any](iterator func(func(V) bool), pred func(V) bool) (V, bool)
- func Find2[V, W any](iterator func(func(V, W) bool), pred func(V, W) bool) (V, W, bool)
- func ForEach[V any](iterator func(func(V) bool), fn func(V))
- func ForEach2[V, W any](iterator func(func(V, W) bool), fn func(V, W))
- func FromChannel[V any](channel <-chan V) iter.Seq[V]
- func Integers[...](start, stop, step V) iter.Seq[V]
- func Left[V, W any](delegate func(func(V, W) bool)) iter.Seq[V]
- func Lines(r io.Reader) iter.Seq2[[]byte, error]
- func LinesString(r io.Reader) iter.Seq2[string, error]
- func Map[V, W any](delegate func(func(V) bool), f func(V) W) iter.Seq[W]
- func Map2[V, W, X, Y any](delegate func(func(V, W) bool), f func(V, W) (X, Y)) iter.Seq2[X, Y]
- func Max[V cmp.Ordered](iterator func(func(V) bool)) (V, bool)
- func Min[V cmp.Ordered](iterator func(func(V) bool)) (V, bool)
- func NaturalNumbers[...]() iter.Seq[V]
- func Once[V any](value V) iter.Seq[V]
- func Once2[V, W any](v V, w W) iter.Seq2[V, W]
- func Reduce[V, R any](iterator func(func(V) bool), fn func(R, V) R, initial R) R
- func Reduce2[V, W, R any](iterator func(func(V, W) bool), fn func(R, V, W) R, initial R) R
- func Repeat[V any](value V) iter.Seq[V]
- func Repeat2[V, W any](value1 V, value2 W) iter.Seq2[V, W]
- func Right[V, W any](delegate func(func(V, W) bool)) iter.Seq[W]
- func Take[V any](delegate func(func(V) bool), limit uint) iter.Seq[V]
- func Take2[V, W any](delegate func(func(V, W) bool), limit uint) iter.Seq2[V, W]
- func ToChannel[V any](seq func(func(V) bool)) <-chan V
- func Zip[V, W any](left func(func(V) bool), right func(func(W) bool)) iter.Seq2[V, W]
Examples ¶
- Chain
- Chain2
- CollectErr
- Cycle
- Cycle2
- Drop
- Drop2
- Enumerate
- Exclude
- Exclude2
- Exhausted
- Exhausted2
- Filter
- Filter2
- Find
- Find (NotFound)
- Find2
- Find2 (NotFound)
- ForEach
- ForEach2
- FromChannel
- Integers
- Left
- Lines
- LinesString
- Map
- Map2
- Max
- Min
- NaturalNumbers
- Once
- Once2
- Reduce
- Reduce2
- Repeat
- Repeat2
- Right
- Take
- Take2
- ToChannel
- Zip
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
Chain yields values from multiple iterators in sequence.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := slices.Collect(it.Chain(slices.Values([]int{1, 2}), slices.Values([]int{3, 4})))
fmt.Println(numbers)
}
Output: [1 2 3 4]
func Chain2 ¶
Chain2 yields values from multiple iterators in sequence.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
pairs := maps.Collect(it.Chain2(maps.All(map[string]int{"a": 1}), maps.All(map[string]int{"b": 2})))
fmt.Println(len(pairs))
}
Output: 2
func CollectErr ¶
CollectErr consumes an iter.Seq2 where the right side yields errors and returns a slice of values and all errors joined together.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
data := strings.NewReader("one\ntwo\nthree\n")
lines, err := it.CollectErr(it.LinesString(data))
fmt.Println(err)
fmt.Println(lines)
}
Output: <nil> [one two three]
func Cycle ¶
Cycle yields values from an iterator repeatedly.
Note: this is an infinite iterator.
Note: memory usage will grow until all values from the underlying iterator are stored in memory.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := slices.Collect(it.Take(it.Cycle(slices.Values([]int{1, 2})), 5))
fmt.Println(numbers)
}
Output: [1 2 1 2 1]
func Cycle2 ¶
Cycle2 yields pairs of values from an iterator repeatedly.
Note: this is an infinite iterator.
Note: memory usage will grow until all values from the underlying iterator are stored in memory.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := maps.Collect(it.Take2(it.Cycle2(maps.All(map[int]string{1: "one"})), 5))
fmt.Println(numbers)
}
Output: map[1:one]
func Drop ¶
Drop yields all values from a delegate iterator except the first `count` values.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for value := range it.Drop(slices.Values([]int{1, 2, 3, 4, 5}), 2) {
fmt.Println(value)
}
}
Output: 3 4 5
func Drop2 ¶
Drop2 yields all pairs of values from a delegate iterator except the first `count` pairs.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := maps.Collect(it.Drop2(maps.All(map[int]string{1: "one", 2: "two", 3: "three"}), 1))
fmt.Println(len(numbers))
}
Output: 2
func Enumerate ¶
Enumerate yields pairs of indices and values from an iterator.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for index, value := range it.Enumerate(slices.Values([]int{1, 2, 3})) {
fmt.Println(index, value)
}
}
Output: 0 1 1 2 2 3
func Exclude ¶
Exclude yields values from an iterator that do not satisfy a predicate.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/filter"
)
func main() {
for number := range it.Exclude(slices.Values([]int{1, 2, 3, 4, 5}), filter.IsEven) {
fmt.Println(number)
}
}
Output: 1 3 5
func Exclude2 ¶
Exclude2 yields values from an iterator that do not satisfy a predicate.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
isOne := func(n int, _ string) bool { return n == 1 }
numbers := map[int]string{1: "one", 3: "three"}
for key, value := range it.Exclude2(maps.All(numbers), isOne) {
fmt.Println(key, value)
}
}
Output: 3 three
func Exhausted ¶
Exhausted is an iterator that yields no values.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
fmt.Println(len(slices.Collect(it.Exhausted[int]())))
}
Output: 0
func Exhausted2 ¶
Exhausted2 is an iterator that yields no values.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
fmt.Println(len(maps.Collect(it.Exhausted2[int, string]())))
}
Output: 0
func Filter ¶
Filter yields values from an iterator that satisfy a predicate.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/filter"
)
func main() {
for number := range it.Filter(slices.Values([]int{1, 2, 3, 4, 5}), filter.IsEven) {
fmt.Println(number)
}
}
Output: 2 4
func Filter2 ¶
Filter2 yields values from an iterator that satisfy a predicate.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
isOne := func(n int, _ string) bool { return n == 1 }
numbers := map[int]string{1: "one", 2: "two", 3: "three"}
for key, value := range it.Filter2(maps.All(numbers), isOne) {
fmt.Println(key, value)
}
}
Output: 1 one
func Find ¶
Find consumes an iterator until a value is found that satisfies a predicate. It returns the value and true if one was found, or the zero value and false if the iterator was exhausted.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
found, ok := it.Find(slices.Values([]int{1, 2, 3}), func(i int) bool {
return i == 2
})
fmt.Println(found, ok)
}
Output: 2 true
Example (NotFound) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
found, ok := it.Find(slices.Values([]int{1, 2, 3}), func(i int) bool {
return i == 4
})
fmt.Println(found, ok)
}
Output: 0 false
func Find2 ¶
Find2 consumes an iterator until a pair of values is found that satisfies a predicate. It returns the pair and true if one was found, or the zero values and false if the iterator was exhausted.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
index, value, ok := it.Find2(it.Enumerate(slices.Values([]int{1, 2, 3})), func(i, v int) bool {
return i == 2
})
fmt.Println(index, value, ok)
}
Output: 2 3 true
Example (NotFound) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
index, value, ok := it.Find2(it.Enumerate(slices.Values([]int{1, 2, 3})), func(i, v int) bool {
return i == 4
})
fmt.Println(index, value, ok)
}
Output: 0 0 false
func ForEach ¶
ForEach consumes an iterator and applies a function to each value yielded.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
it.ForEach(slices.Values([]int{1, 2, 3}), func(number int) {
fmt.Println(number)
})
}
Output: 1 2 3
func ForEach2 ¶
ForEach2 consumes an iterator and applies a function to each pair of values.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
it.ForEach2(it.Enumerate(slices.Values([]int{1, 2, 3})), func(index int, number int) {
fmt.Println(index, number)
})
}
Output: 0 1 1 2 2 3
func FromChannel ¶
FromChannel yields values from a channel.
In order to avoid a deadlock, the channel must be closed before attempting to called `stop` on a pull-style iterator.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
items := make(chan int)
go func() {
defer close(items)
items <- 1
items <- 2
}()
for number := range it.FromChannel(items) {
fmt.Println(number)
}
}
Output: 1 2
func Integers ¶
func Integers[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64](start, stop, step V) iter.Seq[V]
Integers yields all integers in the range [start, stop) with the given step.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for i := range it.Integers[uint](0, 3, 1) {
fmt.Println(i)
}
}
Output: 0 1 2
func Left ¶
Left is a convenience function that unzips an iterator and returns the left iterator, closing the right iterator.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for left := range it.Left(maps.All(map[int]string{1: "one"})) {
fmt.Println(left)
}
}
Output: 1
func Lines ¶
Lines yields lines from an io.Reader.
Note: lines longer than 65536 will cauese an error.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
buffer := strings.NewReader("one\ntwo\nthree\n")
for line, err := range it.Lines(buffer) {
if err != nil {
fmt.Println(err)
break
}
fmt.Println(string(line))
}
}
Output: one two three
func LinesString ¶
LinesString yields lines from an io.Reader as strings.
Note: lines longer than 65536 will cauese an error.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
buffer := strings.NewReader("one\ntwo\nthree\n")
for line, err := range it.LinesString(buffer) {
if err != nil {
fmt.Println(err)
break
}
fmt.Println(line)
}
}
Output: one two three
func Map ¶
Map yields values from an iterator that have had the provided function applied to each value.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
double := func(n int) int { return n * 2 }
for number := range it.Map(slices.Values([]int{1, 2, 3}), double) {
fmt.Println(number)
}
}
Output: 2 4 6
func Map2 ¶
Map2 yields pairs of values from an iterator that have had the provided function applied to each pair.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
doubleBoth := func(n, m int) (int, int) {
return n * 2, m * 2
}
pairs := it.Zip(slices.Values([]int{1, 2, 3}), slices.Values([]int{2, 3, 4}))
for left, right := range it.Map2(pairs, doubleBoth) {
fmt.Println(left, right)
}
}
Output: 2 4 4 6 6 8
func Max ¶
Max consumes an iterator and returns the maximum value yielded and true if there was at least one value, or the zero value and false if the iterator was empty.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
max, ok := it.Max(slices.Values([]int{1, 2, 3}))
fmt.Println(max, ok)
}
Output: 3 true
func Min ¶
Min consumes an iterator and returns the minimum value yielded and true if there was at least one value, or the zero value and false if the iterator was empty.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
min, ok := it.Min(slices.Values([]int{4, 2, 1, 3}))
fmt.Println(min, ok)
}
Output: 1 true
func NaturalNumbers ¶
func NaturalNumbers[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64]() iter.Seq[V]
NaturalNumbers yields all non-negative integers in ascending order.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for i := range it.NaturalNumbers[int]() {
if i >= 3 {
break
}
fmt.Println(i)
}
}
Output: 0 1 2
func Once ¶
Once yields the provided value once.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for number := range it.Once(42) {
fmt.Println(number)
}
}
Output: 42
func Once2 ¶
Once2 yields the provided value pair once.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for key, value := range it.Once2(1, 2) {
fmt.Println(key, value)
}
}
Output: 1 2
func Reduce ¶
Reduce consumes an iterator and applies a function to each value yielded, accumulating a single result.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/op"
)
func main() {
fmt.Println(it.Reduce(slices.Values([]int{1, 2, 3}), op.Add, 0))
}
Output: 6
func Reduce2 ¶
Reduce2 consumes an iterator and applies a function to each pair of values, accumulating a single result.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
fmt.Println(it.Reduce2(it.Enumerate(slices.Values([]int{1, 2, 3})), func(i, a, b int) int {
return i + 1
}, 0))
}
Output: 3
func Repeat ¶
Repeat yields the same value indefinitely.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for number := range it.Take(it.Repeat(42), 2) {
fmt.Println(number)
}
}
Output: 42 42
func Repeat2 ¶
Repeat2 yields the same two values indefinitely.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for v, w := range it.Take2(it.Repeat2(42, "Life"), 2) {
fmt.Println(v, w)
}
}
Output: 42 Life 42 Life
func Right ¶
Right is a convenience function that unzips an iterator and returns the right iterator, closing the left iterator.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for right := range it.Right(maps.All(map[int]string{1: "one"})) {
fmt.Println(right)
}
}
Output: one
func Take ¶
Take yields the first `limit` values from a delegate iterator.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for number := range it.Take(slices.Values([]int{1, 2, 3, 4, 5}), 3) {
fmt.Println(number)
}
}
Output: 1 2 3
func Take2 ¶
Take2 yields the first `limit` pairs of values from a delegate iterator.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := maps.Collect(it.Take2(maps.All(map[int]string{1: "one", 2: "two", 3: "three"}), 2))
fmt.Println(len(numbers))
}
Output: 2
func ToChannel ¶
ToChannel sends yielded values to a channel.
The channel is closed when the iterator is exhausted. Beware of leaked go routines when using this function with an infinite iterator.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
channel := it.ToChannel(slices.Values([]int{1, 2, 3}))
for number := range channel {
fmt.Println(number)
}
}
Output: 1 2 3
func Zip ¶
Zip yields pairs of values from two iterators.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := slices.Values([]int{1, 2, 3})
strings := slices.Values([]string{"one", "two", "three"})
for left, right := range it.Zip(numbers, strings) {
fmt.Println(left, right)
}
}
Output: 1 one 2 two 3 three
Types ¶
This section is empty.