Home

Awesome

Functional Programming in Go

GitHub release (with filter) Actions Status Go Reference Go Report Card codecov

A library of iterators for use with iter.Seq. Requires Go 1.23+.

// The first 5 natural numbers
numbers := slices.Collect(
	it.Take(it.Count[int](), 5),
)

// All even numbers
evens := it.Filter(it.Count[int](), filter.IsEven)

// String representations of integers
numbers := it.Map(it.Count[int](), strconv.Itoa)

Read the docs to see the full iterator library.

Installation

go get github.com/BooleanCat/go-functional/v2@latest

Iterator Chaining

The iterators in this package were designed to be used with the native iter.Seq from Go's standard library. In order to facilitate complex sequences of iterators, the itx package provides Iterator and Iterator2 as wrappers around iter.Seq and iter.Seq2 that allow for chaining operations.

Let's take a look at an example:

// The first 10 odd integers
itx.Count[int]().Filter(filter.IsOdd).Take(10).Collect()

Most iterators support chaining. A notable exception is it.Map which cannot support chaining due to limitations on Go's type system.