iter

package
v2.0.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[V any](iter Iterator[V]) []V

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

type Iterator[V any] iter.Seq[V]

Iterator is a wrapper around the iter.Seq type that allowed for method chaining of iterators found in this package.

func Chain

func Chain[V any](iterators ...Iterator[V]) Iterator[V]

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

func Count() Iterator[int]

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

func Cycle[V any](delegate Iterator[V]) Iterator[V]

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

func Drop[V any](delegate Iterator[V], count int) Iterator[V]

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

func Exclude[V any](delegate Iterator[V], predicate func(V) bool) Iterator[V]

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

func Exhausted[V any]() Iterator[V]
Example
package main

import (
	"github.com/BooleanCat/go-functional/v2/iter"
)

func main() {
	for _ = range iter.Exhausted[int]() {
	}
}

func Filter

func Filter[V any](delegate Iterator[V], predicate func(V) bool) Iterator[V]

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

func Lift[V any](slice []V) Iterator[V]

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

func LiftChannel[V any](channel <-chan V) Iterator[V]

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

func Map[V, W any](delegate Iterator[V], transform func(V) W) Iterator[W]

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

func Repeat[V any](value V) Iterator[V]

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

func Runes[V ~string | []rune](runes V) Iterator[rune]

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

func Take[V any](delegate Iterator[V], limit int) Iterator[V]

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

func Zip[V, W any](one Iterator[V], two Iterator[W]) Iterator[Pair[V, W]]

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]) Chain

func (iter Iterator[V]) Chain(iterators ...Iterator[V]) Iterator[V]

Chain is a convenience method for chaining Chain after an iterator.

func (Iterator[V]) Collect

func (iter Iterator[V]) Collect() []V

Collect is a convenience method for chaining Collect after an iterator.

func (Iterator[V]) Cycle

func (iter Iterator[V]) Cycle() Iterator[V]

Cycle is a convenience method for chaining Cycle after an iterator.

func (Iterator[V]) Drop

func (iter Iterator[V]) Drop(count int) Iterator[V]

Drop is a convenience method for chaining Drop after an iterator.

func (Iterator[V]) Exclude

func (iter Iterator[V]) Exclude(predicate func(V) bool) Iterator[V]

Exclude is a convenience method for chaining Exclude after an iterator.

func (Iterator[V]) Filter

func (iter Iterator[V]) Filter(predicate func(V) bool) Iterator[V]

Filter is a convenience method for chaining Filter after an iterator.

func (Iterator[V]) Take

func (iter Iterator[V]) Take(limit int) Iterator[V]

Take is a convenience method for chaining Take after an iterator.

func (Iterator[V]) Transform

func (iter Iterator[V]) Transform(transform func(V) V) Iterator[V]

Transform is a convenience method for chaining Map after an iterator.

See Map for a method chaining caveat.

type Pair

type Pair[V, W any] struct {
	One V
	Two W
}

Pairs of values.

func (Pair[V, W]) String

func (p Pair[V, W]) String() string

Directories

Path Synopsis
This package contains functions intended for use with [iter.Filter].
This package contains functions intended for use with [iter.Filter].

Jump to

Keyboard shortcuts

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