Documentation
¶
Index ¶
- Variables
- type List
- func (l List[T]) Append(v ...T) List[T]
- func (l List[T]) At(i int) Option[T]
- func (l List[T]) Clone() List[T]
- func (l List[T]) Delete(i, j int) List[T]
- func (l List[T]) Filter(fn func(T) bool) List[T]
- func (l List[T]) Find(fn func(T) bool) Option[T]
- func (l List[T]) First() Option[T]
- func (l List[T]) Insert(i int, v ...T) List[T]
- func (l List[T]) Last() Option[T]
- func (l List[T]) Map[U any](fn func(T) U) List[U]
- func (l List[T]) Max(less func(a, b T) bool) Option[T]
- func (l List[T]) Min(less func(a, b T) bool) Option[T]
- func (l List[T]) Reduce[U any](fn func(U, T) U, init U) U
- func (l List[T]) Reverse() List[T]
- func (l List[T]) Sort(less func(a, b T) bool) List[T]
- func (l List[T]) Unpack() []T
- type Option
- func (o Option[T]) Alt(fn func() Option[T]) Option[T]
- func (o Option[T]) Filter(fn func(T) bool) Option[T]
- func (o Option[T]) Fold[O any](somefn func(T) O, nonefn func() O) O
- func (o Option[T]) Get() T
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsSome() bool
- func (o Option[T]) Map[O any](fn func(T) O) Option[O]
- func (o Option[T]) MapFlat[O any](fn func(T) Option[O]) Option[O]
- func (o Option[T]) Match(somefn func(T), nonefn func())
- func (o Option[T]) Or(fallback T) T
- func (o Option[T]) OrElse(fn func() T) T
- func (o Option[T]) Unpack() (v T, ok bool)
- type PanicError
- type Result
- func (r Result[T]) Catch(fn func(error) Result[T]) Result[T]
- func (r Result[T]) Fold[O any](okfn func(T) O, errfn func(error) O) O
- func (r Result[T]) Get() T
- func (r Result[T]) GetErr() error
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) Map[O any](fn func(T) O) Result[O]
- func (r Result[T]) MapErr(fn func(error) error) Result[T]
- func (r Result[T]) MapFlat[O any](fn func(T) Result[O]) Result[O]
- func (r Result[T]) Match(okfn func(T), errfn func(error))
- func (r Result[T]) Or(fallback T) T
- func (r Result[T]) OrElse(fn func(error) T) T
- func (r Result[T]) Unpack() (T, error)
Constants ¶
This section is empty.
Variables ¶
var ErrIsOk = errors.New("Result: no error")
Simple error for Result
var ErrNone = errors.New("Option: no value")
Simple error for Option
Functions ¶
This section is empty.
Types ¶
type List ¶
type List[T any] []T
List holds an ordered sequence of elements.
func New ¶
New creates an empty List with the specified capacity.
Returns List[T] with length 0 and the given capacity.
func PackList ¶
PackList wraps an existing slice as a List.
Creates List[T] sharing the backing array of s. Mutations to the returned List affect s and vice versa. Use Clone for an independent copy.
func (List[T]) Append ¶
Append adds elements to the end of the List.
Returns a new List[T] with v appended.
func (List[T]) At ¶
At returns an Option containing the element at index i.
In bounds: Creates Some(l[i]). Out of bounds: Creates None.
func (List[T]) Clone ¶
Clone returns a copy of the List.
Creates a new List with a fresh backing array.
func (List[T]) Delete ¶
Delete removes elements from index i to j.
Returns a new List[T] with elements l[i:j] removed.
func (List[T]) Filter ¶
Filter returns a new List containing only elements matching fn.
fn returns true: Element is included. fn returns false: Element is excluded.
func (List[T]) Find ¶
Find returns an Option containing the first element matching fn.
Match found: Creates Some(l[i]). No match: Creates None.
func (List[T]) First ¶
First returns an Option containing the first element.
Non-empty: Creates Some(l[0]). Empty: Creates None.
func (List[T]) Insert ¶
Insert inserts values at index i.
Returns a new List[T] with v inserted at position i.
func (List[T]) Last ¶
Last returns an Option containing the last element.
Non-empty: Creates Some(l[len(l)-1]). Empty: Creates None.
func (List[T]) Map ¶
Map applies fn to each element, returning a new List of the results.
Returns List[U] where each element is fn(original).
func (List[T]) Max ¶
Max returns the maximum element according to less.
Non-empty: Returns Some(max). Empty: Returns None.
func (List[T]) Min ¶
Min returns the minimum element according to less.
Non-empty: Returns Some(min). Empty: Returns None.
func (List[T]) Reduce ¶
Reduce folds all elements into a single value using fn.
Returns the accumulated result starting from init.
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option holds either a value or represents a null value.
func CollectOption ¶
CollectOption converts a slice of Options into an Option of slice.
All Some: Creates Some containing all values. Any None: Creates None.
func None ¶
None creates an Option with no value.
Creates Option[T] with no value. Type must be specified.
func PackOption ¶
PackOption converts a Go (v, ok) return pair into an Option. The inverse of Unpack.
ok == true: Creates Option[T] with value. ok == false: Creates Option[T] with no value.
func Some ¶
Some wraps a value in an Option.
Creates Option[T] with value. Type is inferred from the argument.
func (Option[T]) Alt ¶
Alt replaces none with result of fn.
targets None. Some: propagated forward. None: returns fn().
func (Option[T]) Filter ¶
Filter keeps the value only if fn returns true.
targets Some. Some and fn(val) is true: returns the original Option. Some and fn(val) is false: returns None. None: propagated forward.
func (Option[T]) Fold ¶
Fold collapses the Option into a single value.
Some: somefn(val). None: nonefn().
func (Option[T]) Get ¶
func (o Option[T]) Get() T
Get returns the contained value.
targets Some. Some: returns the contained value. None: panics with ErrNone.
func (Option[T]) IsNone ¶
IsNone reports whether the Option is missing a value.
targets None. Some: returns false. None: returns true.
func (Option[T]) IsSome ¶
IsSome reports whether the Option holds a value.
targets Some. Some: returns true. None: returns false.
func (Option[T]) Map ¶
Map applies fn to the contained value, wrapping the result in Some.
targets Some. Some: Some(fn(val)). None: propagated forward.
func (Option[T]) MapFlat ¶
MapFlat applies fn to the contained value and returns the resulting Option.
targets Some. Some: returns fn(val). None: propagated forward.
func (Option[T]) Match ¶
func (o Option[T]) Match(somefn func(T), nonefn func())
Match dispatches to one of two side-effect functions.
Some: somefn(val). None: nonefn().
func (Option[T]) Or ¶
func (o Option[T]) Or(fallback T) T
Or returns the contained value.
targets Some. Some: returns the contained value. None: returns fallback.
type PanicError ¶
PanicError wraps a value recovered from panic.
func (*PanicError) Error ¶
func (e *PanicError) Error() string
Error returns the message of the caught panic.
Value is error: returns its Error string. Otherwise: returns fmt.Sprintf("%v", Value).
func (*PanicError) Unwrap ¶
func (e *PanicError) Unwrap() error
Unwrap returns the underlying error.
Value is error: returns Value. Otherwise: returns nil.
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result holds either a value of type T or an error.
func CollectResult ¶
CollectResult converts a slice of Results into a Result of slice.
All Ok: Creates Ok containing all values. Any Err: Creates Err of the first error.
func Ok ¶
Ok wraps a value in a Result.
Creates Result[T] with value. Type is inferred from the argument.
func PackResult ¶
PackResult converts a Go (v, error) return pair into a Result. The inverse of Unpack.
err == nil: Creates Result[T] with value. err != nil: Creates Result[T] with error.
func Try ¶
Try calls fn and wraps the result in a Result.
fn returns: Creates Ok(val). fn panics: Creates Err(*PanicError) with the panic value and stack trace.
func (Result[T]) Catch ¶
Catch applies fn to the contained error to produce an alternative Result. Can recover from the error.
targets Err. Ok: no-op. Err: returns fn(err).
func (Result[T]) Fold ¶
Fold collapses the Result into a single value.
Ok: okfn(val). Err: errfn(err).
func (Result[T]) Get ¶
func (r Result[T]) Get() T
Get returns the contained value.
targets Ok. Ok: returns the contained value. Err: panics with stored error.
func (Result[T]) GetErr ¶
GetErr returns the contained error.
targets Err. Ok: panics with ErrIsOk. Err: returns the contained error.
func (Result[T]) IsErr ¶
IsErr reports whether the Result contains an error.
targets Err. Ok: returns false. Err: returns true.
func (Result[T]) IsOk ¶
IsOk reports whether the Result contains a value.
targets Ok. Ok: returns true. Err: returns false.
func (Result[T]) Map ¶
Map applies fn to the contained value, wrapping the result in Ok.
targets Ok. Ok: Ok(fn(val)). Err: propagated forward.
func (Result[T]) MapErr ¶
MapErr replaces the contained error.
targets Err. Ok: no-op. Err: Err(fn(err)).
func (Result[T]) MapFlat ¶
MapFlat applies fn to the contained value and returns the resulting Result.
targets Ok. Ok: returns fn(val). Err: propagated forward.
func (Result[T]) Match ¶
Match dispatches to one of two side-effect functions.
Ok: okfn(val). Err: errfn(err).
func (Result[T]) Or ¶
func (r Result[T]) Or(fallback T) T
Or returns the contained value.
targets Ok. Ok: returns the contained value. Err: returns fallback.