steams

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 5 Imported by: 0

README

Steams

Go functional programming library using iterators

Caveats

  • This library requires Go 1.23+
  • Contains several streams (called steams) using iterators, so the streams are mostly lazy.

Insstallation

go get -u github.com/javiorfo/steams/v2@latest

Example

More examples here
package main

import (
  "fmt"

  "github.com/javiorfo/steams/v2"
)

type Person struct {
  Name string
  Age  int
  Pets []Pet
}

type Pet struct {
  Name string
  Type string
  Age  int
}

const DOG = "DOG"
const CAT = "CAT"

var PeopleWithPets = []Person{
  {Name: "Carl", Age: 34, Pets: []Pet{}},
  {Name: "John", Age: 20, Pets: []Pet{{Name: "Bobby", Type: DOG, Age: 2}, {Name: "Mike", Type: DOG, Age: 12}}},
  {Name: "Grace", Age: 40, Pets: []Pet{{Name: "Pepe", Type: DOG, Age: 4}, {Name: "Snowball", Type: CAT, Age: 8}}},
  {Name: "Robert", Age: 40, Pets: []Pet{{Name: "Ronny", Type: CAT, Age: 3}}},
}


func main() {
  persons := steams.FromSlice(PeopleWithPets).
	  Filter(func(p Person) bool { return p.Age > 21 }).
	  Inspect(func(p Person) { fmt.Println("After Filter => Person:", p.Name) })

  steams.FlatMap(persons, func(p Person) steams.It[Pet] {
	  return steams.FromSlice(p.Pets)
  }).
	  Inspect(func(p Pet) { fmt.Println("After FlatMap = Pet:", p.Name) }).
	  Filter(isCat).
	  Inspect(func(p Pet) { fmt.Println("After second Filter => Pet:", p.Name) }).
      Compare(comparator).
      Inspect(print).
      OrPanic("No results")
}

func isCat(p Pet) bool {
  if p.Type == data.CAT {
	  return true
  }
  return false
}

func comparator(a Pet, b Pet) bool {
  return a.Age < b.Age
}

func print(p Pet) {
  fmt.Printf("The younger cat of the list is %s, age %d", p.Name, p.Age)
}

Api

// It[T] is type based on iter.Seq[T] for a collection of elements,
// providing various methods for functional-style processing.
func (it It[T]) AsSeq() iter.Seq[T]
func (it It[T]) Filter(predicate func(T) bool) It[T]
func (it It[T]) Map(mapper func(T) T) It[T]
func (it It[T]) MapToString(mapper func(T) string) It[string]
func (it It[T]) MapToInt(mapper func(T) int) It[int]
func (it It[T]) FilterMap(mapper func(T) nilo.Option[T]) It[T]
func (it It[T]) FilterMapToString(mapper func(T) nilo.Option[string]) It[string]
func (it It[T]) FilterMapToInt(mapper func(T) nilo.Option[int]) It[int] {
func (it It[T]) FlatMap(mapper func(T) It[T]) It[T]
func (it It[T]) FlatMapToString(mapper func(T) It[string]) It[string]
func (it It[T]) FlatMapToInt(mapper func(T) It[int]) It[int]
func (it It[T]) Take(n int) It[T]
func (it It[T]) Count() int
func (it It[T]) ForEach(consumer func(T))
func (it It[T]) ForEachIdx(consumer func(int, T))
func (it It[T]) Inspect(inspector func(T)) It[T]
func (it It[T]) All(predicate func(T) bool) bool
func (it It[T]) Any(predicate func(T) bool) bool
func (it It[T]) None(predicate func(T) bool) bool
func (it It[T]) First() nilo.Option[T]
func (it It[T]) Find(predicate func(T) bool) nilo.Option[T]
func (it It[T]) TakeWhile(predicate func(T) bool) It[T]
func (it It[T]) SkipWhile(predicate func(T) bool) It[T]
func (it It[T]) Fold(initValue T, acc func(T, T) T) T
func (it It[T]) RFold(initValue T, acc func(T, T) T) T
func (it It[T]) Reverse() It[T]
func (it It[T]) Position(predicate func(T) bool) nilo.Option[int]
func (it It[T]) RPosition(predicate func(T) bool) nilo.Option[int]
func (it It[T]) Enumerate() iter.Seq2[int, T]
func (it It[T]) Last() nilo.Option[T]
func (it It[T]) Skip(n int) It[T]
func (it It[T]) SortBy(cmp func(T, T) int) It[T]
func (it It[T]) Compare(cmp func(T, T) bool) nilo.Option[T]
func (it It[T]) Collect() []T
func (it It[T]) Chain(i2 It[T]) It[T]
func (it It[T]) Nth(n int) nilo.Option[T]
func (it It[T]) Partition(politer func(T) bool) (It[T], It[T])

// It2[K, V] is type based on iter.Seq2[K, V] for a map of elements,
// providing various methods for functional-style processing.
func (it It2[K, V]) Filter(predicate func(K, V) bool) It2[K, V]
func (it It2[K, V]) Map(mapper func(K, V) (K, V)) It2[K, V]
func (it It2[K, V]) MapToString(mapper func(K, V) (K, string)) It2[K, string]
func (it It2[K, V]) MapToInt(mapper func(K, V) (K, int)) It2[K, int]
func (it It2[K, V]) ForEach(consumer func(K, V))
func (it It2[K, V]) SortBy(cmp func(K, K) bool) It2[K, V]
func (it It2[K, V]) Inspect(consumer func(K, V)) It2[K, V]
func (it It2[K, V]) Take(n int) It2[K, V]
func (it It2[K, V]) Values() It[V]
func (it It2[K, V]) Keys() It[K]
func (it It2[K, V]) All(predicate func(K, V) bool) bool
func (it It2[K, V]) Any(predicate func(K, V) bool) bool
func (it It2[K, V]) None(predicate func(K, V) bool) bool
func (it It2[K, V]) Compare(cmp func(K, K) bool) nilo.Option[Entry[K, V]]
func (it It2[K, V]) Collect() map[K]V
func (it It2[K, V]) Count() int

Integration functions

func From[T any](args ...T) It[T]
func FromSlice[T any](slice []T) It[T]
func FromMap[K comparable, V any](m map[K]V) It2[K, V]
func Distinct[T comparable](i It[T]) It[T]
func Map[T any, U any](i It[T], transform func(T) U) It[U]
func FlatMap[T any, U any](i It[T], transform func(T) It[U]) It[U]
func Fold[T any, R any](i It[T], initial R, accumulator func(R, T) R) R
func RFold[T any, R any](i It[T], initial R, accumulator func(T, R) R) R
func Flatten[V any](nested It[iter.Seq[V]]) It[V]
func GroupBy[K comparable, V any](i It[V], classifier func(V) K) It2[K, It[V]]
func GroupByCounting[K comparable, V any](i It[V], classifier func(V) K) It2[K, int]
func Zip[T, R any](i1 It[T], i2 It[R]) It[struct {First T; Second R}]
func CollectItToIt2[T, K comparable, V any](i It[T], keyFunc func(T) K, valueFunc func(T) V) It2[K, V]
func CollectIt2ToIt[K comparable, V, R any](i It2[K, V], mapper func(K, V) R) It[R]
func ChainAll[V any](its ...It[V]) It[V]

Donate
  • Bitcoin (QR) 1GqdJ63RDPE4eJKujHi166FAyigvHu5R7v
  • Paypal

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindPosition

func FindPosition[T Number](p T) func(T) bool

FindPosition returns a function that checks if a given value x is equal to the specified position p. The returned function takes a single argument of type T and returns true if it matches p.

func Fold

func Fold[T any, R any](i It[T], initial R, accumulator func(R, T) R) R

Fold reduces the iterator to a single value by applying an accumulator function, starting with an initial value and processing from left to right.

func Max

func Max[T Ordered](a, b T) bool

Max returns true if the first argument a is less than the second argument b. It is intended to be used with types that implement the Ordered interface, which allows for comparison operations.

func Min

func Min[T Ordered](a, b T) bool

Min returns true if the first argument a is greater than the second argument b. It is intended to be used with types that implement the Ordered interface, which allows for comparison operations.

func OrderAsc

func OrderAsc[T Ordered](a, b T) int

OrderAsc compares two Ordered values in ascending order. It returns -1 if the first value is greater than the second.

func OrderDesc

func OrderDesc[T Ordered](a, b T) int

OrderDesc compares two Ordered values in descending order. It returns -1 if the first value is less than the second.

func OrderStructAsc

func OrderStructAsc[T OrderedStruct[T]](a, b T) bool

OrderStructAsc compares two OrderedStructs in ascending order. It returns true if the first struct is greater than the second.

func OrderStructDesc

func OrderStructDesc[T OrderedStruct[T]](a, b T) bool

OrderStructDesc compares two OrderedStructs in descending order. It returns true if the first struct is less than the second.

func RFold

func RFold[T any, R any](i It[T], initial R, accumulator func(T, R) R) R

RFold reduces the iterator to a single value by processing elements from right to left. Note: This triggers a full collection of the iterator into memory.

func Sum

func Sum[T Number](a, b T) T

Sum returns the sum of two numbers a and b. It is intended to be used with types that implement the Number interface, which allows for addition operations.

Types

type Entry

type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

Entry is a generic struct that holds a key-value pair.

type It

type It[T any] iter.Seq[T]

It is a wrapper around iter.Seq[T] that provides a fluent API for functional-style iterator transformations.

func ChainAll

func ChainAll[V any](its ...It[V]) It[V]

ChainAll concatenates multiple iterators into a single iterator that yields all elements from the first, then the second, and so on.

func CollectIt2ToIt

func CollectIt2ToIt[K comparable, V, R any](i It2[K, V], mapper func(K, V) R) It[R]

CollectIt2ToIt transforms a two-value iterator (it2) into a single-value iterator by applying a mapper function to each key-value pair.

func Distinct

func Distinct[T comparable](i It[T]) It[T]

Distinct returns an iterator that yields only unique elements from the input iterator. It uses a map to track seen elements, which requires O(N) memory.

func FlatMap

func FlatMap[T any, U any](i It[T], transform func(T) It[U]) It[U]

FlatMap returns an iterator that applies a transform function to each element, where the transform returns another iterator. It then flattens the resulting iterators into a single sequence.

func Flatten

func Flatten[V any](nested It[iter.Seq[V]]) It[V]

Flatten takes an iterator of sequences and returns a single iterator yielding all elements from the nested sequences in order.

func From

func From[T any](args ...T) It[T]

From creates a It from a variadic list of elements of type T.

func FromSlice

func FromSlice[T any](slice []T) It[T]

FromSlice creates a It (iterator) from a slice.

func Map

func Map[T any, U any](i It[T], transform func(T) U) It[U]

Map returns an iterator that applies the transform function to each element of the input iterator and yields the results.

func Zip

func Zip[T, R any](i1 It[T], i2 It[R]) It[struct {
	First  T
	Second R
}]

Zip combines two iterators into a single iterator of structs containing elements from both. It stops as soon as either input iterator is exhausted.

func (It[T]) All

func (it It[T]) All(predicate func(T) bool) bool

All returns true if every element satisfies the predicate. It short-circuits on the first false result.

func (It[T]) Any

func (it It[T]) Any(predicate func(T) bool) bool

Any returns true if at least one element satisfies the predicate. It short-circuits on the first true result.

func (It[T]) AsSeq

func (it It[T]) AsSeq() iter.Seq[T]

AsSeq returns the underlying iter.Seq[T].

func (It[T]) Chain

func (it It[T]) Chain(i2 It[T]) It[T]

Chain appends a second iterator to the current one.

func (It[T]) Collect

func (it It[T]) Collect() []T

Collect consumes the iterator and returns a slice of all elements.

func (It[T]) Compare

func (it It[T]) Compare(cmp func(T, T) bool) nilo.Option[T]

Compare finds the "best" element based on the provided comparison function (e.g., to find Min or Max). Note: Use helper functions like steams.Min, steams.Max

func (It[T]) Count

func (it It[T]) Count() int

Count consumes the iterator and returns the total number of elements. Note: This collects the iterator into memory to determine length.

func (It[T]) Enumerate

func (it It[T]) Enumerate() iter.Seq2[int, T]

Enumerate returns a 2-variable iterator yielding (index, value) pairs.

func (It[T]) Filter

func (it It[T]) Filter(predicate func(T) bool) It[T]

Filter returns an iterator containing only elements that satisfy the predicate.

func (It[T]) FilterMap

func (it It[T]) FilterMap(mapper func(T) nilo.Option[T]) It[T]

FilterMap applies a mapper that returns an Option. Only "Value" options are yielded, effectively filtering and transforming in one step.

func (It[T]) FilterMapToInt

func (it It[T]) FilterMapToInt(mapper func(T) nilo.Option[int]) It[int]

FilterMapToInt is a FilterMap variant that yields a sequence of ints.

func (It[T]) FilterMapToString

func (it It[T]) FilterMapToString(mapper func(T) nilo.Option[string]) It[string]

FilterMapToString is a FilterMap variant that yields a sequence of strings.

func (It[T]) Find

func (it It[T]) Find(predicate func(T) bool) nilo.Option[T]

Find returns the first element that satisfies the predicate.

func (It[T]) First

func (it It[T]) First() nilo.Option[T]

First returns the first element as an Option, or an empty Option if the iterator is empty.

func (It[T]) FlatMap

func (it It[T]) FlatMap(mapper func(T) It[T]) It[T]

FlatMap applies a mapper that returns an iterator for each element, then flattens all resulting iterators into a single sequence.

func (It[T]) FlatMapToInt

func (it It[T]) FlatMapToInt(mapper func(T) It[int]) It[int]

FlatMapToInt is a FlatMap variant resulting in a sequence of ints.

func (It[T]) FlatMapToString

func (it It[T]) FlatMapToString(mapper func(T) It[string]) It[string]

FlatMapToString is a FlatMap variant resulting in a sequence of strings.

func (It[T]) Fold

func (it It[T]) Fold(initValue T, acc func(T, T) T) T

Fold reduces the sequence to a single value using an accumulator, starting with initValue and processing from left to right.

func (It[T]) ForEach

func (it It[T]) ForEach(consumer func(T))

ForEach executes the consumer function for every element in the iterator. This is a terminal operation.

func (It[T]) ForEachIdx

func (it It[T]) ForEachIdx(consumer func(int, T))

ForEachIdx executes the consumer function for every element, providing the current 0-based index. This is a terminal operation.

func (It[T]) Inspect

func (it It[T]) Inspect(inspector func(T)) It[T]

Inspect applies a function to each element without modifying the sequence. Useful for debugging or side effects during iteration.

func (It[T]) Last

func (it It[T]) Last() nilo.Option[T]

Last returns the final element of the iterator. Note: This collects the entire sequence into memory first.

func (It[T]) Map

func (it It[T]) Map(mapper func(T) T) It[T]

Map returns an iterator that applies the mapper function to each element.

func (It[T]) MapToInt

func (it It[T]) MapToInt(mapper func(T) int) It[int]

MapToInt applies a mapper that transforms each element into an int.

func (It[T]) MapToString

func (it It[T]) MapToString(mapper func(T) string) It[string]

MapToString applies a mapper that transforms each element into a string.

func (It[T]) None

func (it It[T]) None(predicate func(T) bool) bool

None returns true if no elements satisfy the predicate.

func (It[T]) Nth

func (it It[T]) Nth(n int) nilo.Option[T]

Nth returns the element at the given index, if it exists.

func (It[T]) Partition

func (it It[T]) Partition(politer func(T) bool) (It[T], It[T])

Partition splits the iterator into two collections: those that satisfy the predicate and those that do not.

func (It[T]) Position

func (it It[T]) Position(predicate func(T) bool) nilo.Option[int]

Position returns the index of the first element satisfying the predicate.

func (It[T]) RFold

func (it It[T]) RFold(initValue T, acc func(T, T) T) T

RFold reduces the sequence to a single value processing from right to left. Note: This collects the entire sequence into memory first.

func (It[T]) RPosition

func (it It[T]) RPosition(predicate func(T) bool) nilo.Option[int]

RPosition returns the index of the last element satisfying the predicate. Note: This collects the entire sequence into memory first.

func (It[T]) Reverse

func (it It[T]) Reverse() It[T]

Reverse returns an iterator that yields elements in reverse order. Note: This collects the entire sequence into memory first.

func (It[T]) Skip

func (it It[T]) Skip(n int) It[T]

Skip returns an iterator that ignores the first n elements.

func (It[T]) SkipWhile

func (it It[T]) SkipWhile(predicate func(T) bool) It[T]

SkipWhile discards elements until the predicate returns false, then yields all remaining elements.

func (It[T]) SortBy

func (it It[T]) SortBy(cmp func(T, T) int) It[T]

SortBy returns an iterator yielding elements in the order defined by the comparison function. Note: This collects and sorts the entire sequence in memory.

func (It[T]) Take

func (it It[T]) Take(n int) It[T]

Take returns an iterator that yields at most the first n elements.

func (It[T]) TakeWhile

func (it It[T]) TakeWhile(predicate func(T) bool) It[T]

TakeWhile yields elements as long as the predicate returns true.

type It2

type It2[K comparable, V any] iter.Seq2[K, V]

It2 is a wrapper around iter.Seq2[K, V] that provides a fluent API for functional-style transformations on key-value sequences.

func CollectItToIt2

func CollectItToIt2[T, K comparable, V any](i It[T], keyFunc func(T) K, valueFunc func(T) V) It2[K, V]

CollectItToIt2 transforms a single-value iterator into a two-value iterator (it2) by applying key and value mapping functions to each element.

func FromMap

func FromMap[K comparable, V any](m map[K]V) It2[K, V]

FromMap creates an It2 iterator from a standard Go map.

func GroupBy

func GroupBy[K comparable, V any](i It[V], classifier func(V) K) It2[K, It[V]]

GroupBy organizes elements of the input iterator into groups based on a classifier function. It returns a sequence of keys and their corresponding iterators.

func GroupByCounting

func GroupByCounting[K comparable, V any](i It[V], classifier func(V) K) It2[K, int]

GroupByCounting counts the occurrences of keys generated by the classifier function and returns an iterator of key-count pairs.

func (It2[K, V]) All

func (it It2[K, V]) All(predicate func(K, V) bool) bool

All returns true if every element in the iterator satisfies the predicate.

func (It2[K, V]) Any

func (it It2[K, V]) Any(predicate func(K, V) bool) bool

Any returns true if at least one element in the iterator satisfies the predicate.

func (It2[K, V]) Collect

func (it It2[K, V]) Collect() map[K]V

Collect consumes the iterator and returns a map of all key-value pairs.

func (It2[K, V]) Compare

func (it It2[K, V]) Compare(cmp func(K, K) bool) nilo.Option[Entry[K, V]]

Compare returns the "best" Entry based on the comparison function. If the iterator is empty, it returns a Nil option.

func (It2[K, V]) Count

func (it It2[K, V]) Count() int

Count consumes the iterator and returns the total number of pairs. Note: This implementation collects the iterator into a map to determine length.

func (It2[K, V]) Filter

func (it It2[K, V]) Filter(predicate func(K, V) bool) It2[K, V]

Filter returns an It2 iterator containing only the pairs that satisfy the predicate.

func (It2[K, V]) ForEach

func (it It2[K, V]) ForEach(consumer func(K, V))

ForEach executes the consumer function for every key-value pair in the sequence. This is a terminal operation.

func (It2[K, V]) Inspect

func (it It2[K, V]) Inspect(consumer func(K, V)) It2[K, V]

Inspect applies a function to each pair without modifying the sequence. Note: In its current implementation, this consumes the iterator immediately.

func (It2[K, V]) Keys

func (it It2[K, V]) Keys() It[K]

Keys returns a single-value iterator for the keys.

func (It2[K, V]) Map

func (it It2[K, V]) Map(mapper func(K, V) (K, V)) It2[K, V]

Map returns an It2 iterator that applies a transformation function to each key-value pair.

func (It2[K, V]) MapToInt

func (it It2[K, V]) MapToInt(mapper func(K, V) (K, int)) It2[K, int]

MapToInt applies a mapper that transforms the value into an int while keeping or transforming the key.

func (It2[K, V]) MapToString

func (it It2[K, V]) MapToString(mapper func(K, V) (K, string)) It2[K, string]

MapToString applies a mapper that transforms the value into a string while keeping or transforming the key.

func (It2[K, V]) None

func (it It2[K, V]) None(predicate func(K, V) bool) bool

None returns true if no elements in the iterator satisfy the predicate.

func (It2[K, V]) SortBy

func (it It2[K, V]) SortBy(cmp func(K, K) bool) It2[K, V]

SortBy returns an iterator that yields pairs sorted based on the keys according to the provided comparison function. Note: This collects the entire sequence into memory before sorting.

func (It2[K, V]) Take

func (it It2[K, V]) Take(n int) It2[K, V]

Limit returns a new iterator that yields at most 'n' elements.

func (It2[K, V]) Values

func (it It2[K, V]) Values() It[V]

Values returns a single-value iterator for the values.

type Number

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64
}

Number is a type constraint that includes all sumable types. It allows for summing various numeric types.

type Ordered

type Ordered interface {
	Number | ~string
}

Ordered is a type constraint that includes all ordered types. It allows for comparison of various numeric types and strings.

type OrderedStruct

type OrderedStruct[T any] interface {
	Compare(other T) int
}

OrderedStruct is an interface for structs that can be compared. It requires the implementation of a Compare method that returns an integer. The method should return a negative value if the receiver is less than the other, zero if they are equal, and a positive value if the receiver is greater.

Directories

Path Synopsis
examples
advanced command
basic command
groupby command
zip command

Jump to

Keyboard shortcuts

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