generic

package
v2.0.3 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any[GU ~func() Option[Pair[GU, U]], FCT ~Predicate[U], U any](pred FCT) func(ma GU) bool

Any returns `true` if any element of the iterable is `true`. If the iterable is empty, return `false`

func Ap

func Ap[GUV ~func() Option[Pair[GUV, func(U) V]], GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], U, V any](ma GU) func(fab GUV) GV

func ApS

func ApS[GAS2 ~func() Option[Pair[GAS2, func(A) S2]], GS1 ~func() Option[Pair[GS1, S1]], GS2 ~func() Option[Pair[GS2, S2]], GA ~func() Option[Pair[GA, A]], S1, S2, A any](
	setter func(A) func(S1) S2,
	fa GA,
) func(GS1) GS2

ApS attaches a value to a context [S1] to produce a context [S2] by considering the context and the value concurrently (using Applicative rather than Monad). This allows independent computations to be combined without one depending on the result of the other.

Unlike Bind, which sequences operations, ApS can be used when operations are independent and can conceptually run in parallel. For iterators, this produces the cartesian product.

Example:

type State struct {
    X int
    Y string
}

// These operations are independent and can be combined with ApS
xIter := generic.Of[Iterator[int]](1, 2, 3)
yIter := generic.Of[Iterator[string]]("a", "b")

result := F.Pipe2(
    generic.Do[Iterator[State]](State{}),
    generic.ApS[Iterator[func(int) State], Iterator[State], Iterator[State], Iterator[int], State, State, int](
        func(x int) func(State) State {
            return func(s State) State { s.X = x; return s }
        },
        xIter,
    ),
    generic.ApS[Iterator[func(string) State], Iterator[State], Iterator[State], Iterator[string], State, State, string](
        func(y string) func(State) State {
            return func(s State) State { s.Y = y; return s }
        },
        yIter,
    ),
) // Produces: {1,"a"}, {1,"b"}, {2,"a"}, {2,"b"}, {3,"a"}, {3,"b"}

func Bind

func Bind[GS1 ~func() Option[Pair[GS1, S1]], GS2 ~func() Option[Pair[GS2, S2]], GA ~func() Option[Pair[GA, A]], S1, S2, A any](
	setter func(A) func(S1) S2,
	f func(S1) GA,
) func(GS1) GS2

Bind attaches the result of a computation to a context [S1] to produce a context [S2]. This enables sequential composition where each step can depend on the results of previous steps. For iterators, this produces the cartesian product where later steps can use values from earlier steps.

The setter function takes the result of the computation and returns a function that updates the context from S1 to S2.

Example:

type State struct {
    X int
    Y int
}

result := F.Pipe2(
    generic.Do[Iterator[State]](State{}),
    generic.Bind[Iterator[State], Iterator[State], Iterator[int], State, State, int](
        func(x int) func(State) State {
            return func(s State) State { s.X = x; return s }
        },
        func(s State) Iterator[int] {
            return generic.Of[Iterator[int]](1, 2, 3)
        },
    ),
    generic.Bind[Iterator[State], Iterator[State], Iterator[int], State, State, int](
        func(y int) func(State) State {
            return func(s State) State { s.Y = y; return s }
        },
        func(s State) Iterator[int] {
            // This can access s.X from the previous step
            return generic.Of[Iterator[int]](s.X * 10, s.X * 20)
        },
    ),
) // Produces: {1,10}, {1,20}, {2,20}, {2,40}, {3,30}, {3,60}

func BindTo

func BindTo[GS1 ~func() Option[Pair[GS1, S1]], GA ~func() Option[Pair[GA, A]], S1, A any](
	setter func(A) S1,
) func(GA) GS1

BindTo initializes a new state [S1] from a value [T]

func Chain

func Chain[GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], U, V any](f func(U) GV) func(GU) GV

func ChainFirst

func ChainFirst[GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], U, V any](f func(U) GV) func(GU) GU

func Compress

func Compress[GU ~func() Option[Pair[GU, U]], GB ~func() Option[Pair[GB, bool]], CS ~func() Option[Pair[CS, Pair[U, bool]]], U any](sel GB) func(GU) GU

Compress returns an [Iterator] that filters elements from a data [Iterator] returning only those that have a corresponding element in selector [Iterator] that evaluates to `true`. Stops when either the data or selectors iterator has been exhausted.

func Count

func Count[GU ~func() Option[Pair[GU, int]]](start int) GU

Count creates an [Iterator] containing a consecutive sequence of integers starting with the provided start value

func Current

func Current[GU ~func() Option[Pair[GU, U]], U any](m Pair[GU, U]) U

Current returns the current element in an iterator `Pair`

func Cycle

func Cycle[GU ~func() Option[Pair[GU, U]], U any](ma GU) GU

func Do

func Do[GS ~func() Option[Pair[GS, S]], S any](
	empty S,
) GS

Do creates an empty context of type [S] to be used with the Bind operation. This is the starting point for do-notation style composition.

Example:

type State struct {
    X int
    Y int
}
result := generic.Do[Iterator[State]](State{})

func DropWhile

func DropWhile[GU ~func() Option[Pair[GU, U]], U any](pred Predicate[U]) func(GU) GU

DropWhile creates an [Iterator] that drops elements from the [Iterator] as long as the predicate is true; afterwards, returns every element. Note, the [Iterator] does not produce any output until the predicate first becomes false

func Empty

func Empty[GU ~func() Option[Pair[GU, U]], U any]() GU

Empty returns the empty iterator

func Filter

func Filter[GU ~func() Option[Pair[GU, U]], FCT ~Predicate[U], U any](f FCT) func(ma GU) GU

func FilterChain

func FilterChain[GVV ~func() Option[Pair[GVV, GV]], GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], FCT ~func(U) Option[GV], U, V any](f FCT) func(ma GU) GV

func FilterMap

func FilterMap[GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], FCT ~func(U) Option[V], U, V any](f FCT) func(ma GU) GV

func Flatten

func Flatten[GV ~func() Option[Pair[GV, GU]], GU ~func() Option[Pair[GU, U]], U any](ma GV) GU

func Fold

func Fold[GU ~func() Option[Pair[GU, U]], U any](m M.Monoid[U]) func(ma GU) U

func FoldMap

func FoldMap[GU ~func() Option[Pair[GU, U]], FCT ~func(U) V, U, V any](m M.Monoid[V]) func(FCT) func(ma GU) V

func From

func From[GU ~func() Option[Pair[GU, U]], U any](data ...U) GU

From constructs an array from a set of variadic arguments

func FromArray

func FromArray[GU ~func() Option[Pair[GU, U]], US ~[]U, U any](as US) GU

FromArray returns an iterator from multiple elements

func FromLazy

func FromLazy[GU ~func() Option[Pair[GU, U]], LZ ~func() U, U any](l LZ) GU

FromLazy returns an iterator on top of a lazy function

func FromReflect

func FromReflect[GR ~func() Option[Pair[GR, R.Value]]](val R.Value) GR

func Let

func Let[GS1 ~func() Option[Pair[GS1, S1]], GS2 ~func() Option[Pair[GS2, S2]], S1, S2, A any](
	key func(A) func(S1) S2,
	f func(S1) A,
) func(GS1) GS2

Let attaches the result of a computation to a context [S1] to produce a context [S2]

func LetTo

func LetTo[GS1 ~func() Option[Pair[GS1, S1]], GS2 ~func() Option[Pair[GS2, S2]], S1, S2, B any](
	key func(B) func(S1) S2,
	b B,
) func(GS1) GS2

LetTo attaches the a value to a context [S1] to produce a context [S2]

func MakeBy

func MakeBy[GU ~func() Option[Pair[GU, U]], FCT ~func(int) U, U any](f FCT) GU

MakeBy returns an [Iterator] with an infinite number of elements initialized with `f(i)`

func Map

func Map[GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], FCT ~func(U) V, U, V any](f FCT) func(ma GU) GV

func Monad

func Monad[A, B any, GA ~func() Option[Pair[GA, A]], GB ~func() Option[Pair[GB, B]], GAB ~func() Option[Pair[GAB, func(A) B]]]() monad.Monad[A, B, GA, GB, GAB]

Monad implements the monadic operations for iterators

func MonadAp

func MonadAp[GUV ~func() Option[Pair[GUV, func(U) V]], GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], U, V any](fab GUV, ma GU) GV

func MonadChain

func MonadChain[GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], U, V any](ma GU, f func(U) GV) GV

func MonadChainFirst

func MonadChainFirst[GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], U, V any](ma GU, f func(U) GV) GU

func MonadMap

func MonadMap[GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], U, V any](ma GU, f func(U) V) GV

func Monoid

func Monoid[GU ~func() Option[Pair[GU, U]], U any]() M.Monoid[GU]

func Next

func Next[GU ~func() Option[Pair[GU, U]], U any](m Pair[GU, U]) GU

Next returns the iterator for the next element in an iterator `Pair`

func Of

func Of[GU ~func() Option[Pair[GU, U]], U any](a U) GU

Of returns an iterator with one single element

func Reduce

func Reduce[GU ~func() Option[Pair[GU, U]], U, V any](f func(V, U) V, initial V) func(GU) V

Reduce applies a function for each value of the iterator with a floating result

func Repeat

func Repeat[GU ~func() Option[Pair[GU, U]], U any](n int, a U) GU

Repeat creates an [Iterator] containing a value repeated the specified number of times. Alias of Replicate combined with Take

func Replicate

func Replicate[GU ~func() Option[Pair[GU, U]], U any](a U) GU

Replicate creates an infinite [Iterator] containing a value.

func Scan

func Scan[GV ~func() Option[Pair[GV, V]], GU ~func() Option[Pair[GU, U]], FCT ~func(V, U) V, U, V any](f FCT, initial V) func(ma GU) GV

func StrictUniq

func StrictUniq[AS ~func() Option[Pair[AS, A]], A comparable](as AS) AS

func Take

func Take[GU ~func() Option[Pair[GU, U]], U any](n int) func(ma GU) GU

func ToArray

func ToArray[GU ~func() Option[Pair[GU, U]], US ~[]U, U any](u GU) US

ToArray converts the iterator to an array

func Uniq

func Uniq[AS ~func() Option[Pair[AS, A]], K comparable, A any](f func(A) K) func(as AS) AS

func Zip

func Zip[AS ~func() Option[Pair[AS, A]], BS ~func() Option[Pair[BS, B]], CS ~func() Option[Pair[CS, Pair[A, B]]], A, B any](fb BS) func(AS) CS

Zip takes two iterators and returns an iterators of corresponding pairs. If one input iterators is short, excess elements of the longer iterator are discarded

func ZipWith

func ZipWith[AS ~func() Option[Pair[AS, A]], BS ~func() Option[Pair[BS, B]], CS ~func() Option[Pair[CS, C]], FCT ~func(A, B) C, A, B, C any](fa AS, fb BS, f FCT) CS

ZipWith applies a function to pairs of elements at the same index in two iterators, collecting the results in a new iterator. If one input iterator is short, excess elements of the longer iterator are discarded.

Types

type Lazy

type Lazy[A any] = lazy.Lazy[A]

type Option

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

func First

func First[GU ~func() Option[Pair[GU, U]], U any](mu GU) Option[U]

First returns the first item in an iterator if such an item exists

func Last

func Last[GU ~func() Option[Pair[GU, U]], U any](mu GU) Option[U]

Last returns the last item in an iterator if such an item exists

type Pair

type Pair[L, R any] = pair.Pair[L, R]

type Predicate

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

Jump to

Keyboard shortcuts

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