package stateless
import "github.com/IBM/fp-go/iterator/stateless"
Package stateless defines a stateless (pure) iterator, i.e. one that can be iterated over multiple times without
side effects, it is threadsafe
Code:
Output: Code:
Output:Example (Any)¶
{
// `Any` function that simply returns the boolean identity
anyBool := Any(F.Identity[bool])
fmt.Println(anyBool(FromArray(A.From(true, false, false))))
fmt.Println(anyBool(FromArray(A.From(false, false, false))))
fmt.Println(anyBool(Empty[bool]()))
// Output:
// true
// false
// false
}
true
false
false
Example (Next)¶
{
seq := MakeBy(F.Identity[int])
first := seq()
value := F.Pipe1(
first,
O.Map(Current[int]),
)
fmt.Println(value)
// Output:
// Some[int](0)
}
Some[int](0)
Index ¶
- func Any[U any](pred func(U) bool) func(ma Iterator[U]) bool
- func Ap[V, U any](ma Iterator[U]) func(Iterator[func(U) V]) Iterator[V]
- func ApS[S1, S2, T any]( setter func(T) func(S1) S2, fa Iterator[T], ) func(Iterator[S1]) Iterator[S2]
- func Bind[S1, S2, T any]( setter func(T) func(S1) S2, f func(S1) Iterator[T], ) func(Iterator[S1]) Iterator[S2]
- func BindTo[S1, T any]( setter func(T) S1, ) func(Iterator[T]) Iterator[S1]
- func Chain[U, V any](f func(U) Iterator[V]) func(Iterator[U]) Iterator[V]
- func ChainFirst[U, V any](f func(U) Iterator[V]) func(Iterator[U]) Iterator[U]
- func Compress[U any](sel Iterator[bool]) func(Iterator[U]) Iterator[U]
- func Current[U any](m P.Pair[Iterator[U], U]) U
- func DropWhile[U any](pred func(U) bool) func(Iterator[U]) Iterator[U]
- func Filter[U any](f func(U) bool) func(ma Iterator[U]) Iterator[U]
- func FilterChain[U, V any](f func(U) O.Option[Iterator[V]]) func(ma Iterator[U]) Iterator[V]
- func FilterMap[U, V any](f func(U) O.Option[V]) func(ma Iterator[U]) Iterator[V]
- func First[U any](mu Iterator[U]) O.Option[U]
- func Fold[U any](m M.Monoid[U]) func(Iterator[U]) U
- func FoldMap[U, V any](m M.Monoid[V]) func(func(U) V) func(ma Iterator[U]) V
- func Last[U any](mu Iterator[U]) O.Option[U]
- func Let[S1, S2, T any]( setter func(T) func(S1) S2, f func(S1) T, ) func(Iterator[S1]) Iterator[S2]
- func LetTo[S1, S2, T any]( setter func(T) func(S1) S2, b T, ) func(Iterator[S1]) Iterator[S2]
- func Map[U, V any](f func(U) V) func(ma Iterator[U]) Iterator[V]
- func Monad[A, B any]() monad.Monad[A, B, Iterator[A], Iterator[B], Iterator[func(A) B]]
- func Monoid[U any]() M.Monoid[Iterator[U]]
- func Reduce[U, V any](f func(V, U) V, initial V) func(Iterator[U]) V
- func Scan[FCT ~func(V, U) V, U, V any](f FCT, initial V) func(ma Iterator[U]) Iterator[V]
- func Take[U any](n int) func(ma Iterator[U]) Iterator[U]
- func ToArray[U any](u Iterator[U]) []U
- func Uniq[A any, K comparable](f func(A) K) func(as Iterator[A]) Iterator[A]
- func Zip[A, B any](fb Iterator[B]) func(Iterator[A]) Iterator[P.Pair[A, B]]
- type Iterator
- func Count(start int) Iterator[int]
- func Cycle[U any](ma Iterator[U]) Iterator[U]
- func Do[S any]( empty S, ) Iterator[S]
- func Empty[U any]() Iterator[U]
- func Flatten[U any](ma Iterator[Iterator[U]]) Iterator[U]
- func From[U any](data ...U) Iterator[U]
- func FromArray[U any](as []U) Iterator[U]
- func FromIO[U any](io IO.IO[U]) Iterator[U]
- func FromLazy[U any](l L.Lazy[U]) Iterator[U]
- func FromReflect(val R.Value) Iterator[R.Value]
- func MakeBy[FCT ~func(int) U, U any](f FCT) Iterator[U]
- func MonadAp[V, U any](fab Iterator[func(U) V], ma Iterator[U]) Iterator[V]
- func MonadChain[U, V any](ma Iterator[U], f func(U) Iterator[V]) Iterator[V]
- func MonadChainFirst[U, V any](ma Iterator[U], f func(U) Iterator[V]) Iterator[U]
- func MonadMap[U, V any](ma Iterator[U], f func(U) V) Iterator[V]
- func Next[U any](m P.Pair[Iterator[U], U]) Iterator[U]
- func Of[U any](a U) Iterator[U]
- func Repeat[U any](n int, a U) Iterator[U]
- func Replicate[U any](a U) Iterator[U]
- func StrictUniq[A comparable](as Iterator[A]) Iterator[A]
- func ZipWith[FCT ~func(A, B) C, A, B, C any](fa Iterator[A], fb Iterator[B], f FCT) Iterator[C]
Examples ¶
Functions ¶
func Any ¶
Any returns `true` if any element of the iterable is `true`. If the iterable is empty, return `false` Similar to the [https://docs.python.org/3/library/functions.html#any] function
func Ap ¶
Ap is the applicative functor for iterators
func ApS ¶
func ApS[S1, S2, T any]( setter func(T) func(S1) S2, fa Iterator[T], ) func(Iterator[S1]) Iterator[S2]
ApS attaches a value to a context [S1] to produce a context [S2] by considering the context and the value concurrently
func Bind ¶
func Bind[S1, S2, T any]( setter func(T) func(S1) S2, f func(S1) Iterator[T], ) func(Iterator[S1]) Iterator[S2]
Bind attaches the result of a computation to a context [S1] to produce a context [S2]
func BindTo ¶
BindTo initializes a new state [S1] from a value [T]
func Chain ¶
func ChainFirst ¶
func Compress ¶
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 Current ¶
Current returns the current element in an Iterator `P.Pair`
func DropWhile ¶
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 Filter ¶
Filter filters the content of an iterator
func FilterChain ¶
FilterChain filters and transforms the content of an iterator
func FilterMap ¶
FilterMap filters and transforms the content of an iterator
func First ¶
First returns the first item in an iterator if such an item exists
func Fold ¶
Fold folds the iterator using the provided Monoid.
func FoldMap ¶
FoldMap maps and folds an iterator. Map the iterator passing each value to the iterating function. Then fold the results using the provided Monoid.
Code:
Output:Example¶
{
src := From("a", "b", "c")
fold := FoldMap[string](S.Monoid)(strings.ToUpper)
fmt.Println(fold(src))
// Output: ABC
}
ABC
func Last ¶
Last returns the last item in an iterator if such an item exists Note that the function will consume the Iterator in this call completely, to identify the last element. Do not use this for infinite iterators
func Let ¶
func Let[S1, S2, T any]( setter func(T) func(S1) S2, f func(S1) T, ) func(Iterator[S1]) Iterator[S2]
Let attaches the result of a computation to a context [S1] to produce a context [S2]
func LetTo ¶
LetTo attaches the a value to a context [S1] to produce a context [S2]
func Map ¶
Map transforms an Iterator of type [U] into an Iterator of type [V] via a mapping function
func Monad ¶
Monad returns the monadic operations for an Iterator
func Monoid ¶
Monoid contructs a [M.Monoid] that concatenates two [Iterator]s
func Reduce ¶
Reduce applies a function for each value of the iterator with a floating result
func Scan ¶
Scan takes an Iterator and returns a new Iterator of the same length, where the values of the new Iterator are the result of the application of `f` to the value of the source iterator with the previously accumulated value
func Take ¶
Take limits the number of values in the Iterator to a maximum number
func ToArray ¶
ToArray converts the iterator to an array
func Uniq ¶
func Uniq[A any, K comparable](f func(A) K) func(as Iterator[A]) Iterator[A]
Uniq converts an Iterator of arbitrary items into an Iterator or unique items where uniqueness is determined based on a key extractor function
func Zip ¶
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
Types ¶
type Iterator ¶
Iterator represents a stateless, pure way to iterate over a sequence
func Count ¶
Count creates an Iterator containing a consecutive sequence of integers starting with the provided start value
func Cycle ¶
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 Do ¶
Bind creates an empty context of type [S] to be used with the Bind operation
func Empty ¶
Empty returns the empty iterator
func Flatten ¶
Flatten converts an Iterator of Iterator into a simple Iterator
func From ¶
From constructs an Iterator from a set of variadic arguments
func FromArray ¶
FromArray returns an iterator from multiple elements
func FromIO ¶
FromIO returns an Iterator on top of an IO function
func FromLazy ¶
FromLazy returns an Iterator on top of a lazy function
func FromReflect ¶
FromReflect creates an iterator that can iterate over types that define [R.Index] and [R.Len]
func MakeBy ¶
MakeBy returns an Iterator with an infinite number of elements initialized with `f(i)`
func MonadAp ¶
MonadAp is the applicative functor for iterators
func MonadChain ¶
func MonadChainFirst ¶
func MonadMap ¶
MonadMap transforms an Iterator of type [U] into an Iterator of type [V] via a mapping function
func Next ¶
Next returns the Iterator for the next element in an iterator `P.Pair`
func Of ¶
Of returns an iterator with one single element
func Repeat ¶
Repeat creates an Iterator containing a value repeated the specified number of times. Alias of Replicate
func Replicate ¶
Replicate creates an Iterator containing a value repeated an infinite number of times.
func StrictUniq ¶
func StrictUniq[A comparable](as Iterator[A]) Iterator[A]
StrictUniq converts an Iterator of arbitrary items into an Iterator or unique items where uniqueness is determined by the built-in uniqueness constraint
func ZipWith ¶
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.
Source Files ¶
any.go bind.go compress.go cycle.go doc.go dropwhile.go first.go io.go iterator.go last.go monad.go monoid.go reflect.go scan.go take.go uniq.go zip.go
Directories ¶
Path | Synopsis |
---|---|
iterator/stateless/generic |
- Version
- v1.0.151 (latest)
- Published
- Nov 23, 2024
- Platform
- linux/amd64
- Imports
- 8 packages
- Last checked
- 4 months ago –
Tools for package owners.