Documentation
¶
Index ¶
- func Collect[V any](iter Iterator[V]) []V
- type Iterator
- func Chain[V any](iterators ...Iterator[V]) Iterator[V]
- func Count() Iterator[int]
- func Cycle[V any](delegate Iterator[V]) Iterator[V]
- func Drop[V any](delegate Iterator[V], count int) Iterator[V]
- func Exclude[V any](delegate Iterator[V], predicate func(V) bool) Iterator[V]
- func Exhausted[V any]() Iterator[V]
- func Filter[V any](delegate Iterator[V], predicate func(V) bool) Iterator[V]
- func Lift[V any](slice []V) Iterator[V]
- func LiftChannel[V any](channel <-chan V) Iterator[V]
- func LiftHashMap[K comparable, V any](hashmap map[K]V) Iterator[Pair[K, V]]
- func Map[V, W any](delegate Iterator[V], transform func(V) W) Iterator[W]
- func Repeat[V any](value V) Iterator[V]
- func Runes[V ~string | []rune](runes V) Iterator[rune]
- func Take[V any](delegate Iterator[V], limit int) Iterator[V]
- func Zip[V, W any](one Iterator[V], two Iterator[W]) Iterator[Pair[V, W]]
- func (iter Iterator[V]) Chain(iterators ...Iterator[V]) Iterator[V]
- func (iter Iterator[V]) Collect() []V
- func (iter Iterator[V]) Cycle() Iterator[V]
- func (iter Iterator[V]) Drop(count int) Iterator[V]
- func (iter Iterator[V]) Exclude(predicate func(V) bool) Iterator[V]
- func (iter Iterator[V]) Filter(predicate func(V) bool) Iterator[V]
- func (iter Iterator[V]) Take(limit int) Iterator[V]
- func (iter Iterator[V]) Transform(transform func(V) V) Iterator[V]
- type Pair
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Collect ¶
Collect consumes an iterator and returns a slice of all items yielded.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
fmt.Println(iter.Collect(iter.Lift([]int{1, 2})))
}
Output: [1 2]
Example (Method) ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
fmt.Println(iter.Lift([]int{1, 2}).Collect())
}
Output: [1 2]
Types ¶
type Iterator ¶
Iterator is a wrapper around the iter.Seq type that allowed for method chaining of iterators found in this package.
func Chain ¶
Chain yields the elements of each iterator in turn.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for value := range iter.Chain(iter.Repeat(42).Take(2), iter.Count().Take(2)) {
fmt.Println(value)
}
}
Output: 42 42 0 1
Example (Method) ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for value := range iter.Repeat(42).Take(2).Chain(iter.Count().Take(2)) {
fmt.Println(value)
}
}
Output: 42 42 0 1
func Count ¶
Count yields an infinite sequence of integers, starting from 0.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Count().Take(3) {
fmt.Println(i)
}
}
Output: 0 1 2
func Cycle ¶
Cycle yields all items in the provided slice, repeating the sequence indefinitely.
Since Cycle stores the members from the underlying iterator, it will grow in size as items are yielded until the underlying iterator is exhausted.
In most cases this iterator is infinite, except when the underlying iterator is immediately exhausted in which case this iterator will exhaust.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Cycle(iter.Lift([]int{1, 2})).Take(5) {
fmt.Println(i)
}
}
Output: 1 2 1 2 1
Example (Method) ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Lift([]int{1, 2}).Cycle().Take(5) {
fmt.Println(i)
}
}
Output: 1 2 1 2 1
func Drop ¶
Drop discards the first `count` elements of a delegate iterator before yielding the rest.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Drop(iter.Lift([]int{1, 2, 3}), 1) {
fmt.Println(i)
}
}
Output: 2 3
Example (Method) ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Lift([]int{1, 2, 3}).Drop(1) {
fmt.Println(i)
}
}
Output: 2 3
func Exclude ¶
Exclude drops the values from delegate that satisfy the predicate.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
"github.com/BooleanCat/go-functional/v2/iter/filter"
)
func main() {
fmt.Println(iter.Exclude(iter.Count(), filter.IsEven).Take(3).Collect())
}
Output: [1 3 5]
Example (Method) ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
"github.com/BooleanCat/go-functional/v2/iter/filter"
)
func main() {
fmt.Println(iter.Count().Exclude(filter.IsEven).Take(3).Collect())
}
Output: [1 3 5]
func Exhausted ¶
Example ¶
package main
import (
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for _ = range iter.Exhausted[int]() {
}
}
func Filter ¶
Filter yields the values from delegate that satisfy predicate.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
"github.com/BooleanCat/go-functional/v2/iter/filter"
)
func main() {
fmt.Println(iter.Filter(iter.Count(), filter.IsEven).Take(3).Collect())
}
Output: [0 2 4]
Example (Method) ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
"github.com/BooleanCat/go-functional/v2/iter/filter"
)
func main() {
fmt.Println(iter.Count().Filter(filter.IsEven).Take(3).Collect())
}
Output: [0 2 4]
func Lift ¶
Lift yields all items in the provided slice.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Lift([]int{1, 2}) {
fmt.Println(i)
}
}
Output: 1 2
func LiftChannel ¶
LiftChannel yields all items in the provided channel.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
ch := make(chan int, 2)
go func() {
ch <- 1
ch <- 2
close(ch)
}()
fmt.Println(iter.LiftChannel(ch).Collect())
}
Output: [1 2]
func LiftHashMap ¶
func LiftHashMap[K comparable, V any](hashmap map[K]V) Iterator[Pair[K, V]]
LiftHashMap yields all items in the provided map as key-value pairs.
Example ¶
package main
import (
"fmt"
"sort"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
pairs := iter.LiftHashMap(map[string]int{"one": 1, "two": 2}).Collect()
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].One < pairs[j].One
})
fmt.Println(pairs)
}
Output: [(one, 1) (two, 2)]
func Map ¶
Map applies a transformation to each value from delegate.
Unlike other iterators, Map cannot be chained as a method on iterators because of a limitation of Go's type system; new type parameters cannot be defined on methods. A limited version of this method is available as the [Transform] method on iterators where the argument and returned value for the operation are of the same type.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Map(iter.Lift([]int{1, 2, 3}), func(i int) int { return i * 2 }) {
fmt.Println(i)
}
}
Output: 2 4 6
Example (Method) ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Lift([]int{1, 2, 3}).Transform(func(i int) int { return i * 2 }) {
fmt.Println(i)
}
}
Output: 2 4 6
func Repeat ¶
Repeat yields an infinite sequence of the same value.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for value := range iter.Repeat(42).Take(3) {
fmt.Println(value)
}
}
Output: 42 42 42
func Runes ¶
Runes yields the runes of the provided string or rune slice.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
runes := iter.Runes("Hello, 世界!").Collect()
fmt.Println(string(runes[7:9]))
}
Output: 世界
Example (Slice) ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
runes := iter.Runes([]rune("Hello, 世界!")).Collect()
fmt.Println(string(runes[7:9]))
}
Output: 世界
func Take ¶
Take limits the number of elements yielded by a delegate iterator to a maximum limit.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Take(iter.Lift([]int{1, 2, 3}), 2) {
fmt.Println(i)
}
}
Output: 1 2
Example (Method) ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
for i := range iter.Lift([]int{1, 2, 3}).Take(2) {
fmt.Println(i)
}
}
Output: 1 2
func Zip ¶
Zip yields pairs of elements from the two provided iterators.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/v2/iter"
)
func main() {
zipped := iter.Zip(iter.Count().Take(2), iter.Repeat("Hi")).Collect()
fmt.Println(zipped)
}
Output: [(0, Hi) (1, Hi)]
func (Iterator[V]) Collect ¶
func (iter Iterator[V]) Collect() []V
Collect is a convenience method for chaining Collect after an iterator.
func (Iterator[V]) Exclude ¶
Exclude is a convenience method for chaining Exclude after an iterator.