iter

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 8 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 items within a slice. It does not protect against infinite Iterators.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.Collect[int](iter.Count().Take(3)))
}
Output:

[0 1 2]
Example (Method)
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.Count().Take(3).Collect())
}
Output:

[0 1 2]

func CollectResults added in v0.16.0

func CollectResults[T any](iter Iterator[result.Result[T]]) result.Result[[]T]

CollectResults consumes an Iterator with a yield type of result.Result. It returns all items as a slice. It does not protect against infinite iterators.

When a result.Err is encountered, collection stops and the result.Err is returned immediately.

Example
package main

import (
	"bytes"
	"fmt"

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

func main() {
	words := iter.CollectResults[string](iter.LinesString(bytes.NewBufferString("hello\nfriend")))

	fmt.Println(words)
}
Output:

Ok([hello friend])

func Find added in v0.10.0

func Find[T any](iter Iterator[T], predicate func(v T) bool) option.Option[T]

Find the first occurrence of a value that satisfies the predicate and return that value. If no value satisfies the predicate, return option.None.

Example
package main

import (
	"fmt"

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

func main() {
	values := iter.Lift([]string{"foo", "bar", "baz"})
	bar := iter.Find[string](values, func(v string) bool { return v == "bar" })

	fmt.Println(bar)
}
Output:

Some(bar)
Example (Method)
package main

import (
	"fmt"

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

func main() {
	bar := iter.Lift([]string{"foo", "bar", "baz"}).Find(func(v string) bool {
		return v == "bar"
	})

	fmt.Println(bar)
}
Output:

Some(bar)

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 initial value and returns a new value for the next accumulation. Fold does not protect against infinite Iterators.

Unlike other Iterator consumers (e.g. Collect), it is not possible to call Fold as a method on iterators defined in this package. This is due to a limitation of Go's type system; new type parameters cannot be defined on methods.

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.Count().Take(4), 0, ops.Add[int])

	fmt.Println(sum)
}
Output:

6

func ForEach added in v0.11.0

func ForEach[T any](iter Iterator[T], callback func(T))

ForEach consumes an Iterator and executes callback function on each item.

Example
package main

import (
	"fmt"

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

func main() {
	iter.ForEach[int](iter.Lift([]int{1, 2, 3}), func(number int) {
		fmt.Println(number)
	})

}
Output:

1
2
3
Example (Method)
package main

import (
	"fmt"

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

func main() {
	iter.Lift([]int{1, 2, 3}).ForEach(func(number int) {
		fmt.Println(number)
	})

}
Output:

1
2
3

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
Example (Method)
package main

import (
	"fmt"

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

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

}
Output:

1
2
3

Types

type BaseIter added in v0.14.0

type BaseIter[T any] struct {
	Iterator[T]
}

BaseIter is intended to be embedded in other iterators to expose method chaining.

func (*BaseIter[T]) Chain added in v0.15.0

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

Chain is a convenience method for Chain, providing this iterator as an argument.

func (*BaseIter[T]) Collect added in v0.14.0

func (iter *BaseIter[T]) Collect() []T

Collect is a convenience method for Collect, providing this iterator as an argument.

func (*BaseIter[T]) Drop added in v0.14.0

func (iter *BaseIter[T]) Drop(n uint) *DropIter[T]

Drop is a convenience method for Drop, providing this iterator as an argument.

func (*BaseIter[T]) Enumerate added in v0.16.0

func (iter *BaseIter[T]) Enumerate() *EnumerateIter[T]

Enumerate is a convenience method for Enumerate, providing this iterator as an argument.

func (*BaseIter[T]) Exclude added in v0.16.0

func (iter *BaseIter[T]) Exclude(fun func(T) bool) *FilterIter[T]

Exclude is a convenience method for Exclude, providing this iterator as an argument.

func (*BaseIter[T]) Filter added in v0.15.0

func (iter *BaseIter[T]) Filter(fun func(T) bool) *FilterIter[T]

Filter is a convenience method for Filter, providing this iterator as an argument.

func (*BaseIter[T]) FilterMap added in v1.1.0

func (iter *BaseIter[T]) FilterMap(fun func(T) option.Option[T]) *FilterMapIter[T, T]

FilterMap is a convenience method for FilterMap, providing this iterator as an argument.

func (*BaseIter[T]) Find added in v0.14.0

func (iter *BaseIter[T]) Find(predicate func(T) bool) option.Option[T]

Find is a convenience method for Find, providing this iterator as an argument.

func (*BaseIter[T]) ForEach added in v0.14.0

func (iter *BaseIter[T]) ForEach(callback func(T))

ForEach is a convenience method for ForEach, providing this iterator as an argument.

func (*BaseIter[T]) Take added in v0.14.0

func (iter *BaseIter[T]) Take(n uint) *TakeIter[T]

Take is a convenience method for Take, providing this iterator as an argument.

func (*BaseIter[T]) ToChannel added in v0.15.0

func (iter *BaseIter[T]) ToChannel() chan T

ToChannel is a convenience method for ToChannel, providing this iterator as an argument.

func (*BaseIter[T]) Transform added in v0.16.0

func (iter *BaseIter[T]) Transform(op func(T) T) *MapIter[T, T]

Transform is a convenience method for Transform, providing this iterator as an argument.

type ChainIter added in v0.4.0

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

ChainIter iterator, see Chain.

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 first to last.

Example
package main

import (
	"fmt"

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

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

[1 2 3 4 0 9]
Example (Method)
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.Lift([]int{1, 2}).Chain(iter.Lift([]int{3, 4}), iter.Lift([]int{0, 9})).Collect())
}
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.

func (ChainIter[T]) String added in v1.0.0

func (c ChainIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

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

Iterator<Chain, type=int>

type ChannelIter added in v0.6.0

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

ChannelIter iterator, see FromChannel.

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.FromChannel(ch).Collect())
}
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.

func (ChannelIter[T]) String added in v1.1.0

func (iter ChannelIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

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

	fmt.Println(iter.FromChannel(ch))
}
Output:

Iterator<Channel, type=int>

type CountIter

type CountIter struct {
	BaseIter[int]
	// contains filtered or unexported fields
}

CountIter iterator, see Count.

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.

func (CountIter) String added in v1.0.0

func (c CountIter) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.Count())
}
Output:

Iterator<Count>

type CycleIter added in v0.12.0

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

CycleIter iterator, see Cycle.

func Cycle added in v0.12.0

func Cycle[T any](iter Iterator[T]) *CycleIter[T]

Cycle instantiates a *CycleIter yielding all items from the provided iterator until exhaustion, and then yields them all over again on repeat.

Note that CycleIter stores the members from the underlying iterator so 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 exhausted before the first call to Next() in which case this iterator will always yield None.

Example
package main

import (
	"fmt"

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

func main() {
	numbers := iter.Cycle[int](iter.Lift([]int{1, 2})).Take(5)
	fmt.Println(numbers.Collect())
}
Output:

[1 2 1 2 1]

func (*CycleIter[T]) Next added in v0.12.0

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

Next implements the Iterator interface.

func (CycleIter[T]) String added in v1.1.0

func (iter CycleIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.Cycle[int](iter.Lift([]int{1, 2})))
}
Output:

Iterator<Cycle, type=int>

type DropIter

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

DropIter iterator, see Drop.

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
Example (Method)
package main

import (
	"fmt"

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

func main() {
	counter := iter.Count().Drop(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.

func (DropIter[T]) String added in v1.1.0

func (iter DropIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

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

Iterator<Drop, type=int>

type EnumerateIter added in v0.16.0

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

EnumerateIter iterator, see Enumerate.

func Enumerate added in v0.16.0

func Enumerate[T any](iterator Iterator[T]) *EnumerateIter[T]

Drop instantiates an *EnumerateIter that yield Pairs of the index of iteration and values for a given iterator.

Example
package main

import (
	"fmt"

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

func main() {
	iterator := iter.Enumerate[string](iter.Lift([]string{"Hello", "Friend"}))
	fmt.Println(iterator.Next())
	fmt.Println(iterator.Next())
	fmt.Println(iterator.Next())

}
Output:

Some((0, Hello))
Some((1, Friend))
None
Example (Method)
package main

import (
	"fmt"

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

func main() {
	iterator := iter.Lift([]string{"Hello", "Friend"}).Enumerate()
	fmt.Println(iterator.Next())
	fmt.Println(iterator.Next())
	fmt.Println(iterator.Next())

}
Output:

Some((0, Hello))
Some((1, Friend))
None

func (*EnumerateIter[T]) Next added in v0.16.0

func (iter *EnumerateIter[T]) Next() option.Option[Pair[uint, T]]

Next implements the Iterator interface.

func (EnumerateIter[T]) String added in v1.1.0

func (iter EnumerateIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.Enumerate[string](iter.Exhausted[string]()))
}
Output:

Iterator<Enumerate, type=Pair<uint, string>>

type ExhaustedIter

type ExhaustedIter[T any] struct {
	BaseIter[T]
}

ExhaustedIter iterator, see Exhausted.

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.

func (ExhaustedIter[T]) String added in v1.1.0

func (iter ExhaustedIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

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

Iterator<Exhausted, type=int>

type FilterIter

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

FilterIter iterator, see Filter.

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
Example (Method)
package main

import (
	"fmt"

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

func main() {
	filtered := iter.Lift([]int{0, 1, 0, 2}).Exclude(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
Example (Method)
package main

import (
	"fmt"

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

func main() {
	filtered := iter.Lift([]int{0, 1, 0, 2}).Filter(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.

func (FilterIter[T]) String added in v1.1.0

func (iter FilterIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.Filter[int](iter.Exhausted[int](), filters.IsZero[int]))
}
Output:

Iterator<Filter, type=int>

type FilterMapIter added in v0.7.0

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

FilterMapIter iterator, see FilterMap.

func FilterMap added in v0.7.0

func FilterMap[T any, U any](iter Iterator[T], fun func(T) option.Option[U]) *FilterMapIter[T, U]

FilterMap instantiates a *FilterMapIter that selectively yields only results that cause the provided function to return `true` with a map operation performed on them.

Unlike other iterators (e.g. Filter), it is not possible to call FilterMap as a method on iterators defined in this package. This is due to a limitation of Go's type system; new type parameters cannot be defined on methods.

Example
package main

import (
	"fmt"

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

func main() {
	selectAndTripleOdds := func(x int) option.Option[int] {
		if x%2 == 0 {
			return option.None[int]()
		}
		return option.Some(x * 3)
	}

	triples := iter.FilterMap[int](
		iter.Count().Take(6),
		selectAndTripleOdds,
	)

	fmt.Println(triples.Collect())
}
Output:

[3 9 15]
Example (Method)
package main

import (
	"fmt"

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

func main() {
	selectAndTripleOdds := func(x int) option.Option[int] {
		if x%2 == 0 {
			return option.None[int]()
		}
		return option.Some(x * 3)
	}

	triples := iter.Count().Take(6).FilterMap(selectAndTripleOdds)

	fmt.Println(triples.Collect())
}
Output:

[3 9 15]

func (*FilterMapIter[T, U]) Next added in v0.7.0

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

Next implements the Iterator interface.

func (FilterMapIter[T, U]) String added in v1.1.0

func (iter FilterMapIter[T, U]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.FilterMap[int](iter.Exhausted[int](), func(_ int) option.Option[int] {
		return option.Some(42)
	}))
}
Output:

Iterator<FilterMap, type=int>

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 option.None variant on every subsequent call.

type LiftHashMapIter added in v0.8.0

type LiftHashMapIter[T comparable, U any] struct {
	BaseIter[Pair[T, U]]
	// contains filtered or unexported fields
}

LiftHashMapIter iterator, see LiftHashMap.

func LiftHashMap added in v0.8.0

func LiftHashMap[T comparable, U any](hashmap map[T]U) *LiftHashMapIter[T, U]

LiftHashMap instantiates a *LiftHashMapIter that will yield all items in the provided map as a Pair.

Unlike most iterators, LiftHashMap should be closed after usage (because range order is non-deterministic and the iterator needs to preserve its progress). This restriction may be removed if/when Go has a "yield" keyword.

The iterator is closed when any of the two conditions are met.

1. The caller explicitly invokes the `Close` method. 2. The iterator is exhausted.

It is safe to call Close multiple times or after exhaustion. It is not necessary to call Close if exhaustion is guaranteed, but may be wise to redundantly call Close if you're unsure.

Example
package main

import (
	"fmt"
	"sort"

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

func main() {
	pokemon := make(map[string]string)
	pokemon["name"] = "pikachu"
	pokemon["type"] = "electric"

	items := iter.LiftHashMap(pokemon).Collect()
	sort.Slice(items, func(i, j int) bool {
		return items[i].One < items[j].One
	})

	fmt.Println(items)
}
Output:

[(name, pikachu) (type, electric)]

func (*LiftHashMapIter[T, U]) Close added in v0.8.0

func (iter *LiftHashMapIter[T, U]) Close() error

Close the iterator. See LiftHashMap's documentation for details.

This function implements the io.Closer interface.

This function can never fail and the error can be ignored.

func (*LiftHashMapIter[T, U]) Next added in v0.8.0

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

Next implements the Iterator interface.

func (LiftHashMapIter[T, U]) String added in v1.1.0

func (iter LiftHashMapIter[T, U]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.LiftHashMap(make(map[string]string)))
}
Output:

Iterator<LiftHashMap, type=Pair<string, string>>

type LiftHashMapKeysIter added in v0.8.0

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

LiftHashMapKeysIter iterator, see LiftHashMapKeys.

func LiftHashMapKeys added in v0.8.0

func LiftHashMapKeys[T comparable, U any](hashmap map[T]U) *LiftHashMapKeysIter[T, U]

LiftHashMapKeys instantiates a *LiftHashMapKeysIter that will yield all keys in the provided map.

See LiftHashMap for information on closing this iterator.

Example
package main

import (
	"fmt"
	"sort"

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

func main() {
	pokemon := make(map[string]string)
	pokemon["name"] = "pikachu"
	pokemon["type"] = "electric"

	keys := iter.LiftHashMapKeys(pokemon).Collect()
	sort.Strings(keys)

	fmt.Println(keys)
}
Output:

[name type]

func (*LiftHashMapKeysIter[T, U]) Close added in v0.8.0

func (iter *LiftHashMapKeysIter[T, U]) Close() error

Close the iterator. See LiftHashMap's documentation for details.

This function implements the io.Closer interface.

This function can never fail and the error can be ignored.

func (*LiftHashMapKeysIter[T, U]) Next added in v0.8.0

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

Next implements the Iterator interface.

func (LiftHashMapKeysIter[T, U]) String added in v1.1.0

func (iter LiftHashMapKeysIter[T, U]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.LiftHashMapKeys(make(map[string]int)))
}
Output:

Iterator<LiftHashMapKeys, type=string>

type LiftHashMapValuesIter added in v0.8.0

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

LiftHashMapValuesIter iterator, see LiftHashMapValues.

func LiftHashMapValues added in v0.8.0

func LiftHashMapValues[T comparable, U any](hashmap map[T]U) *LiftHashMapValuesIter[T, U]

LiftHashMapValues instantiates a *LiftHashMapValuesIter that will yield all keys in the provided map.

See LiftHashMap for information on closing this iterator.

Example
package main

import (
	"fmt"
	"sort"

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

func main() {
	pokemon := make(map[string]string)
	pokemon["name"] = "pikachu"
	pokemon["type"] = "electric"

	values := iter.LiftHashMapValues(pokemon).Collect()
	sort.Strings(values)

	fmt.Println(values)
}
Output:

[electric pikachu]

func (*LiftHashMapValuesIter[T, U]) Close added in v0.8.0

func (iter *LiftHashMapValuesIter[T, U]) Close() error

Close the iterator. See LiftHashMap's documentation for details.

This function implements the io.Closer interface.

This function can never fail and the error can be ignored.

func (*LiftHashMapValuesIter[T, U]) Next added in v0.8.0

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

Next implements the Iterator interface.

func (LiftHashMapValuesIter[T, U]) String added in v1.1.0

func (iter LiftHashMapValuesIter[T, U]) String() string

String implements the fmt.Stringer interface

type LiftIter

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

LiftIter iterator, see Lift.

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(positives.Collect())
}
Output:

[4 6 4]

func (*LiftIter[T]) Next

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

Next implements the Iterator interface.

func (LiftIter[T]) String added in v1.1.0

func (iter LiftIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

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

Iterator<Lift, type=int>

type LinesIter

type LinesIter struct {
	BaseIter[result.Result[[]byte]]
	// contains filtered or unexported fields
}

LinesIter iterator, see Lines.

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 wrapped in a result.Result.

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(unwrapped.Collect())
}
Output:

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

func (*LinesIter) CollectResults added in v0.18.0

func (iter *LinesIter) CollectResults() result.Result[[][]byte]

CollectResults is a convenience method for CollectResults, providing this iterator as an argument.

func (*LinesIter) Next

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

Next implements the Iterator interface.

func (LinesIter) String added in v1.1.0

func (iter LinesIter) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"bytes"
	"fmt"

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

func main() {
	fmt.Println(iter.Lines(bytes.NewBufferString("hello\nthere")))
}
Output:

Iterator<Lines, type=Result<[]byte>>

type LinesStringIter added in v0.18.0

type LinesStringIter struct {
	BaseIter[result.Result[string]]
	// contains filtered or unexported fields
}

func LinesString

func LinesString(r io.Reader) *LinesStringIter

LinesString instantiates a *LinesStringIter that behaves like a *LinesIter except that it yields strings. See LinesIter.

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(unwrapped.Collect())
}
Output:

[hello there]

func (*LinesStringIter) CollectResults added in v0.18.0

func (iter *LinesStringIter) CollectResults() result.Result[[]string]

CollectResults is a convenience method for CollectResults, providing this iterator as an argument.

func (*LinesStringIter) Next added in v0.18.0

Next implements the Iterator interface.

func (LinesStringIter) String added in v1.1.0

func (iter LinesStringIter) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"bytes"
	"fmt"

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

func main() {
	fmt.Println(iter.LinesString(bytes.NewBufferString("hello\nthere")))
}
Output:

Iterator<LinesString, type=Result<string>>

type MapIter

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

MapIter iterator, see Map.

func Map

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

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

Unlike other iterators (e.g. Filter), it is not possible to call Map as a method on iterators defined in this package. This is due to a limitation of Go's type system; new type parameters cannot be defined on methods.

Example
package main

import (
	"fmt"

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

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

	fmt.Println(items)
}
Output:

[0 2 4 6]

func Transform added in v0.16.0

func Transform[T any](iter Iterator[T], op func(T) T) *MapIter[T, T]

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

Transform is a special case of Map where the argument and returned value for the operation are of the same type. This makes it possible to call Transform as a method on other iterators.

Example
package main

import (
	"fmt"

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

func main() {
	addTwo := func(number int) int { return number + 2 }
	numbers := iter.Transform[int](iter.Count(), addTwo).Take(3).Collect()

	fmt.Println(numbers)
}
Output:

[2 3 4]
Example (Method)
package main

import (
	"fmt"

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

func main() {
	addTwo := func(number int) int { return number + 2 }
	numbers := iter.Count().Transform(addTwo).Take(3).Collect()

	fmt.Println(numbers)
}
Output:

[2 3 4]

func (*MapIter[T, U]) Next

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

Next implements the Iterator interface.

func (MapIter[T, U]) String added in v1.0.0

func (iter MapIter[T, U]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"
	"strconv"

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

func main() {
	fmt.Println(iter.Map[int, string](iter.Count(), strconv.Itoa))
}
Output:

Iterator<Map, type=string>

type Pair added in v0.16.0

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

Pairs of values.

func (Pair[T, U]) String added in v0.18.0

func (p Pair[T, U]) String() string

type RepeatIter added in v0.12.0

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

RepeatIter iterator, see Repeat.

func Repeat added in v0.12.0

func Repeat[T any](item T) *RepeatIter[T]

Repeat instantiates a *RepeatIter always yield the provided item.

This iterator will never be exhausted.

Example
package main

import (
	"fmt"

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

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

Some(42)
Some(42)

func (*RepeatIter[T]) Next added in v0.12.0

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

Next implements the Iterator interface.

func (RepeatIter[T]) String added in v1.1.0

func (iter RepeatIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.Repeat[int](42))
}
Output:

Iterator<Repeat, type=int>

type RunesIter added in v0.17.0

type RunesIter struct {
	BaseIter[rune]
	// contains filtered or unexported fields
}

RunesIter iterator, see Runes.

func Runes added in v0.17.0

func Runes[T string | []rune](runes T) *RunesIter

Runes instantiates a *RunesIter that will yield a rune on iteration.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/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/iter"
)

func main() {
	runes := iter.Runes([]rune("Hello, 世界!")).Collect()

	fmt.Println(string(runes[7:9]))
}
Output:

世界

func (*RunesIter) Next added in v0.17.0

func (iter *RunesIter) Next() option.Option[rune]

Next implements the Iterator interface.

func (RunesIter) String added in v1.1.0

func (iter RunesIter) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(iter.Runes("Hello, 世界!"))
}
Output:

Iterator<Runes>

type TakeIter

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

TakeIter iterator, see Take.

func Take

func Take[T any](iter Iterator[T], limit uint) *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
Example (Method)
package main

import (
	"fmt"

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

func main() {
	iter := iter.Count().Take(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.

func (TakeIter[T]) String added in v1.1.0

func (iter TakeIter[T]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

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

Iterator<Take, type=int>

type ZipIter

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

ZipIter iterator, see Zip.

func Zip

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

Zip instantiates a *ZipIter yielding a Pair containing the result of a call to each provided Iterator's Next. This iterator is exhausted when one of the provided iterators is exhausted.

Example
package main

import (
	"fmt"

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

func main() {
	evens := iter.Filter[int](iter.Count(), filters.IsEven[int])
	odds := iter.Filter[int](iter.Count(), filters.IsOdd[int])

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

[(0, 1) (2, 3) (4, 5)]

func (*ZipIter[T, U]) Next

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

Next implements the Iterator interface.

func (ZipIter[T, U]) String added in v1.1.0

func (iter ZipIter[T, U]) String() string

String implements the fmt.Stringer interface

Example
package main

import (
	"fmt"

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

func main() {
	evens := iter.Filter[int](iter.Count(), filters.IsEven[int])
	odds := iter.Filter[int](iter.Count(), filters.IsOdd[int])

	fmt.Println(iter.Zip[int, int](evens, odds))
}
Output:

Iterator<Zip, type=Pair<int, int>>

Directories

Path Synopsis
This package contains functions intended for use with [iter.Filter].
This package contains functions intended for use with [iter.Filter].
This package contains functions intended for use with [iter.Fold] and [iter.Map] / [iter.Transform].
This package contains functions intended for use with [iter.Fold] and [iter.Map] / [iter.Transform].

Jump to

Keyboard shortcuts

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