fx

package module
v2.0.12 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: MIT Imports: 5 Imported by: 5

README

fx: Functional utilities for Go

GoDoc

The fx module contains a number of common utilities for functional programmers, such as map/reduce/filter. The package also contains utility types for working with pairs, value-or-error enumerations, and sets.

Aside from its root package, this module also contains shape-specific packages that are more convenient when dealing with slices, maps, and sets.

Documentation

Overview

The fx package contains a number of common utilities for functional programmers, such as map/reduce/filter. The package also contains utility types for working with pairs, value-or-error enumerations, and sets.

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 All2 added in v2.0.3

func All2[T, U any](it iter.Seq2[T, U], pred func(t T, u U) bool) bool

All2 returns true if pred returns true for every element of the input slice.

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 And2 added in v2.0.3

func And2[T, U any](preds ...func(t T, u U) bool) func(T, U) bool

And2 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 Any2 added in v2.0.3

func Any2[T, U any](it iter.Seq2[T, U], pred func(t T, u U) bool) bool

Any2 returns true if pred returns true for any element of the input slice.

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 Concat2 added in v2.0.3

func Concat2[T, U any](iters ...iter.Seq2[T, U]) iter.Seq2[T, U]

Concat2 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 ConcatMany2 added in v2.0.3

func ConcatMany2[T, U any](iters iter.Seq[iter.Seq2[T, U]]) iter.Seq2[T, U]

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

func Contains added in v2.0.10

func Contains[T comparable](it iter.Seq[T], t T) bool

Contains returns true if the input sequence contains t.

func Empty

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

Empty returns an empty sequence.

func Empty2 added in v2.0.3

func Empty2[T, U any]() iter.Seq2[T, U]

Empty2 returns an empty sequence.

func Enumerate added in v2.0.12

func Enumerate[T any](len func() int, item func(int) T) iter.Seq2[int, T]

Enumerate returns a sequence of (index, value) pairs from the given length and element accessor.

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 FMap2 added in v2.0.3

func FMap2[T, U, V, W any](it iter.Seq2[T, U], fn func(t T, u U) (V, W, bool)) iter.Seq2[V, W]

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

func FMap2Pack added in v2.0.10

func FMap2Pack[T, U, V any](it iter.Seq2[T, U], fn func(t T, u U) (V, bool)) iter.Seq[V]

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

func FMapUnpack added in v2.0.10

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

FMapUnpack 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 Filter2 added in v2.0.3

func Filter2[T, U any](it iter.Seq2[T, U], fn func(t T, u U) bool) iter.Seq2[T, U]

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

func First added in v2.0.10

func First[T any](it iter.Seq[T]) (t T, ok bool)

First returns the first element of it, if any elements exist.

func First2 added in v2.0.10

func First2[T, U any](it iter.Seq2[T, U]) (t T, u U, ok bool)

First2 returns the first element of it, if any elements exist.

func Last added in v2.0.10

func Last[T any](it iter.Seq[T]) (last T, ok bool)

Last returns the last element of it, if any elements exist.

func Last2 added in v2.0.10

func Last2[T, U any](it iter.Seq2[T, U]) (lastT T, lastU U, ok bool)

Last2 returns the last element of it, if any elements exist.

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 Map2 added in v2.0.3

func Map2[T, U, V, W any](it iter.Seq2[T, U], fn func(t T, u U) (V, W)) iter.Seq2[V, W]

Map2 invokes fn on each value in the input slice and returns the results.

func Map2Pack added in v2.0.10

func Map2Pack[T, U, V any](it iter.Seq2[T, U], fn func(t T, u U) V) iter.Seq[V]

Map2Pack invokes fn on each value in the input slice and returns the results.

func MapUnpack added in v2.0.10

func MapUnpack[T, U, V any](it iter.Seq[T], fn func(v T) (U, V)) iter.Seq2[U, V]

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

func MaxRange added in v2.0.10

func MaxRange(max int) iter.Seq[int]

MaxRange returns a sequence of each integer in the range [0, max)

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 Not2 added in v2.0.3

func Not2[T, U any](pred func(t T, u U) bool) func(T, U) bool

Not2 inverts the result of a predicate.

func OfType

func OfType[U, T 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 OfType2 added in v2.0.10

func OfType2[U, K, T any](it iter.Seq2[K, T]) iter.Seq2[K, U]

OfType2 returns a sequence composed of all elements in the input sequence where the second value is 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 Only2 added in v2.0.3

func Only2[T, U any](t T, u U) iter.Seq2[T, U]

Only2 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 Or2 added in v2.0.3

func Or2[T, U any](preds ...func(t T, u U) bool) func(T, U) bool

Or2 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 Reduce2 added in v2.0.3

func Reduce2[T, U, V any](it iter.Seq2[T, U], init V, fn func(acc V, t T, u U) V) V

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

func Skip added in v2.0.10

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

Skip returns an iterator that skips n values from its source.

func Skip2 added in v2.0.10

func Skip2[T, U any](it iter.Seq2[T, U], n int) iter.Seq2[T, U]

Skip2 returns an iterator that skips n values from its source.

func Some added in v2.0.7

func Some[T any](v T) *T

Some returns a pointer to its argument.

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 Take2 added in v2.0.3

func Take2[T, U any](it iter.Seq2[T, U], n int) iter.Seq2[T, U]

Take2 returns an iterator that takes at most n values from the input slice.

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.

func Values added in v2.0.12

func Values[T any](len func() int, item func(int) T) iter.Seq[T]

Values returns a sequence of values from the given length and element accessor.

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 AndThen added in v2.0.5

func AndThen[T, U any](r Result[T], fn func(t T) Result[U]) Result[U]

AndThen invokes fn and returns the result if r is not an error. Otherwise, AndThen returns a new Result that wraps r's error.

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 OrElse added in v2.0.5

func OrElse[T any](r Result[T], fn func() Result[T]) Result[T]

OrElse invokes fn and returns the result if r is an error. Otherwise, OrElse returns r.

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 NewSet added in v2.0.8

func NewSet[T comparable](values ...T) Set[T]

NewSet returns a new set that contains the given elements.

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]) String added in v2.0.6

func (s Set[T]) String() string

String returns a pretty-printed representation of the values in 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
The maps package contains a number of common utilities for functional programmers, such as map/reduce/filter.
The maps package contains a number of common utilities for functional programmers, such as map/reduce/filter.
The sets package contains a number of common utilities for functional programmers, such as map/reduce/filter.
The sets package contains a number of common utilities for functional programmers, such as map/reduce/filter.
The slices package contains a number of common utilities for functional programmers, such as map/reduce/filter.
The slices package contains a number of common utilities for functional programmers, such as map/reduce/filter.
The try package contains a number of common utilities for functional programmers, such as map/reduce/filter.
The try package contains a number of common utilities for functional programmers, such as map/reduce/filter.

Jump to

Keyboard shortcuts

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