Documentation
¶
Index ¶
- func All(iterator func(func(bool) bool)) bool
- func Any(iterator func(func(bool) bool)) bool
- 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 Collect2[V, W any](iterator func(func(V, W) bool)) ([]V, []W)
- func Compact[V comparable](delegate func(func(V) bool)) iter.Seq[V]
- func Contains[V comparable](iterator func(func(V) bool), v V) bool
- 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 Drain[V any](iterator func(func(V) bool))
- func Drain2[V, W any](iterator func(func(V, W) bool))
- 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 DropWhile[V any](delegate func(func(V) bool), predicate func(V) bool) iter.Seq[V]
- func DropWhile2[V, W any](delegate func(func(V, W) bool), predicate func(V, W) bool) 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 ExcludeError[V any](delegate func(func(V) bool), predicate func(V) (bool, error)) iter.Seq2[V, error]
- 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 FilterError[V any](delegate func(func(V) bool), predicate func(V) (bool, error)) iter.Seq2[V, error]
- func FilterUnique[V comparable](iterator func(func(V) bool)) iter.Seq[V]
- 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 Fold[V, R any](iterator func(func(V) bool), fn func(R, V) R, initial R) R
- func Fold2[V, W, R any](iterator func(func(V, W) bool), fn func(R, V, W) R, initial R) R
- 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 Len[V any](iterator func(func(V) bool)) int
- func Len2[V, W any](iterator func(func(V, W) bool)) int
- 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 MapError[V, W any](delegate func(func(V) bool), f func(V) (W, error)) iter.Seq2[W, error]
- 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 MustCollect[V any](iterator func(func(V, error) bool)) []V
- 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 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 TakeWhile[V any](delegate func(func(V) bool), predicate func(V) bool) iter.Seq[V]
- func TakeWhile2[V, W any](delegate func(func(V, W) bool), predicate func(V, W) bool) iter.Seq2[V, W]
- func ToChannel[V any](seq func(func(V) bool)) <-chan V
- func TryCollect[V any](iterator func(func(V, error) bool)) ([]V, error)
- func Zip[V, W any](left func(func(V) bool), right func(func(W) bool)) iter.Seq2[V, W]
Examples ¶
- All
- Any
- Chain
- Chain2
- Collect2
- Compact
- Contains
- Cycle
- Cycle2
- Drain
- Drain2
- Drop
- Drop2
- DropWhile
- DropWhile2
- Enumerate
- Exclude
- Exclude2
- ExcludeError
- Exhausted
- Exhausted2
- Filter
- Filter2
- FilterError
- FilterUnique
- Find
- Find (NotFound)
- Find2
- Find2 (NotFound)
- Fold
- Fold2
- ForEach
- ForEach2
- FromChannel
- Integers
- Left
- Len
- Len2
- Lines
- LinesString
- Map
- Map2
- MapError
- Max
- Min
- MustCollect
- NaturalNumbers
- Once
- Once2
- Repeat
- Repeat2
- Right
- Take
- Take2
- TakeWhile
- TakeWhile2
- ToChannel
- TryCollect
- Zip
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶ added in v2.2.0
All consumes an iter.Seq of `bool`s and returns true if all values are true, or false otherwise. Iteration will terminate early if a `false` is encountered.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { truths := []bool{true, true, true} fmt.Println(it.All(slices.Values(truths))) }
Output: true
func Any ¶ added in v2.2.0
All consumes an iter.Seq of `bool`s and returns true if any of the values are true, or false otherwise. Iteration will terminate early if a `true` is encountered.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { aTrue := []bool{false, true, false} fmt.Println(it.Any(slices.Values(aTrue))) }
Output: true
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 Collect2 ¶
Collect2 consumes an iter.Seq2 and returns two slices of values.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { indicies, values := it.Collect2(slices.All([]int{1, 2, 3})) fmt.Println(values) fmt.Println(indicies) }
Output: [1 2 3] [0 1 2]
func Compact ¶ added in v2.3.0
func Compact[V comparable](delegate func(func(V) bool)) iter.Seq[V]
Compact yields all values from a delegate iterator that are not zero values.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { words := slices.Values([]string{"", "foo", "", "", "bar", ""}) fmt.Println(slices.Collect(it.Compact(words))) }
Output: [foo bar]
func Contains ¶
func Contains[V comparable](iterator func(func(V) bool), v V) bool
Contains consumes an iter.Seq until the provided value is found and returns true. If the value is not found, it returns false when the iterator is exhausted.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { numbers := slices.Values([]int{1, 2, 3}) fmt.Println(it.Contains(numbers, 2)) }
Output: true
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 Drain ¶ added in v2.1.0
Drain consumes an iter.Seq completely, dropping all values.
You may wish to use this to execute side effects without needing to collect values.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { numbers := it.Map(slices.Values([]int{1, 2, 3}), func(n int) int { fmt.Println(n) return n }) it.Drain(numbers) }
Output: 1 2 3
func Drain2 ¶ added in v2.1.0
Drain2 consumes an iter.Seq2 completely, dropping all values.
You may wish to use this to execute side effects without needing to collect values.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { numbers := it.Map2(slices.All([]int{1, 2, 3}), func(i, n int) (int, int) { fmt.Println(n) return i, n }) it.Drain2(numbers) }
Output: 1 2 3
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" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { _, numbers := it.Collect2(it.Drop2(slices.All([]string{"zero", "one", "two"}), 1)) fmt.Println(numbers) }
Output: [one two]
func DropWhile ¶
DropWhile yields all values from a delegate iterator after the predicate returns false.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for value := range it.DropWhile(slices.Values([]int{1, 2, 3, 4, 1}), filter.LessThan(3)) { fmt.Println(value) } }
Output: 3 4 1
func DropWhile2 ¶
func DropWhile2[V, W any](delegate func(func(V, W) bool), predicate func(V, W) bool) iter.Seq2[V, W]
DropWhile2 yields all pairs of values from a delegate iterator after the predicate returns false.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { _, values := it.Collect2(it.DropWhile2(slices.All([]int{1, 2, 3}), func(int, v int) bool { return v < 3 })) fmt.Println(values) }
Output: [3]
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 ExcludeError ¶
func ExcludeError[V any](delegate func(func(V) bool), predicate func(V) (bool, error)) iter.Seq2[V, error]
ExcludeError yields values from an iterator that do not satisfy a predicate where the predicate can return an error.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { isFoo := func(s string) (bool, error) { return s == "foo", nil } values := slices.Values([]string{"foo", "bar", "foo"}) bars, err := it.TryCollect(it.ExcludeError(values, isFoo)) fmt.Println(bars, err) }
Output: [bar] <nil>
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 FilterError ¶
func FilterError[V any](delegate func(func(V) bool), predicate func(V) (bool, error)) iter.Seq2[V, error]
FilterError yields values from an iterator that satisfy a predicate where the predicate can return an error.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { isFoo := func(s string) (bool, error) { return s == "foo", nil } values := slices.Values([]string{"foo", "bar", "foo"}) foos, err := it.TryCollect(it.FilterError(values, isFoo)) fmt.Println(foos, err) }
Output: [foo foo] <nil>
func FilterUnique ¶ added in v2.4.0
func FilterUnique[V comparable](iterator func(func(V) bool)) iter.Seq[V]
FilterUnique yields all the unique values from an iterator.
Note: All unique values seen from an iterator are stored in memory.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { for number := range it.FilterUnique(slices.Values([]int{1, 2, 2, 3, 3, 3, 4})) { fmt.Println(number) } }
Output: 1 2 3 4
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(slices.All([]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(slices.All([]int{1, 2, 3}), func(i, v int) bool { return i == 4 }) fmt.Println(index, value, ok) }
Output: 0 0 false
func Fold ¶
Fold will fold every element into an accumulator by applying a function and passing an initial value.
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.Fold(slices.Values([]int{1, 2, 3}), op.Add, 0)) }
Output: 6
func Fold2 ¶
Fold2 will fold every element into an accumulator by applying a function and passing an initial value.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { fmt.Println(it.Fold2(slices.All([]int{1, 2, 3}), func(i, a, b int) int { return i + 1 }, 0)) }
Output: 3
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(slices.All([]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, 5, 2) { fmt.Println(i) } }
Output: 0 2 4
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 Len ¶
Len consumes an iter.Seq and returns the number of values yielded.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { fmt.Println(it.Len(slices.Values([]int{1, 2, 3}))) }
Output: 3
func Len2 ¶
Len2 consumes an iter.Seq2 and returns the number of pairs of values yielded.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { fmt.Println(it.Len2(slices.All([]int{1, 2, 3}))) }
Output: 3
func Lines ¶
Lines yields lines from an io.Reader.
Note: lines longer than 65536 will cause 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 MapError ¶
MapError yields values from an iterator that have had the provided function applied to each value where the function can return an error.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { double := func(n int) (int, error) { return n * 2, nil } numbers, err := it.TryCollect(it.MapError(slices.Values([]int{1, 2, 3}), double)) if err == nil { fmt.Println(numbers) } }
Output: [2 4 6]
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 MustCollect ¶ added in v2.1.0
MustCollect consumes an iter.Seq2 where the right side yields errors and returns a slice of values. If an error is encountered this function will panic.
Example ¶
package main import ( "fmt" "strings" "github.com/BooleanCat/go-functional/v2/it" ) func main() { buffer := strings.NewReader("one\ntwo") lines := it.MustCollect(it.LinesString(buffer)) fmt.Println(lines) }
Output: [one two]
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 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 TakeWhile ¶
TakeWhile yields values from a delegate iterator until the predicate returns false.
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.TakeWhile(slices.Values([]int{1, 2, 3, 4, 5}), filter.LessThan(4)) { fmt.Println(number) } }
Output: 1 2 3
func TakeWhile2 ¶
func TakeWhile2[V, W any](delegate func(func(V, W) bool), predicate func(V, W) bool) iter.Seq2[V, W]
TakeWhile2 yields pairs of values from a delegate iterator until the predicate returns false.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" ) func main() { _, values := it.Collect2(it.TakeWhile2(slices.All([]int{1, 2, 3}), func(i int, v int) bool { return v < 3 })) fmt.Println(values) }
Output: [1 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 TryCollect ¶
TryCollect consumes an iter.Seq2 where the right side yields errors and returns a slice of values and the first error encountered. Iteration stops at the first error.
Example ¶
package main import ( "fmt" "strings" "github.com/BooleanCat/go-functional/v2/it" ) func main() { text := strings.NewReader("one\ntwo\nthree\n") lines, err := it.TryCollect(it.LinesString(text)) fmt.Println(err) fmt.Println(lines) }
Output: <nil> [one two three]
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.