lazyiterate

package module
v0.0.0-...-bfdbe1c Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2025 License: MIT Imports: 2 Imported by: 0

README

lazyiterate

A Go package providing generic, lazy if possible iterator utility functions for functional-style programming. For full documentation, see the GoDoc.

Features

  • All / Any: Test if all or any elements (or key-value pairs) satisfy a predicate.
  • Count: Count elements or key-value pairs in a sequence.
  • Find: Find the first element or key-value pair matching a predicate.
  • Filter: Lazily filter elements or key-value pairs.
  • Map: Lazily transform elements or key-value pairs.
  • Reduce: Accumulate elements or key-value pairs into a single value.
  • Reverse: Iterate elements or key-value pairs in reverse order.
  • Skip / Take: Skip or take a fixed number of elements or key-value pairs.
  • Zip: Combine two sequences into pairs.

All functions are generic and work with the iter package's Seq and Seq2 types.

Example Usage

import (
    "slices"

    "github.com/longlodw/lazyiterate"
)

int main() {
    seq := slices.Values([]int{1, 2, 3, 4, 5})
    even := lazyiterate.Filter(seq, func(x int) bool { return x%2 == 0 })
    doubled := lazyiterate.Map(even, func(x int) int { return x * 2 })
    count := lazyiterate.Count(doubled)
}

Requirements

  • Go 1.23+ (for iterator)
  • iter package

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](it iter.Seq[T], pred func(T) bool) bool

Any returns true if any element in the sequence satisfies the predicate.

func All2

func All2[K any, V any](it iter.Seq2[K, V], pred func(K, V) bool) bool

All2 returns true if all key-value pairs in the sequence satisfy the predicate.

func Any

func Any[T any](it iter.Seq[T], pred func(T) bool) bool

Any returns true if any element in the sequence satisfies the predicate.

func Any2

func Any2[K any, V any](it iter.Seq2[K, V], pred func(K, V) bool) bool

Any2 returns true if any key-value pair in the sequence satisfies the predicate.

func Count

func Count[T any](it iter.Seq[T]) int

Count returns the number of elements in the sequence.

func Count2

func Count2[K any, V any](it iter.Seq2[K, V]) int

Count2 returns the number of key-value pairs in the sequence.

func Filter

func Filter[T any](it iter.Seq[T], pred func(T) bool) iter.Seq[T]

Filter returns a new sequence containing only the elements that satisfy the predicate.

func Filter2

func Filter2[K any, V any](it iter.Seq2[K, V], pred func(K, V) bool) iter.Seq2[K, V]

Filter2 returns a new sequence containing only the key-value pairs that satisfy the predicate.

func Find

func Find[T any](it iter.Seq[T], pred func(T) bool) (T, error)

Find returns the first element in the sequence that satisfies the predicate.

func Find2

func Find2[K any, V any](it iter.Seq2[K, V], pred func(K, V) bool) (K, V, error)

Find2 returns the first key-value pair in the sequence that satisfies the predicate.

func Map

func Map[T, R any](it iter.Seq[T], fn func(T) R) iter.Seq[R]

Map returns a new sequence containing the results of applying the function to each element.

func Map2

func Map2[K, V, R any](it iter.Seq2[K, V], fn func(K, V) R) iter.Seq[R]

Map2 returns a new sequence containing the results of applying the function to each key-value pair.

func Reduce

func Reduce[A any, T any](it iter.Seq[T], fn func(A, T) A, init A) A

Reduce applies a function cumulatively to the elements of the sequence, reducing it to a single value.

func Reduce2

func Reduce2[A any, K any, V any](it iter.Seq2[K, V], fn func(A, K, V) A, init A) A

Reduce2 applies a function cumulatively to the key-value pairs of the sequence, reducing it to a single value.

func Reverse

func Reverse[T any](it iter.Seq[T]) iter.Seq[T]

Reverse returns a new sequence with the elements in reverse order.

func Reverse2

func Reverse2[K any, V any](it iter.Seq2[K, V]) iter.Seq2[K, V]

Reverse2 returns a new sequence with the key-value pairs in reverse order.

func Skip

func Skip[T any](it iter.Seq[T], n int) iter.Seq[T]

Skip returns a new sequence that skips the first n elements.

func Skip2

func Skip2[K any, V any](it iter.Seq2[K, V], n int) iter.Seq2[K, V]

Skip2 returns a new sequence that skips the first n key-value pairs.

func Take

func Take[T any](it iter.Seq[T], n int) iter.Seq[T]

Take returns a new sequence that takes the first n elements.

func Take2

func Take2[K any, V any](it iter.Seq2[K, V], n int) iter.Seq2[K, V]

Take2 returns a new sequence that takes the first n key-value pairs.

func Zip

func Zip[T1, T2 any](it1 iter.Seq[T1], it2 iter.Seq[T2]) iter.Seq2[T1, T2]

Zip returns a new sequence that combines elements from two sequences into pairs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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