iter

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2022 License: MIT Imports: 6 Imported by: 5

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

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

Collect consumes an Iterator and returns all remaining items within a slice. It does not protect against infinite Iterators.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	numbers := iter.Collect[int](iter.Take[int](iter.Count(), 3))

	fmt.Println(numbers)
}
Output:

[0 1 2]

func Fold added in v0.3.0

func Fold[T any, U any](iter Iterator[T], initial U, biop func(U, T) U) U

Fold consumes an Iterator and returns the final result of applying the accumulator function to each element. The accumulator function accepts two arguments - an accumulator and an element and returns a new accumulator. The initial value is the accumulator for the first call. Fold does not protect against infinite Iterators.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
)

func main() {
	sum := iter.Fold[int](iter.Take[int](iter.Count(), 4), 0, ops.Add[int])

	fmt.Println(sum)
}
Output:

6

func ToChannel added in v0.6.0

func ToChannel[T any](iter Iterator[T]) chan T

ToChannel consumes an iterator and returns a channel that will receive all values from the provided iterator. The channel is closed once the iterator is exhausted.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	for number := range iter.ToChannel[int](iter.Lift([]int{1, 2, 3})) {
		fmt.Println(number)
	}

}
Output:

1
2
3

Types

type ChainIter added in v0.4.0

type ChainIter[T any] struct {
	// contains filtered or unexported fields
}

ChainIter implements `Chain`. See `Chain`'s documentation.

func Chain added in v0.4.0

func Chain[T any](iterators ...Iterator[T]) *ChainIter[T]

Chain instantiates a `ChainIter` that will yield all items in the provided iterators to exhaustion, from left to right.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	fmt.Println(iter.Collect[int](iter.Chain[int](iter.Lift([]int{1, 2}), iter.Lift([]int{3, 4}), iter.Lift([]int{0, 9}))))
}
Output:

[1 2 3 4 0 9]

func (*ChainIter[T]) Next added in v0.4.0

func (iter *ChainIter[T]) Next() option.Option[T]

Next implements the Iterator interface for `Chain`.

type ChannelIter added in v0.6.0

type ChannelIter[T any] struct {
	// contains filtered or unexported fields
}

ChannelIter implements `FromChannel`. See `FromChannel`'s documentation.

func FromChannel added in v0.6.0

func FromChannel[T any](ch chan T) *ChannelIter[T]

FromChannel instantiates a `ChannelIter` that will yield each value from the provided channel.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	ch := make(chan int, 2)

	go func() {
		ch <- 1
		ch <- 2
		close(ch)
	}()

	fmt.Println(iter.Collect[int](iter.FromChannel[int](ch)))

}
Output:

[1 2]

func (*ChannelIter[T]) Next added in v0.6.0

func (iter *ChannelIter[T]) Next() option.Option[T]

Next implements the Iterator interface for `ChannelIter`.

type CountIter

type CountIter struct {
	// contains filtered or unexported fields
}

CountIter implements `Count`. See `Count`'s documentation.

func Count

func Count() *CountIter

Count instantiates a `CountIter` that will iterate over 0 and the natural numbers. Count is functionally "unlimited" although it does not protect against the integer limit.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	counter := iter.Count()
	fmt.Println(counter.Next())
	fmt.Println(counter.Next())
	fmt.Println(counter.Next())

}
Output:

Some(0)
Some(1)
Some(2)

func (*CountIter) Next

func (c *CountIter) Next() option.Option[int]

Next implements the Iterator interface for `CountIter`.

type DropIter

type DropIter[T any] struct {
	// contains filtered or unexported fields
}

DropIter implements `Drop`. See `Drop`'s documentation.

func Drop

func Drop[T any](iter Iterator[T], count uint) *DropIter[T]

Drop instantiates a `DropIter` that will skip the number of items of its wrapped iterator by the provided count.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	counter := iter.Drop[int](iter.Count(), 2)
	fmt.Println(counter.Next().Unwrap())
}
Output:

2

func (*DropIter[T]) Next

func (iter *DropIter[T]) Next() option.Option[T]

Next implements the Iterator interface for `DropIter`.

type ExhaustedIter

type ExhaustedIter[T any] struct{}

ExhaustedIter implements `Exhausted`. See `Exhausted`'s documentation.

func Exhausted

func Exhausted[T any]() *ExhaustedIter[T]

Exhausted instantiates an `ExhaustedIter` that will immediately be exhausted (`Next` will always return a `None` variant).

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	fmt.Println(iter.Exhausted[int]().Next())
}
Output:

None

func (*ExhaustedIter[T]) Next

func (iter *ExhaustedIter[T]) Next() option.Option[T]

Next implements the Iterator interface for `ExhaustedIter`.

type FilterIter

type FilterIter[T any] struct {
	// contains filtered or unexported fields
}

FilterIter implements `Filter`. See `Filter`'s documentation.

func Exclude

func Exclude[T any](iter Iterator[T], fun func(T) bool) *FilterIter[T]

Exclude instantiates a `FilterIter` that selectively yields only results that cause the provided function to return `false`.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	filtered := iter.Exclude[int](iter.Lift([]int{0, 1, 0, 2}), filters.IsZero[int])
	fmt.Println(filtered.Next())
	fmt.Println(filtered.Next())
	fmt.Println(filtered.Next())

}
Output:

Some(1)
Some(2)
None

func Filter

func Filter[T any](iter Iterator[T], fun func(T) bool) *FilterIter[T]

Filter instantiates a `FilterIter` that selectively yields only results that cause the provided function to return `true`.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	filtered := iter.Filter[int](iter.Lift([]int{0, 1, 0, 2}), filters.IsZero[int])
	fmt.Println(filtered.Next())
	fmt.Println(filtered.Next())
	fmt.Println(filtered.Next())

}
Output:

Some(0)
Some(0)
None

func (*FilterIter[T]) Next

func (iter *FilterIter[T]) Next() option.Option[T]

Next implements the Iterator interface for `Filter`.

type Iterator

type Iterator[T any] interface {
	Next() option.Option[T]
}

Iterator declares that each Iterator must implement a Next method. Successive calls to the next method shall return the next item in the Iterator, wrapped in an `Option.Some` variant.

Exhausted Iterators shall return a `None` variant of `Option` on every subsequent call.

type LiftIter

type LiftIter[T any] struct {
	// contains filtered or unexported fields
}

LiftIter implements `Lift`. See `Lift`'s documentation.

func Lift

func Lift[T any](items []T) *LiftIter[T]

Lift instantiates a `LiftIter` that will yield all items in the provided slice.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	positives := iter.Filter[int](iter.Lift([]int{-1, 4, 6, 4, -5}), filters.GreaterThan(-1))
	fmt.Println(iter.Collect[int](positives))
}
Output:

[4 6 4]

func (*LiftIter[T]) Next

func (iter *LiftIter[T]) Next() option.Option[T]

Next implements the Iterator interface for `Lift`.

type LinesIter

type LinesIter struct {
	// contains filtered or unexported fields
}

LinesIter implements `Lines`. See `Lines`' documentation.

func Lines

func Lines(r io.Reader) *LinesIter

Lines instantiates a `LinesIter` that will yield each line from the provided `io.Reader`.

Be aware that since `Read` operations can fail, the result time of each item is `result.Result[[]byte]`. Errors will need to be handled as a failure is wrapped in an Ok variant. Multiple calls to `Next()` may simply repeat the same error and cause an infinite loop when collected.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
	"github.com/BooleanCat/go-functional/result"
)

func main() {
	lines := iter.Lines(bytes.NewBufferString("hello\nthere"))
	unwrapped := iter.Map[result.Result[[]byte]](lines, ops.UnwrapResult[[]byte])

	fmt.Println(iter.Collect[[]byte](unwrapped))
}
Output:

[[104 101 108 108 111] [116 104 101 114 101]]

func (*LinesIter) Next

func (iter *LinesIter) Next() option.Option[result.Result[[]byte]]

Next implements the Iterator interface for `LinesIter`.

type MapIter

type MapIter[T, U any] struct {
	// contains filtered or unexported fields
}

MapIter implements `Map`. See `Map`'s documentation.

func LinesString

func LinesString(r io.Reader) *MapIter[result.Result[[]byte], result.Result[string]]

LinesString instantiates a `LinesIter` with results converted to a string via a MapIter. See `Lines` documentation for more information.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/ops"
	"github.com/BooleanCat/go-functional/result"
)

func main() {
	lines := iter.LinesString(bytes.NewBufferString("hello\nthere"))
	unwrapped := iter.Map[result.Result[string]](lines, ops.UnwrapResult[string])

	fmt.Println(iter.Collect[string](unwrapped))
}
Output:

[hello there]

func Map

func Map[T, U any](iter Iterator[T], f func(T) U) *MapIter[T, U]

Take instantiates a `MapIter` that will apply the provided function to each item yielded by the provided Iterator.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	double := func(a int) int { return a * 2 }
	items := iter.Collect[int](iter.Map[int](iter.Lift([]int{0, 1, 2, 3}), double))

	fmt.Println(items)
}
Output:

[0 2 4 6]

func (*MapIter[T, U]) Next

func (iter *MapIter[T, U]) Next() option.Option[U]

Next implements the Iterator interface for `MapIter`.

type TakeIter

type TakeIter[T any] struct {
	// contains filtered or unexported fields
}

TakeIter implements `Take`. See `Take`'s documentation.

func Take

func Take[T any](iter Iterator[T], limit int) *TakeIter[T]

Take instantiates a `TakeIter` that will limit the number of items of its wrapped iterator to a maximum limit.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	iter := iter.Take[int](iter.Count(), 2)
	fmt.Println(iter.Next())
	fmt.Println(iter.Next())
	fmt.Println(iter.Next())
}
Output:

Some(0)
Some(1)
None

func (*TakeIter[T]) Next

func (iter *TakeIter[T]) Next() option.Option[T]

Next implements the Iterator interface for `TakeIter`.

type Tuple

type Tuple[T, U any] struct {
	One T
	Two U
}

type ZipIter

type ZipIter[T, U any] struct {
	// contains filtered or unexported fields
}

ZipIter implements `Zip`. See `Zip`'s documentation.

func Zip

func Zip[T, U any](iter1 Iterator[T], iter2 Iterator[U]) *ZipIter[T, U]

Zip instantiates a `Zip` yield `Tuples` containing the result of a call to each provided Iterator's `Next`. The Iterator is exhausted when one of the provided Iterators is exhausted.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
)

func main() {
	isEven := func(a int) bool { return a%2 == 0 }
	evens := iter.Filter[int](iter.Count(), isEven)
	odds := iter.Exclude[int](iter.Count(), isEven)

	zipped := iter.Collect[iter.Tuple[int, int]](
		iter.Take[iter.Tuple[int, int]](iter.Zip[int, int](evens, odds), 3),
	)
	fmt.Println(zipped)
}
Output:

[{0 1} {2 3} {4 5}]

func (*ZipIter[T, U]) Next

func (iter *ZipIter[T, U]) Next() option.Option[Tuple[T, U]]

Next implements the Iterator interface for `ZipIter`.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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