Documentation
¶
Index ¶
- func All[A any](s []A, pred Pred[A]) bool
- func Any[A any](s []A, pred Pred[A]) bool
- func Comp[A, B, C any](f func(A) B, g func(B) C) func(A) C
- func Const[B, A any](a A) func(B) A
- func Elem[A comparable](a A, s []A) bool
- func ElimEither[L, R, O any](e Either[L, R], f func(L) O, g func(R) O) O
- func ElimOption[A, B any](o Option[A], b func() B, f func(A) B) B
- func Eq[A comparable](x A) func(A) bool
- func Filter[A any](s []A, pred Pred[A]) []A
- func FilterMap[A, B any](as []A, f func(A) Option[B]) []B
- func FlatMapOption[A, B any](f func(A) Option[B]) func(Option[A]) Option[B]
- func Flatten[A any](s [][]A) []A
- func Foldl[A, B any](seed B, s []A, f func(B, A) B) B
- func Foldr[A, B any](seed B, s []A, f func(A, B) B) B
- func ForEachConc[A, B any](as []A, f func(A) B) []B
- func HasDuplicates[A comparable](items []A) bool
- func Iden[A any](a A) A
- func Len[A any](items []A) uint
- func LiftA2Option[A, B, C any](f func(A, B) C) func(Option[A], Option[B]) Option[C]
- func LiftA2Result[A, B, C any](f func(A, B) C) func(Result[A], Result[B]) Result[C]
- func Map[A, B any](s []A, f func(A) B) []B
- func MapFirst[A, B, C any](f func(A) B) func(T2[A, C]) T2[B, C]
- func MapLeft[L, R, O any](f func(L) O) func(Either[L, R]) Either[O, R]
- func MapOk[A, B any](f func(A) B) func(Result[A]) Result[B]
- func MapOption[A, B any](f func(A) B) func(Option[A]) Option[B]
- func MapOptionZ[A, B any](o Option[A], f func(A) B) B
- func MapRight[L, R, O any](f func(R) O) func(Either[L, R]) Either[L, O]
- func MapSecond[A, B, C any](f func(A) B) func(T2[C, A]) T2[C, B]
- func Neq[A comparable](x A) func(A) bool
- func NewSubMap[K comparable, V any](m map[K]V, keys []K) (map[K]V, error)
- func NewSubMapIntersect[K comparable, V any](m map[K]V, keys []K) map[K]V
- func Pair[A, B, C any](f func(A) B, g func(A) C) func(A) T2[B, C]
- func Replicate[A any](n uint, val A) []A
- func SetDiff[T comparable](a, b []T) []T
- func SliceToMap[A any, K comparable, V any](s []A, keyFunc func(A) K, valueFunc func(A) V) map[K]V
- func Span[A any](s []A, pred Pred[A]) ([]A, []A)
- func SplitAt[A any](n uint, s []A) ([]A, []A)
- func Sum[B Number](items []B) B
- func TrimNones[A any](as []Option[A]) []A
- func ZipWith[A, B, C any](a []A, b []B, f func(A, B) C) []C
- type Either
- func (e Either[L, R]) IsLeft() bool
- func (e Either[L, R]) IsRight() bool
- func (e Either[L, R]) LeftToSome() Option[L]
- func (e Either[L, R]) RightToSome() Option[R]
- func (e Either[L, R]) Swap() Either[R, L]
- func (e Either[L, R]) UnwrapLeftOr(l L) L
- func (e Either[L, R]) UnwrapRightOr(r R) R
- func (e Either[L, R]) WhenLeft(f func(L))
- func (e Either[L, R]) WhenRight(f func(R))
- type Number
- type Option
- func CollectOptions[A any](options []Option[A]) Option[[]A]
- func Find[A any](s []A, pred Pred[A]) Option[A]
- func FindIdx[A any](s []A, pred Pred[A]) Option[T2[int, A]]
- func FlattenOption[A any](oo Option[Option[A]]) Option[A]
- func Head[A any](items []A) Option[A]
- func Init[A any](items []A) Option[[]A]
- func Last[A any](items []A) Option[A]
- func None[A any]() Option[A]
- func OptionFromPtr[A any](a *A) Option[A]
- func Some[A any](a A) Option[A]
- func Tail[A any](items []A) Option[[]A]
- func TransposeResOpt[A any](r Result[Option[A]]) Option[Result[A]]
- func TraverseOption[A, B any](as []A, f func(A) Option[B]) Option[[]B]
- func Uncons[A any](items []A) Option[T2[A, []A]]
- func Unsnoc[A any](items []A) Option[T2[[]A, A]]
- func (o Option[A]) Alt(o2 Option[A]) Option[A]
- func (o Option[A]) IsNone() bool
- func (o Option[A]) IsSome() bool
- func (o Option[A]) SomeToOk(err error) Result[A]
- func (o Option[A]) SomeToOkf(errString string, args ...interface{}) Result[A]
- func (o Option[A]) UnsafeFromSome() A
- func (o Option[A]) UnwrapOr(a A) A
- func (o Option[A]) UnwrapOrErr(err error) (A, error)
- func (o Option[A]) UnwrapOrFail(t *testing.T) A
- func (o Option[A]) UnwrapOrFunc(f func() A) A
- func (o Option[A]) UnwrapOrFuncErr(f func() (A, error)) (A, error)
- func (o Option[A]) WhenSome(f func(A))
- type Pred
- type Result
- func AndThen[A, B any](r Result[A], f func(A) Result[B]) Result[B]
- func CollectResults[A any](results []Result[A]) Result[[]A]
- func Err[T any](err error) Result[T]
- func Errf[T any](errString string, args ...any) Result[T]
- func FlatMapResult[A, B any](r Result[A], f func(A) Result[B]) Result[B]
- func FlattenResult[A any](r Result[Result[A]]) Result[A]
- func NewResult[T any](val T, err error) Result[T]
- func Ok[T any](val T) Result[T]
- func TransposeOptRes[A any](o Option[Result[A]]) Result[Option[A]]
- func TraverseResult[A, B any](as []A, f func(A) Result[B]) Result[[]B]
- func (r Result[T]) AndThen(f func(T) Result[T]) Result[T]
- func (r Result[T]) Err() error
- func (r Result[T]) FlatMap(f func(T) Result[T]) Result[T]
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) MapErr(f func(error) error) Result[T]
- func (r Result[T]) MapOk(f func(T) T) Result[T]
- func (r Result[T]) OkToSome() Option[T]
- func (r Result[T]) OrElse(f func(error) Result[T]) Result[T]
- func (r Result[A]) Sink(f func(A) error) error
- func (r Result[T]) Unpack() (T, error)
- func (r Result[T]) UnwrapOr(defaultValue T) T
- func (r Result[T]) UnwrapOrElse(f func(error) T) T
- func (r Result[T]) UnwrapOrFail(t *testing.T) T
- func (r Result[T]) WhenErr(f func(error))
- func (r Result[T]) WhenOk(f func(T))
- type Set
- func (s Set[T]) Add(e T)
- func (s Set[T]) Contains(e T) bool
- func (s Set[T]) Diff(other Set[T]) Set[T]
- func (s Set[T]) Equal(other Set[T]) bool
- func (s Set[T]) Intersect(other Set[T]) Set[T]
- func (s Set[T]) Remove(e T)
- func (s Set[T]) Subset(other Set[T]) bool
- func (s Set[T]) ToSlice() []T
- func (s Set[T]) Union(other Set[T]) Set[T]
- type T2
- type Unit
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true when the supplied predicate evaluates to true for all of the values in the slice.
func Any ¶
Any returns true when the supplied predicate evaluates to true for any of the values in the slice.
func Comp ¶
func Comp[A, B, C any](f func(A) B, g func(B) C) func(A) C
Comp is left to right function composition. Comp(f, g)(x) == g(f(x)). This can make it easier to create on the fly closures that we may use as arguments to other functions defined in this package (or otherwise).
func Const ¶
func Const[B, A any](a A) func(B) A
Const is a function that accepts an argument and returns a function that always returns that value irrespective of the returned function's argument. This is also quite useful in conjunction with higher order functions.
func Elem ¶
func Elem[A comparable](a A, s []A) bool
Elem returns true if the element in the argument is found in the slice
func ElimEither ¶
ElimEither is the universal Either eliminator. It can be used to safely handle all possible values inside the Either by supplying two continuations, one for each side of the Either.
func ElimOption ¶
ElimOption is the universal Option eliminator. It can be used to safely handle all possible values inside the Option by supplying two continuations.
ElimOption : (Option[A], () -> B, A -> B) -> B.
func Eq ¶
func Eq[A comparable](x A) func(A) bool
Eq is a curried function that returns true if its eventual two arguments are equal.
func Filter ¶
Filter creates a new slice of values where all the members of the returned slice pass the predicate that is supplied in the argument.
func FilterMap ¶
FilterMap takes a function argument that optionally produces a value and returns a slice of the 'Some' return values.
func FlatMapOption ¶
FlatMapOption transforms a function A -> Option[B] into one that accepts an Option[A] as an argument.
FlatMapOption : (A -> Option[B]) -> Option[A] -> Option[B].
func Flatten ¶
func Flatten[A any](s [][]A) []A
Flatten takes a slice of slices and returns a concatenation of those slices.
func Foldl ¶
func Foldl[A, B any](seed B, s []A, f func(B, A) B) B
Foldl iterates through all members of the slice left to right and reduces them pairwise with an accumulator value that is seeded with the seed value in the argument.
func Foldr ¶
func Foldr[A, B any](seed B, s []A, f func(A, B) B) B
Foldr, is exactly like Foldl except that it iterates over the slice from right to left.
func ForEachConc ¶
func ForEachConc[A, B any](as []A, f func(A) B) []B
ForEachConc maps the argument function over the slice, spawning a new goroutine for each element in the slice and then awaits all results before returning them.
func HasDuplicates ¶
func HasDuplicates[A comparable](items []A) bool
HasDuplicates checks if the given slice contains any duplicate elements. It returns false if there are no duplicates in the slice (i.e., all elements are unique), otherwise returns false.
func Iden ¶
func Iden[A any](a A) A
Iden is the left and right identity of Comp. It is a function that simply returns its argument. The utility of this function is only apparent in conjunction with other functions in this package.
func Len ¶
Len is the len function that is defined in a way that makes it usable in higher-order contexts.
func LiftA2Option ¶
LiftA2Option transforms a pure function (A, B) -> C into one that will operate in an Option context. For the returned function, if either of its arguments are None, then the result will be None.
LiftA2Option : ((A, B) -> C) -> (Option[A], Option[B]) -> Option[C].
func LiftA2Result ¶
LiftA2Result lifts a two-argument function to a function that can operate over results of its arguments.
func Map ¶
func Map[A, B any](s []A, f func(A) B) []B
Map applies the function argument to all members of the slice and returns a slice of those return values.
func MapFirst ¶
MapFirst lifts the argument function into one that applies to the first element of a 2-tuple.
func MapOk ¶
MapOk applies a non-endomorphic function to the success value if it exists and returns a Result of the new type.
func MapOption ¶
MapOption transforms a pure function A -> B into one that will operate inside the Option context.
MapOption : (A -> B) -> Option[A] -> Option[B].
func MapOptionZ ¶
MapOptionZ transforms a pure function A -> B into one that will operate inside the Option context. Unlike MapOption, this function will return the default/zero argument of the return type if the Option is empty.
func MapSecond ¶
MapSecond lifts the argument function into one that applies to the second element of a 2-tuple.
func Neq ¶
func Neq[A comparable](x A) func(A) bool
Neq is a curried function that returns true if its eventual two arguments are not equal.
func NewSubMap ¶
func NewSubMap[K comparable, V any](m map[K]V, keys []K) (map[K]V, error)
NewSubMap creates a sub-map from a given map using specified keys. It errors if any of the keys is not found in the map.
func NewSubMapIntersect ¶
func NewSubMapIntersect[K comparable, V any](m map[K]V, keys []K) map[K]V
NewSubMapIntersect returns a sub-map of `m` containing only the keys found in both `m` and the `keys` slice.
func Pair ¶
Pair takes two functions that share the same argument type and runs them both and produces a 2-tuple of the results.
func SetDiff ¶
func SetDiff[T comparable](a, b []T) []T
SetDiff returns all the items that are in the first set but not in the second.
func SliceToMap ¶
func SliceToMap[A any, K comparable, V any](s []A, keyFunc func(A) K, valueFunc func(A) V) map[K]V
SliceToMap converts a slice to a map using the provided key and value functions.
func Span ¶
Span, applied to a predicate and a slice, returns two slices where the first element is the longest prefix (possibly empty) of slice elements that satisfy the predicate and second element is the remainder of the slice.
func SplitAt ¶
SplitAt(n, s) returns a tuple where first element is s prefix of length n and second element is the remainder of the list.
Types ¶
type Either ¶
Either is a type that can be either left or right.
func SomeToLeft ¶
SomeToLeft can be used to convert an Option value into an Either, by providing the Right value that should be used if the Option value is None.
func SomeToRight ¶
SomeToRight can be used to convert an Option value into an Either, by providing the Left value that should be used if the Option value is None.
func (Either[L, R]) LeftToSome ¶
LeftToSome converts a Left value to an Option, returning None if the inner Either value is a Right value.
func (Either[L, R]) RightToSome ¶
RightToSome converts a Right value to an Option, returning None if the inner Either value is a Left value.
func (Either[L, R]) Swap ¶
Swap reverses the type argument order. This can be useful as an adapter between APIs.
func (Either[L, R]) UnwrapLeftOr ¶
func (e Either[L, R]) UnwrapLeftOr(l L) L
UnwrapLeftOr will extract the Left value from the Either if it is present returning the supplied default if it is not.
func (Either[L, R]) UnwrapRightOr ¶
func (e Either[L, R]) UnwrapRightOr(r R) R
UnwrapRightOr will extract the Right value from the Either if it is present returning the supplied default if it is not.
type Number ¶
type Number interface {
constraints.Integer | constraints.Float | constraints.Complex
}
Number is a type constraint for all numeric types in Go (integers, float and complex numbers)
type Option ¶
type Option[A any] struct { // contains filtered or unexported fields }
Option represents a value which may or may not be there. This is very often preferable to nil-able pointers.
func CollectOptions ¶
CollectOptions collects a list of Options into a single Option of the list of Some values in it. If there are any Nones present it will return None.
func Find ¶
Find returns the first value that passes the supplied predicate, or None if the value wasn't found.
func FindIdx ¶
FindIdx returns the first value that passes the supplied predicate along with its index in the slice. If no satisfactory value is found, None is returned.
func FlattenOption ¶
FlattenOption joins multiple layers of Options together such that if any of the layers is None, then the joined value is None. Otherwise the innermost Some value is returned.
FlattenOption : Option[Option[A]] -> Option[A].
func Init ¶
Init returns the slice without the last element, assuming the slice is not empty. Note this makes a copy of the slice.
func OptionFromPtr ¶
OptionFromPtr constructs an option from a pointer.
OptionFromPtr : *A -> Option[A].
func Tail ¶
Tail returns the slice without the first element, assuming the slice is not empty. Note this makes a copy of the slice.
func TransposeResOpt ¶
TransposeResOpt transposes the Result[Option[A]] into a Option[Result[A]]. This has the effect of leaving an A value alone while inverting the Result and Option layers. If there is no internal A value, it will convert the non-success value to the proper one in the transposition.
func TraverseOption ¶
TraverseOption traverses a slice of A values, applying the provided function to each, collecting the results into an Option of a slice of B values. If any of the results are None, the entire result is None.
func (Option[A]) Alt ¶
Alt chooses the left Option if it is full, otherwise it chooses the right option. This can be useful in a long chain if you want to choose between many different ways of producing the needed value.
Alt : Option[A] -> Option[A] -> Option[A].
func (Option[A]) IsSome ¶
IsSome returns true if the Option contains a value
IsSome : Option[A] -> bool.
func (Option[A]) SomeToOk ¶
SomeToOk allows you to convert an Option value to a Result with your own error. If the Option contained a Some, then the supplied error is ignored and Some is converted to Ok.
func (Option[A]) SomeToOkf ¶
SomeToOkf allows you to convert an Option value to a Result with your own error message. If the Option contains a Some, then the supplied message is ignored and Some is converted to Ok.
func (Option[A]) UnsafeFromSome ¶
func (o Option[A]) UnsafeFromSome() A
UnsafeFromSome can be used to extract the internal value. This will panic if the value is None() though.
func (Option[A]) UnwrapOr ¶
func (o Option[A]) UnwrapOr(a A) A
UnwrapOr is used to extract a value from an option, and we supply the default value in the case when the Option is empty.
UnwrapOr : (Option[A], A) -> A.
func (Option[A]) UnwrapOrErr ¶
UnwrapOrErr is used to extract a value from an option, if the option is empty, then the specified error is returned directly.
func (Option[A]) UnwrapOrFail ¶
UnwrapOrFail is used to extract a value from an option within a test context. If the option is None, then the test fails.
func (Option[A]) UnwrapOrFunc ¶
func (o Option[A]) UnwrapOrFunc(f func() A) A
UnwrapOrFunc is used to extract a value from an option, and we supply a thunk to be evaluated in the case when the Option is empty.
func (Option[A]) UnwrapOrFuncErr ¶
UnwrapOrFuncErr is used to extract a value from an option, and we supply a thunk to be evaluated in the case when the Option is empty.
func (Option[A]) WhenSome ¶
func (o Option[A]) WhenSome(f func(A))
WhenSome is used to conditionally perform a side-effecting function that accepts a value of the type that parameterizes the option. If this function performs no side effects, WhenSome is useless.
WhenSome : (Option[A], A -> ()) -> ().
type Pred ¶
Pred[A] is a type alias for a predicate operating over type A.
type Result ¶
Result represents a value that can either be a success (T) or an error.
func AndThen ¶
AndThen is an alias for FlatMap. This along with OrElse can be used to Railway Oriented Programming (ROP).
func CollectResults ¶
CollectResults collects a list of Results into a single Result of the list of Ok values in it. If there are any errors present it will return the first error encountered.
func FlatMapResult ¶
FlatMapResult applies a function that returns a Result[B] to the success value if it exists.
func FlattenResult ¶
FlattenResult takes a nested Result and joins the two functor layers into one.
func TransposeOptRes ¶
TransposeOptRes transposes the Option[Result[A]] into a Result[Option[A]]. This has the effect of leaving an A value alone while inverting the Option and Result layers. If there is no internal A value, it will convert the non-success value to the proper one in the transposition.
func TraverseResult ¶
TraverseResult traverses a slice of A values, applying the provided function to each, collecting the results into a Result of a slice of B values. If any of the results are Err, the entire result is the first error encountered.
func (Result[T]) AndThen ¶
AndThen is an alias for FlatMap. This along with OrElse can be used to Railway Oriented Programming (ROP) by chaining successive computational operations from a single result type.
func (Result[T]) FlatMap ¶
FlatMap applies a kleisli endomorphic function that returns a Result to the success value if it exists.
func (Result[T]) OrElse ¶
OrElse returns the original Result if it is a success, otherwise it returns the provided alternative Result. This along with AndThen can be used to Railway Oriented Programming (ROP).
func (Result[A]) Sink ¶
Sink consumes a Result, either propagating its error or processing its success value with a function that can fail.
func (Result[T]) UnwrapOr ¶
func (r Result[T]) UnwrapOr(defaultValue T) T
UnwrapOr returns the success value or a default value if it's an error.
func (Result[T]) UnwrapOrElse ¶
UnwrapOrElse returns the success value or computes a value from a function if it's an error.
func (Result[T]) UnwrapOrFail ¶
UnwrapOrFail returns the success value or fails the test if it's an error.
type Set ¶
type Set[T comparable] map[T]struct{}
Set is a generic set using type params that supports the following operations: diff, union, intersection, and subset.
func KeySet ¶
func KeySet[K comparable, V any](m map[K]V) Set[K]
KeySet converts a map into a Set containing the keys of the map.
func NewSet ¶
func NewSet[T comparable](elems ...T) Set[T]
NewSet returns a new set with the given elements.
type T2 ¶
type T2[A, B any] struct { // contains filtered or unexported fields }
T2 is the simplest 2-tuple type. It is useful for capturing ad hoc type conjunctions in a single value that can be easily dot-chained.