Documentation
¶
Index ¶
- func Chain[V any](iterators ...iter.Seq[V]) iter.Seq[V]
- func Chain2[V, W any](iterators ...iter.Seq2[V, W]) iter.Seq2[V, W]
- func Count[...]() iter.Seq[V]
- func Cycle[V any](delegate iter.Seq[V]) iter.Seq[V]
- func Cycle2[V, W any](delegate iter.Seq2[V, W]) iter.Seq2[V, W]
- func Drop[V any](delegate iter.Seq[V], count int) iter.Seq[V]
- func Drop2[V, W any](delegate iter.Seq2[V, W], count int) iter.Seq2[V, W]
- func Enumerate[V any](delegate iter.Seq[V]) iter.Seq2[int, V]
- func Exclude[V any](delegate iter.Seq[V], predicate func(V) bool) iter.Seq[V]
- func Exclude2[V, W any](delegate iter.Seq2[V, W], 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 iter.Seq[V], predicate func(V) bool) iter.Seq[V]
- func Filter2[V, W any](delegate iter.Seq2[V, W], predicate func(V, W) bool) iter.Seq2[V, W]
- func ForEach[V any](iter iter.Seq[V], fn func(V))
- func ForEach2[V, W any](iter iter.Seq2[V, W], fn func(V, W))
- func FromChannel[V any](channel <-chan V) iter.Seq[V]
- func Map[V, W any](delegate iter.Seq[V], transform func(V) W) iter.Seq[W]
- func Map2[V, W, X, Y any](delegate iter.Seq2[V, W], transform func(V, W) (X, Y)) iter.Seq2[X, Y]
- func Reduce[V any, R any](iter iter.Seq[V], fn func(R, V) R, initial R) R
- func Reduce2[V, W any, R any](iter iter.Seq2[V, W], 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 Take[V any](delegate iter.Seq[V], limit int) iter.Seq[V]
- func Take2[V, W any](delegate iter.Seq2[V, W], limit int) iter.Seq2[V, W]
- func ToChannel[V any](seq iter.Seq[V]) <-chan V
- func Unzip[V, W any](delegate iter.Seq2[V, W]) (iter.Seq[V], iter.Seq[W])
- func Zip[V, W any](left iter.Seq[V], right iter.Seq[W]) iter.Seq2[V, W]
- type Iterator
- func (iterator Iterator[V]) Chain(iterators ...iter.Seq[V]) Iterator[V]
- func (iterator Iterator[V]) Collect() []V
- func (iterator Iterator[V]) Cycle() Iterator[V]
- func (iterator Iterator[V]) Drop(count int) Iterator[V]
- func (iterator Iterator[V]) Enumerate() Iterator2[int, V]
- func (iterator Iterator[V]) Exclude(predicate func(V) bool) Iterator[V]
- func (iterator Iterator[V]) Filter(predicate func(V) bool) Iterator[V]
- func (iterator Iterator[V]) ForEach(fn func(V))
- func (iterator Iterator[V]) Take(limit int) Iterator[V]
- func (iterator Iterator[V]) ToChannel() <-chan V
- type Iterator2
- func (iterator Iterator2[V, W]) Chain(iterators ...iter.Seq2[V, W]) Iterator2[V, W]
- func (iterator Iterator2[V, W]) Cycle() Iterator2[V, W]
- func (iterator Iterator2[V, W]) Drop(count int) Iterator2[V, W]
- func (iterator Iterator2[V, W]) Exclude(predicate func(V, W) bool) Iterator2[V, W]
- func (iterator Iterator2[V, W]) Filter(predicate func(V, W) bool) Iterator2[V, W]
- func (iterator Iterator2[V, W]) ForEach(fn func(V, W))
- func (iterator Iterator2[V, W]) Take(limit int) Iterator2[V, W]
- func (iterator Iterator2[V, W]) Unzip() (Iterator[V], Iterator[W])
Examples ¶
- Chain
- Chain (Method)
- Chain2
- Chain2 (Method)
- Count
- Cycle
- Cycle (Method)
- Cycle2
- Cycle2 (Method)
- Drop
- Drop (Method)
- Drop2
- Drop2 (Method)
- Enumerate
- Enumerate (Method)
- Exclude
- Exclude (Method)
- Exclude2
- Exclude2 (Method)
- Exhausted
- Exhausted2
- Filter
- Filter (Method)
- Filter2
- Filter2 (Method)
- ForEach
- ForEach (Method)
- ForEach2
- ForEach2 (Method)
- FromChannel
- Map
- Map2
- Reduce
- Reduce2
- Repeat
- Repeat2
- Take
- Take (Method)
- Take2
- Take2 (Method)
- ToChannel
- ToChannel (Method)
- Unzip
- Unzip (Method)
- 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]
Example (Method) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := it.Iterator[int](slices.Values([]int{1, 2})).Chain(slices.Values([]int{3, 4})).Collect()
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
Example (Method) ¶
package main
import (
"fmt"
"iter"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
pairs := it.Iterator2[string, int](maps.All(map[string]int{"a": 1})).Chain(maps.All(map[string]int{"b": 2}))
fmt.Println(len(maps.Collect(iter.Seq2[string, int](pairs))))
}
Output: 2
func Count ¶
func Count[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64]() iter.Seq[V]
Count 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.Count[int]() {
if i >= 3 {
break
}
fmt.Println(i)
}
}
Output: 0 1 2
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]
Example (Method) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := it.Iterator[int](slices.Values([]int{1, 2})).Cycle().Take(5).Collect()
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]
Example (Method) ¶
package main
import (
"fmt"
"iter"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := it.Iterator2[int, string](maps.All(map[int]string{1: "one"})).Cycle().Take(5)
fmt.Println(maps.Collect(iter.Seq2[int, string](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
Example (Method) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for value := range it.Iterator[int](slices.Values([]int{1, 2, 3, 4, 5})).Drop(2) {
fmt.Println(value)
}
}
Output: 3 4 5
func Drop2 ¶
Drop2 yields all pairs of values from a delegate Iterator2 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
Example (Method) ¶
package main
import (
"fmt"
"iter"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := it.Iterator2[int, string](maps.All(map[int]string{1: "one", 2: "two", 3: "three"})).Drop(1)
fmt.Println(len(maps.Collect(iter.Seq2[int, string](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
Example (Method) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for index, value := range it.Iterator[int](slices.Values([]int{1, 2, 3})).Enumerate() {
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
Example (Method) ¶
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.Iterator[int](slices.Values([]int{1, 2, 3, 4, 5})).Exclude(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
Example (Method) ¶
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.Iterator2[int, string](maps.All(numbers)).Exclude(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
Example (Method) ¶
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.Iterator[int](slices.Values([]int{1, 2, 3, 4, 5})).Filter(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
Example (Method) ¶
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.Iterator2[int, string](maps.All(numbers)).Filter(isOne) {
fmt.Println(key, value)
}
}
Output: 1 one
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
Example (Method) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
it.Iterator[int](slices.Values([]int{1, 2, 3})).ForEach(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
Example (Method) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
it.Iterator[int](slices.Values([]int{1, 2, 3})).Enumerate().ForEach(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 Map ¶
Map yields values from an iterator that have been transformed by a function.
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 been transformed by a function.
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 }
for left, right := range it.Map2(it.Zip(slices.Values([]int{1, 2, 3}), slices.Values([]int{2, 3, 4})), doubleBoth) {
fmt.Println(left, right)
}
}
Output: 2 4 4 6 6 8
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"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := slices.Collect(it.Take(it.Repeat(42), 2))
fmt.Println(numbers)
}
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() {
numbers := it.Take2(it.Repeat2(42, "Life"), 2)
for v, w := range numbers {
fmt.Println(v, w)
}
}
Output: 42 Life 42 Life
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
Example (Method) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
for number := range it.Iterator[int](slices.Values([]int{1, 2, 3, 4, 5})).Take(3) {
fmt.Println(number)
}
}
Output: 1 2 3
func Take2 ¶
Take2 yields the first `limit` pairs of values from a delegate Iterator2.
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
Example (Method) ¶
package main
import (
"fmt"
"iter"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
numbers := maps.Collect(iter.Seq2[int, string](it.Iterator2[int, string](maps.All(map[int]string{1: "one", 2: "two", 3: "three"})).Take(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
Example (Method) ¶
package main
import (
"fmt"
"slices"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
channel := it.Iterator[int](slices.Values([]int{1, 2, 3})).ToChannel()
for number := range channel {
fmt.Println(number)
}
}
Output: 1 2 3
func Unzip ¶
Unzip returns two [Iterator]s from a single Iterator2.
Each returned Iterator yields the left and right values from the original Iterator2, respectively.
It is safe to concurrently pull from the returned [Iterator]s.
Both returned [Iterator]s must be stopped, the underlying Iterator2 is stopped when both are stopped. It is safe to stop one of the returned [Iterator]s immediately and continue pulling from the other.
Example ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
keys, values := it.Unzip(maps.All(map[int]string{1: "one", 2: "two"}))
for key := range keys {
fmt.Println(key)
}
for value := range values {
fmt.Println(value)
}
}
Example (Method) ¶
package main
import (
"fmt"
"maps"
"github.com/BooleanCat/go-functional/v2/it"
)
func main() {
keys, values := it.Iterator2[int, string](maps.All(map[int]string{1: "one", 2: "two"})).Unzip()
for key := range keys {
fmt.Println(key)
}
for value := range values {
fmt.Println(value)
}
}
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() {
for left, right := range it.Zip(slices.Values([]int{1, 2, 3}), slices.Values([]string{"one", "two", "three"})) {
fmt.Println(left, right)
}
}
Output: 1 one 2 two 3 three
Types ¶
type Iterator ¶
Iterator is a wrapper around iter.Seq that allows for method chaining of most iterators found in this package.
func (Iterator[V]) Collect ¶
func (iterator Iterator[V]) Collect() []V
Collect is a convenience method for chaining [Collect] on [Iterator]s.
func (Iterator[V]) Enumerate ¶
Enumerate is a convenience method for chaining Enumerate on [Iterator]s.
func (Iterator[V]) ForEach ¶
func (iterator Iterator[V]) ForEach(fn func(V))
ForEach is a convenience method for chaining ForEach on [Iterator]s.
type Iterator2 ¶
Iterator is a wrapper around iter.Seq2 that allows for method chaining of most iterators found in this package.
func (Iterator2[V, W]) Cycle ¶
func (iterator Iterator2[V, W]) Cycle() Iterator2[V, W]
Cycle is a convenience method for chaining Cycle2 on [Iterator2]s.
func (Iterator2[V, W]) Drop ¶
func (iterator Iterator2[V, W]) Drop(count int) Iterator2[V, W]
Drop is a convenience method for chaining Drop on [Iterator2]s.
func (Iterator2[V, W]) Exclude ¶
func (iterator Iterator2[V, W]) Exclude(predicate func(V, W) bool) Iterator2[V, W]
Exclude is a convenience method for chaining Exclude on [Iterator2]s.
func (Iterator2[V, W]) Filter ¶
func (iterator Iterator2[V, W]) Filter(predicate func(V, W) bool) Iterator2[V, W]
Filter is a convenience method for chaining Filter on [Iterator2]s.
func (Iterator2[V, W]) ForEach ¶
func (iterator Iterator2[V, W]) ForEach(fn func(V, W))
ForEach is a convenience method for chaining ForEach on [Iterator2]s.