iter

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package iter provides functional programming utilities for Go 1.23+ iterators.

This package offers a comprehensive set of operations for working with lazy sequences using Go's native iter.Seq and iter.Seq2 types. It follows functional programming principles and provides monadic operations, transformations, and reductions.

The package supports:

  • Functor operations (Map, MapWithIndex, MapWithKey)
  • Monad operations (Chain, Flatten, Ap)
  • Filtering (Filter, FilterMap, FilterWithIndex, FilterWithKey)
  • Folding and reduction (Reduce, Fold, FoldMap)
  • Sequence construction (Of, From, MakeBy, Replicate)
  • Sequence combination (Zip, Prepend, Append)

All operations are lazy and only execute when the sequence is consumed via iteration.

Example usage:

// Create a sequence and transform it
seq := From(1, 2, 3, 4, 5)
doubled := Map(N.Mul(2))(seq)

// Filter and reduce
evens := Filter(func(x int) bool { return x%2 == 0 })(doubled)
sum := MonadReduce(evens, func(acc, x int) int { return acc + x }, 0)
// sum = 20 (2+4+6+8+10 from doubled evens)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fold

func Fold[A any](m M.Monoid[A]) func(Seq[A]) A

Fold returns a function that folds a sequence using a monoid. This is the curried version of MonadFold.

Example:

import "github.com/IBM/fp-go/v2/number"
sumAll := Fold(number.MonoidSum[int]())
seq := From(1, 2, 3, 4, 5)
result := sumAll(seq)
// returns: 15

func FoldMap

func FoldMap[A, B any](m M.Monoid[B]) func(func(A) B) func(Seq[A]) B

FoldMap returns a function that maps and folds using a monoid. This is the curried version of MonadFoldMap.

Example:

import "github.com/IBM/fp-go/v2/string"
stringify := FoldMap(string.Monoid)(func(x int) string {
    return fmt.Sprintf("%d ", x)
})
seq := From(1, 2, 3)
result := stringify(seq)
// returns: "1 2 3 "
Example
seq := From("a", "b", "c")
fold := FoldMap[string](S.Monoid)(strings.ToUpper)
result := fold(seq)
fmt.Println(result)
Output:

ABC

func FoldMapWithIndex

func FoldMapWithIndex[A, B any](m M.Monoid[B]) func(func(int, A) B) func(Seq[A]) B

FoldMapWithIndex returns a function that maps with index and folds. This is the curried version of MonadFoldMapWithIndex.

Example:

import "github.com/IBM/fp-go/v2/string"
indexedStringify := FoldMapWithIndex(string.Monoid)(func(i int, s string) string {
    return fmt.Sprintf("%d:%s ", i, s)
})
seq := From("a", "b", "c")
result := indexedStringify(seq)
// returns: "0:a 1:b 2:c "

func FoldMapWithKey

func FoldMapWithKey[K, A, B any](m M.Monoid[B]) func(func(K, A) B) func(Seq2[K, A]) B

FoldMapWithKey returns a function that maps with key and folds. This is the curried version of MonadFoldMapWithKey.

func MapToArray

func MapToArray[A, B any](f func(A) B) func(Seq[A]) []B

func MonadFold

func MonadFold[A any](fa Seq[A], m M.Monoid[A]) A

MonadFold folds a sequence using a monoid's concat operation and empty value.

Example:

import "github.com/IBM/fp-go/v2/number"
seq := From(1, 2, 3, 4, 5)
sum := MonadFold(seq, number.MonoidSum[int]())
// returns: 15

func MonadFoldMap

func MonadFoldMap[A, B any](fa Seq[A], f func(A) B, m M.Monoid[B]) B

MonadFoldMap maps each element to a monoid value and combines them using the monoid.

Example:

import "github.com/IBM/fp-go/v2/string"
seq := From(1, 2, 3)
result := MonadFoldMap(seq, func(x int) string {
    return fmt.Sprintf("%d ", x)
}, string.Monoid)
// returns: "1 2 3 "

func MonadFoldMapWithIndex

func MonadFoldMapWithIndex[A, B any](fa Seq[A], f func(int, A) B, m M.Monoid[B]) B

MonadFoldMapWithIndex maps each element with its index to a monoid value and combines them.

Example:

import "github.com/IBM/fp-go/v2/string"
seq := From("a", "b", "c")
result := MonadFoldMapWithIndex(seq, func(i int, s string) string {
    return fmt.Sprintf("%d:%s ", i, s)
}, string.Monoid)
// returns: "0:a 1:b 2:c "

func MonadFoldMapWithKey

func MonadFoldMapWithKey[K, A, B any](fa Seq2[K, A], f func(K, A) B, m M.Monoid[B]) B

MonadFoldMapWithKey maps each key-value pair to a monoid value and combines them.

Example:

import "github.com/IBM/fp-go/v2/string"
seq := Of2("x", 10)
result := MonadFoldMapWithKey(seq, func(k string, v int) string {
    return fmt.Sprintf("%s:%d ", k, v)
}, string.Monoid)
// returns: "x:10 "

func MonadMapToArray

func MonadMapToArray[A, B any](fa Seq[A], f func(A) B) []B

func MonadReduce

func MonadReduce[A, B any](fa Seq[A], f func(B, A) B, initial B) B

MonadReduce reduces a sequence to a single value by applying a function to each element and an accumulator, starting with an initial value.

Example:

seq := From(1, 2, 3, 4, 5)
sum := MonadReduce(seq, func(acc, x int) int { return acc + x }, 0)
// returns: 15

func MonadReduceWithIndex

func MonadReduceWithIndex[A, B any](fa Seq[A], f func(int, B, A) B, initial B) B

MonadReduceWithIndex reduces a sequence using a function that also receives the element's index.

Example:

seq := From(10, 20, 30)
result := MonadReduceWithIndex(seq, func(i, acc, x int) int {
    return acc + (i * x)
}, 0)
// returns: 0*10 + 1*20 + 2*30 = 80

func MonadReduceWithKey

func MonadReduceWithKey[K, A, B any](fa Seq2[K, A], f func(K, B, A) B, initial B) B

MonadReduceWithKey reduces a key-value sequence using a function that receives the key.

Example:

seq := Of2("x", 10)
result := MonadReduceWithKey(seq, func(k string, acc int, v int) int {
    return acc + v
}, 0)
// returns: 10

func Monoid

func Monoid[T any]() M.Monoid[Seq[T]]

Monoid returns a Monoid instance for Seq[T]. The monoid's concat operation concatenates sequences, and the empty value is an empty sequence.

Example:

m := Monoid[int]()
seq1 := From(1, 2)
seq2 := From(3, 4)
result := m.Concat(seq1, seq2)
// yields: 1, 2, 3, 4
Example
m := Monoid[int]()
seq1 := From(1, 2, 3)
seq2 := From(4, 5, 6)
combined := m.Concat(seq1, seq2)
result := toSlice(combined)
fmt.Println(result)
Output:

[1 2 3 4 5 6]

func Reduce

func Reduce[A, B any](f func(B, A) B, initial B) func(Seq[A]) B

Reduce returns a function that reduces a sequence to a single value. This is the curried version of MonadReduce.

Example:

sum := Reduce(func(acc, x int) int { return acc + x }, 0)
seq := From(1, 2, 3, 4, 5)
result := sum(seq)
// returns: 15

func ReduceWithIndex

func ReduceWithIndex[A, B any](f func(int, B, A) B, initial B) func(Seq[A]) B

ReduceWithIndex returns a function that reduces with index. This is the curried version of MonadReduceWithIndex.

Example:

weightedSum := ReduceWithIndex(func(i, acc, x int) int {
    return acc + (i * x)
}, 0)
seq := From(10, 20, 30)
result := weightedSum(seq)
// returns: 80

func ReduceWithKey

func ReduceWithKey[K, A, B any](f func(K, B, A) B, initial B) func(Seq2[K, A]) B

ReduceWithKey returns a function that reduces key-value pairs. This is the curried version of MonadReduceWithKey.

Example:

sumValues := ReduceWithKey(func(k string, acc int, v int) int {
    return acc + v
}, 0)
seq := Of2("x", 10)
result := sumValues(seq)
// returns: 10

func Zip

func Zip[A, B any](fa Seq[A]) func(Seq[B]) Seq2[A, B]

Zip returns a function that zips a sequence with another sequence. This is the curried version of MonadZip.

Example:

seqA := From(1, 2, 3)
zipWithA := Zip(seqA)
seqB := From("a", "b", "c")
result := zipWithA(seqB)
// yields: (1, "a"), (2, "b"), (3, "c")

Types

type Iterator

type Iterator[T any] = stateless.Iterator[T]

Iterator is a stateless iterator type.

type Kleisli

type Kleisli[A, B any] = func(A) Seq[B]

Kleisli represents a function that takes a value and returns a sequence. This is the monadic bind operation for sequences.

type Kleisli2

type Kleisli2[K, A, B any] = func(A) Seq2[K, B]

Kleisli2 represents a function that takes a value and returns a key-value sequence.

type Operator

type Operator[A, B any] = Kleisli[Seq[A], B]

Operator represents a transformation from one sequence to another. It's a function that takes a Seq[A] and returns a Seq[B].

func Ap

func Ap[B, A any](fa Seq[A]) Operator[func(A) B, B]

Ap returns a function that applies functions to values. This is the curried version of MonadAp.

Example:

applyTo5 := Ap(From(5))
fns := From(N.Mul(2), N.Add(10))
result := applyTo5(fns)
// yields: 10, 15

func Append

func Append[A any](tail A) Operator[A, A]

Append returns a function that adds an element to the end of a sequence.

Example:

seq := From(1, 2, 3)
result := Append(4)(seq)
// yields: 1, 2, 3, 4

func Chain

func Chain[A, B any](f func(A) Seq[B]) Operator[A, B]

Chain returns a function that chains (flatMaps) a sequence transformation. This is the curried version of MonadChain.

Example:

duplicate := Chain(func(x int) Seq[int] { return From(x, x) })
seq := From(1, 2, 3)
result := duplicate(seq)
// yields: 1, 1, 2, 2, 3, 3
Example
seq := From(1, 2)
result := F.Pipe2(
	seq,
	Chain(func(x int) Seq[int] {
		return From(x, x*10)
	}),
	toSlice[int],
)
fmt.Println(result)
Output:

[1 10 2 20]

func Filter

func Filter[A any](pred func(A) bool) Operator[A, A]

Filter returns a function that filters elements based on a predicate. This is the curried version of MonadFilter.

Example:

evens := Filter(func(x int) bool { return x%2 == 0 })
seq := From(1, 2, 3, 4, 5)
result := evens(seq)
// yields: 2, 4

func FilterMap

func FilterMap[A, B any](f option.Kleisli[A, B]) Operator[A, B]

FilterMap returns a function that filters and maps in one operation. This is the curried version of MonadFilterMap.

Example:

evenDoubled := FilterMap(func(x int) Option[int] {
    if x%2 == 0 {
        return option.Some(x * 2)
    }
    return option.None[int]()
})
seq := From(1, 2, 3, 4)
result := evenDoubled(seq)
// yields: 4, 8

func FilterMapWithIndex

func FilterMapWithIndex[A, B any](f func(int, A) Option[B]) Operator[A, B]

FilterMapWithIndex returns a function that filters and maps with index. This is the curried version of MonadFilterMapWithIndex.

Example:

evenIndexed := FilterMapWithIndex(func(i int, s string) Option[string] {
    if i%2 == 0 {
        return option.Some(s)
    }
    return option.None[string]()
})
seq := From("a", "b", "c", "d")
result := evenIndexed(seq)
// yields: "a", "c"

func FilterWithIndex

func FilterWithIndex[A any](pred func(int, A) bool) Operator[A, A]

FilterWithIndex returns a function that filters elements based on their index and value. This is the curried version of MonadFilterWithIndex.

Example:

evenIndices := FilterWithIndex(func(i int, s string) bool { return i%2 == 0 })
seq := From("a", "b", "c", "d")
result := evenIndices(seq)
// yields: "a", "c"

func Flap

func Flap[B, A any](a A) Operator[func(A) B, B]

Flap returns a function that applies a fixed value to functions. This is the curried version of MonadFlap.

func Map

func Map[A, B any](f func(A) B) Operator[A, B]

Map returns a function that transforms each element in a sequence. This is the curried version of MonadMap.

Example:

double := Map(N.Mul(2))
seq := From(1, 2, 3)
result := double(seq)
// yields: 2, 4, 6

func MapWithIndex

func MapWithIndex[A, B any](f func(int, A) B) Operator[A, B]

MapWithIndex returns a function that transforms elements with their indices. This is the curried version of MonadMapWithIndex.

Example:

addIndex := MapWithIndex(func(i int, s string) string {
    return fmt.Sprintf("%d:%s", i, s)
})
seq := From("a", "b", "c")
result := addIndex(seq)
// yields: "0:a", "1:b", "2:c"

func Prepend

func Prepend[A any](head A) Operator[A, A]

Prepend returns a function that adds an element to the beginning of a sequence.

Example:

seq := From(2, 3, 4)
result := Prepend(1)(seq)
// yields: 1, 2, 3, 4

type Operator2

type Operator2[K, A, B any] = Kleisli2[K, Seq2[K, A], B]

Operator2 represents a transformation from one key-value sequence to another.

func FilterMapWithKey

func FilterMapWithKey[K, A, B any](f func(K, A) Option[B]) Operator2[K, A, B]

FilterMapWithKey returns a function that filters and maps key-value pairs. This is the curried version of MonadFilterMapWithKey.

Example:

largeDoubled := FilterMapWithKey(func(k string, v int) Option[int] {
    if v > 5 {
        return option.Some(v * 2)
    }
    return option.None[int]()
})
seq := Of2("x", 10)
result := largeDoubled(seq)
// yields: ("x", 20)

func FilterWithKey

func FilterWithKey[K, A any](pred func(K, A) bool) Operator2[K, A, A]

FilterWithKey returns a function that filters key-value pairs based on a predicate. This is the curried version of MonadFilterWithKey.

Example:

largeValues := FilterWithKey(func(k string, v int) bool { return v > 5 })
seq := Of2("x", 10)
result := largeValues(seq)
// yields: ("x", 10)

func MapWithKey

func MapWithKey[K, A, B any](f func(K, A) B) Operator2[K, A, B]

MapWithKey returns a function that transforms values using their keys. This is the curried version of MonadMapWithKey.

Example:

doubleValue := MapWithKey(func(k string, v int) int { return v * 2 })
seq := Of2("x", 10)
result := doubleValue(seq)
// yields: ("x", 20)

type Option

type Option[A any] = option.Option[A]

Option represents an optional value, either Some(value) or None.

type Predicate

type Predicate[T any] = predicate.Predicate[T]

Predicate is a function that tests a value and returns a boolean.

type Seq

type Seq[T any] = I.Seq[T]

Seq is a single-value iterator sequence from Go 1.23+. It represents a lazy sequence of values that can be iterated using range.

func Empty

func Empty[A any]() Seq[A]

Empty returns an empty sequence that yields no elements.

Example:

seq := Empty[int]()
// yields nothing

func Flatten

func Flatten[A any](mma Seq[Seq[A]]) Seq[A]

Flatten flattens a sequence of sequences into a single sequence.

Example:

nested := From(From(1, 2), From(3, 4), From(5))
result := Flatten(nested)
// yields: 1, 2, 3, 4, 5

func From

func From[A any](data ...A) Seq[A]

From creates a sequence from a variadic list of elements.

Example:

seq := From(1, 2, 3, 4, 5)
// yields: 1, 2, 3, 4, 5

func MakeBy

func MakeBy[A any](n int, f func(int) A) Seq[A]

MakeBy creates a sequence of n elements by applying a function to each index. Returns an empty sequence if n <= 0.

Example:

seq := MakeBy(5, func(i int) int { return i * i })
// yields: 0, 1, 4, 9, 16

func MonadAp

func MonadAp[B, A any](fab Seq[func(A) B], fa Seq[A]) Seq[B]

MonadAp applies a sequence of functions to a sequence of values. This is the applicative apply operation.

Example:

fns := From(N.Mul(2), N.Add(10))
vals := From(5, 3)
result := MonadAp(fns, vals)
// yields: 10, 6, 15, 13 (each function applied to each value)

func MonadChain

func MonadChain[A, B any](as Seq[A], f Kleisli[A, B]) Seq[B]

MonadChain applies a function that returns a sequence to each element and flattens the results. This is the monadic bind operation (flatMap).

Example:

seq := From(1, 2, 3)
result := MonadChain(seq, func(x int) Seq[int] {
    return From(x, x*10)
})
// yields: 1, 10, 2, 20, 3, 30

func MonadFilter

func MonadFilter[A any](as Seq[A], pred func(A) bool) Seq[A]

MonadFilter returns a sequence containing only elements that satisfy the predicate.

Example:

seq := From(1, 2, 3, 4, 5)
result := MonadFilter(seq, func(x int) bool { return x%2 == 0 })
// yields: 2, 4

func MonadFilterMap

func MonadFilterMap[A, B any](as Seq[A], f option.Kleisli[A, B]) Seq[B]

MonadFilterMap applies a function that returns an Option to each element, keeping only the Some values and unwrapping them.

Example:

seq := From(1, 2, 3, 4, 5)
result := MonadFilterMap(seq, func(x int) Option[int] {
    if x%2 == 0 {
        return option.Some(x * 10)
    }
    return option.None[int]()
})
// yields: 20, 40

func MonadFilterMapWithIndex

func MonadFilterMapWithIndex[A, B any](as Seq[A], f func(int, A) Option[B]) Seq[B]

MonadFilterMapWithIndex applies a function with index that returns an Option, keeping only the Some values.

Example:

seq := From("a", "b", "c")
result := MonadFilterMapWithIndex(seq, func(i int, s string) Option[string] {
    if i%2 == 0 {
        return option.Some(fmt.Sprintf("%d:%s", i, s))
    }
    return option.None[string]()
})
// yields: "0:a", "2:c"

func MonadFilterWithIndex

func MonadFilterWithIndex[A any](as Seq[A], pred func(int, A) bool) Seq[A]

MonadFilterWithIndex filters elements using a predicate that also receives the element's index.

Example:

seq := From("a", "b", "c", "d")
result := MonadFilterWithIndex(seq, func(i int, s string) bool { return i%2 == 0 })
// yields: "a", "c" (elements at even indices)

func MonadFlap

func MonadFlap[B, A any](fab Seq[func(A) B], a A) Seq[B]

MonadFlap applies a fixed value to a sequence of functions. This is the dual of MonadAp.

Example:

fns := From(N.Mul(2), N.Add(10))
result := MonadFlap(fns, 5)
// yields: 10, 15

func MonadMap

func MonadMap[A, B any](as Seq[A], f func(A) B) Seq[B]

MonadMap transforms each element in a sequence using the provided function. This is the monadic version that takes the sequence as the first parameter.

Example:

seq := From(1, 2, 3)
result := MonadMap(seq, N.Mul(2))
// yields: 2, 4, 6

func MonadMapWithIndex

func MonadMapWithIndex[A, B any](as Seq[A], f func(int, A) B) Seq[B]

MonadMapWithIndex transforms each element in a sequence using a function that also receives the element's index.

Example:

seq := From("a", "b", "c")
result := MonadMapWithIndex(seq, func(i int, s string) string {
    return fmt.Sprintf("%d:%s", i, s)
})
// yields: "0:a", "1:b", "2:c"

func Of

func Of[A any](a A) Seq[A]

Of creates a sequence containing a single element.

Example:

seq := Of(42)
// yields: 42

func Replicate

func Replicate[A any](n int, a A) Seq[A]

Replicate creates a sequence containing n copies of the same element.

Example:

seq := Replicate(3, "hello")
// yields: "hello", "hello", "hello"

type Seq2

type Seq2[K, V any] = I.Seq2[K, V]

Seq2 is a key-value iterator sequence from Go 1.23+. It represents a lazy sequence of key-value pairs that can be iterated using range.

func MonadFilterMapWithKey

func MonadFilterMapWithKey[K, A, B any](as Seq2[K, A], f func(K, A) Option[B]) Seq2[K, B]

MonadFilterMapWithKey applies a function with key that returns an Option to key-value pairs, keeping only the Some values.

Example:

seq := Of2("x", 10)
result := MonadFilterMapWithKey(seq, func(k string, v int) Option[int] {
    if v > 5 {
        return option.Some(v * 2)
    }
    return option.None[int]()
})
// yields: ("x", 20)

func MonadFilterWithKey

func MonadFilterWithKey[K, A any](as Seq2[K, A], pred func(K, A) bool) Seq2[K, A]

MonadFilterWithKey filters key-value pairs using a predicate that receives both key and value.

Example:

seq := Of2("x", 10)
result := MonadFilterWithKey(seq, func(k string, v int) bool { return v > 5 })
// yields: ("x", 10)

func MonadMapWithKey

func MonadMapWithKey[K, A, B any](as Seq2[K, A], f func(K, A) B) Seq2[K, B]

MonadMapWithKey transforms values in a key-value sequence using a function that receives both key and value.

Example:

seq := Of2("x", 10)
result := MonadMapWithKey(seq, func(k string, v int) int { return v * 2 })
// yields: ("x", 20)

func MonadZip

func MonadZip[A, B any](fb Seq[B], fa Seq[A]) Seq2[A, B]

MonadZip combines two sequences into a sequence of pairs. The resulting sequence stops when either input sequence is exhausted.

Example:

seqA := From(1, 2, 3)
seqB := From("a", "b")
result := MonadZip(seqB, seqA)
// yields: (1, "a"), (2, "b")

func Of2

func Of2[K, A any](k K, a A) Seq2[K, A]

Of2 creates a key-value sequence containing a single key-value pair.

Example:

seq := Of2("key", 100)
// yields: ("key", 100)

Jump to

Keyboard shortcuts

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