fx

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 3 Imported by: 0

README

fx: Functional utilities for Go

GoDoc

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(v T) bool) bool

All returns true if pred returns true for every element of the input sequence.

func And

func And[T any](preds ...func(v T) bool) func(T) bool

And combines a list of predicates into a predicate that returns true if every predicate in the list returns true.

func Any

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

Any returns true if pred returns true for any element of the input sequence.

func Concat

func Concat[T any](iters ...iter.Seq[T]) iter.Seq[T]

Concat returns an iterator that returns values from each iterator in sequence.

func ConcatMany

func ConcatMany[T any](iters iter.Seq[iter.Seq[T]]) iter.Seq[T]

ConcatMany returns an iterator that returns values from each iterator in sequence.

func Empty

func Empty[T any]() iter.Seq[T]

Empty returns an empty sequence.

func FMap

func FMap[T, U any](it iter.Seq[T], fn func(v T) (U, bool)) iter.Seq[U]

FMap returns a sequence of values computed by invoking fn on each element of the input sequence and returning only mapped values for with fn returns true.

func Filter

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

Filter returns a sequence of values computed by invoking fn on each element of the input sequence and returning only those elements for with fn returns true.

func Map

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

Map invokes fn on each value in the input sequence and returns the results.

func MinRange

func MinRange(min int) iter.Seq[int]

MinRange returns a sequence of each integer in the range [min, ∞)

func Not

func Not[T any](pred func(v T) bool) func(T) bool

Not inverts the result of a predicate.

func OfType

func OfType[T, U any](it iter.Seq[T]) iter.Seq[U]

OfType returns a sequence composed of all elements in the input sequence that are of type U.

func Only

func Only[T any](v T) iter.Seq[T]

Only returns a sequence that contains the single value v.

func Or

func Or[T any](preds ...func(v T) bool) func(T) bool

Or combines a list of predicates into a predicate that returns true if any predicate in the list returns true.

func PackAll

func PackAll[K, V any](it iter.Seq2[K, V]) iter.Seq[Pair[K, V]]

PackAll transforms a sequence of (K, V) pairs into a sequence of Pair[K, V] values.

func Range

func Range(min, max int) iter.Seq[int]

Range returns a sequence of each integer in the range [min, max).

func Reduce

func Reduce[T, U any](it iter.Seq[T], init U, fn func(acc U, v T) U) U

Reduce calls fn on each element of the input sequence, passing in the current value of the accumulator with each invocation and updating the accumulator to the result of fn after each invocation.

func Take

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

Take returns an iterator that takes at most n values from its source.

func TryFunc

func TryFunc[T, U any](fn func(t T) (U, error)) func(t T) Result[U]

TryFunc wraps a function that returns (U, error) so that it instead returns a Result[U].

func UnpackAll

func UnpackAll[K, V any](it iter.Seq[Pair[K, V]]) iter.Seq2[K, V]

UnpackAll transforms a sequence of Pair[K, V] values into a sequence of (K, V) pairs.

Types

type Pair

type Pair[T, U any] struct {
	Fst T
	Snd U
}

A Pair is a pair of (possibly-differently) typed values.

func Pack

func Pack[T, U any](fst T, snd U) Pair[T, U]

Pack creates a Pair from a pair of values.

func (Pair[T, U]) Unpack

func (p Pair[T, U]) Unpack() (T, U)

Unpack() extracts the contained values from the Pair.

type Result

type Result[T any] struct {
	// contains filtered or unexported fields
}

A Result wraps a (T, error) tuple in a single value.

func Err

func Err[T any](e error) Result[T]

Err creates a Result that contains (_, err).

func OK

func OK[T any](v T) Result[T]

OK creates a Result that contains (T, nil).

func Try

func Try[T any](v T, e error) Result[T]

Try creates a Result that contains (v, err).

func (Result[T]) Unpack

func (r Result[T]) Unpack() (T, error)

Unpack returns the Result's contained (T, error).

type Set

type Set[T comparable] map[T]struct{}

A Set represents a set of comparable values.

func (Set[T]) Add

func (s Set[T]) Add(v T)

Add adds a value to the set.

func (Set[T]) Copy

func (s Set[T]) Copy() Set[T]

Copy returns a shallow copy of S.

func (Set[T]) Has

func (s Set[T]) Has(v T) bool

Has returns true if the set contains the given value.

func (Set[T]) Intersect

func (s Set[T]) Intersect(other Set[T])

Intersect sets the contents of s to the intersection of s and other.

func (Set[T]) Len

func (s Set[T]) Len() int

Len returns the number of elements in the set.

func (Set[T]) Remove

func (s Set[T]) Remove(v T)

Remove removes a value from the set.

func (Set[T]) ToSlice

func (s Set[T]) ToSlice() []T

ToSlice returns a slice that contains the values in the set. The ordering of elements is undefined.

func (Set[T]) Union

func (s Set[T]) Union(other Set[T])

Union sets the contents of s to the union of s and other.

func (Set[T]) Values

func (s Set[T]) Values() iter.Seq[T]

Values returns a sequence of each value in the set. The ordering of elements is undefined.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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