Documentation ¶
Index ¶
- func Contains[T comparable, TOptional Optional[T]](value T, o TOptional) bool
- func Count(o OptionalCheckOnly) int
- func DefaultValue[T any, TOptional Optional[T]](value T, o TOptional) T
- func DefaultWith[T any, TOptional Optional[T]](defThunk func() T, o TOptional) T
- func Exists[T any, TOptional Optional[T]](predicate func(T) bool, o TOptional) bool
- func Fold[T, State any, TOptional Optional[T]](folder func(State, T) State, s State, o TOptional) State
- func FoldBack[T, State any, TOptional Optional[T]](folder func(T, State) State, o TOptional, s State) State
- func ForAll[T any, TOptional Optional[T]](predicate func(T) bool, o TOptional) bool
- func Get[T any, TOptional Optional[T]](o TOptional) T
- func HandleOption[T any, TOptional Optional[T], F func(T) error, FN func() error](o TOptional, whenSome F, whenNone FN) error
- func HandleOptionIgnoreNone[T any, TOptional Optional[T], F func(T) error](o TOptional, whenSome F) error
- func IsNone[T any, TOptional Optional[T]](o TOptional) bool
- func IsSome[T any, TOptional Optional[T]](o TOptional) bool
- func Iter[T any, TOptional Optional[T]](action func(T), o TOptional)
- func Lift[T any](f func() (T, error)) func() Option[T]
- func Lift1[TInput, T any](f func(TInput) (T, error)) func(TInput) Option[T]
- func Lift2[TInput1, TInput2, T any](f func(TInput1, TInput2) (T, error)) func(TInput1, TInput2) Option[T]
- func OrElse[T any, TOptional Optional[T]](ifNone TOptional, o TOptional) TOptional
- func OrElseWith[T any, TOptional Optional[T]](ifNoneThunk func() TOptional, o TOptional) TOptional
- func ToNullable[T any, TOptional Optional[T]](o TOptional) *T
- func ToSlice[T any, TOptional Optional[T]](o TOptional) []T
- type Option
- func Bind[T, R any](f func(T) Option[R], input Option[T]) Option[R]
- func Filter[T any](predicate func(T) bool, o Option[T]) Option[T]
- func Flatten[T any](oo Option[Option[T]]) Option[T]
- func Map[T, R any](f func(T) R, o Option[T]) Option[R]
- func Map2[T1, T2, R any](f func(T1, T2) R, o1 Option[T1], o2 Option[T2]) Option[R]
- func Map3[T1, T2, T3, R any](f func(T1, T2, T3) R, o1 Option[T1], o2 Option[T2], o3 Option[T3]) Option[R]
- func None[T any]() Option[T]
- func OfNullable[T any](value *T) Option[T]
- func Some[T any](value T) Option[T]
- type Optional
- type OptionalCheckOnly
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable, TOptional Optional[T]](value T, o TOptional) bool
Contains tests whether the value in the Optional value o is equal to value.
func DefaultValue ¶
DefaultValue tests whether o.IsNone() and returns value if true. If o.IsSome(), o.Value() is returned.
func DefaultWith ¶
DefaultWith tests whether o.IsNone() and returns the result of defThunk if true. If o.IsSome(), o.Value() is returned.
func Exists ¶
Exists tests is the value in the Optional o matches the predicate. If o.IsNone(), it will return false.
func Fold ¶
func Fold[T, State any, TOptional Optional[T]](folder func(State, T) State, s State, o TOptional) State
Fold applies the folder function to the value in o. If o is None, it will return s. If o is Some, it will return the result of the function, with s being passed as the first parameter and the value of o being the second parameter.
func FoldBack ¶
func FoldBack[T, State any, TOptional Optional[T]](folder func(T, State) State, o TOptional, s State) State
FoldBack applies the folder function to the value in o. If o is None, it will return s. If o is Some, it will return the result of the function, with the value of o being passed as the first parameter and s being the second parameter. It is the same as Fold, but with the parameters swapped.
func ForAll ¶
ForAll returns true if either o is None or the predicate returns true when applied to the value of o. It returns false only if the predicate returns false.
func HandleOption ¶
func HandleOption[T any, TOptional Optional[T], F func(T) error, FN func() error](o TOptional, whenSome F, whenNone FN) error
HandleOption accepts an Optional value and two functions that return errors. whenSome is the function to use when o.IsSome() is true. whenNone is the function to use when o.IsNone() is true. The error returned by the function that is used will be returned from HandleOption.
Example ¶
package main import ( "fmt" "github.com/flowonyx/functional/option" ) func main() { input := option.Some(1) err := option.HandleOption(input, func(i int) error { fmt.Printf("%d", i) return nil }, func() error { fmt.Println("None") return nil }) if err != nil { panic(err) } }
Output: 1
func HandleOptionIgnoreNone ¶
func HandleOptionIgnoreNone[T any, TOptional Optional[T], F func(T) error](o TOptional, whenSome F) error
HandleOptionIgnoreNone accepts an Optional value and one function that returns an error. whenSome is the function to use when o.IsSome() is true. The error returned by whenSome will be returned from HandleOptionIgnoreNone or nil will be returned when o.IsNone().
Example ¶
package main import ( "fmt" "github.com/flowonyx/functional/option" ) func main() { input := option.None[int]() err := option.HandleOptionIgnoreNone(input, func(i int) error { fmt.Printf("%d", i) return nil }) if err != nil { panic(err) } }
Output:
func Lift ¶
Lift converts the function f that returns a value and an error to a function that returns an Option.
func Lift1 ¶
Lift1 converts the function f that accepts a single input and returns a value and an error to a function that accepts a single input and returns an Option.
func Lift2 ¶
func Lift2[TInput1, TInput2, T any](f func(TInput1, TInput2) (T, error)) func(TInput1, TInput2) Option[T]
Lift2 converts the function f that accepts two inputs and returns a value and an error to a function that accepts two inputs and returns an Option.
func OrElseWith ¶
OrElseWith returns the return value of ifNoneThunk if o is None. Otherwise it returns o.
func ToNullable ¶
ToNullable returns nil if o is None. Otherwise, it returns a pointer the value of o.
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option is a type that allows optional values. It is similar to a Sum type and can either be Some(value) or None.
func Bind ¶
Bind applies f to input if input.IsSome() and otherwise returns None.
Example ¶
package main import ( "fmt" "github.com/flowonyx/functional/option" ) func main() { input := option.Some(2) r := option.Bind(func(i int) option.Option[int] { return option.Some(i * 2) }, input) fmt.Println(r) // Some(4) }
Output:
func Map ¶
Map applies f to the value of o and returns the result as an Option. If o is None, it returns None.
Example ¶
package main import ( "fmt" "strconv" "github.com/flowonyx/functional" "github.com/flowonyx/functional/list" "github.com/flowonyx/functional/option" ) func main() { f := func(i int) string { return "i:" + strconv.Itoa(i) } input := []option.Option[int]{option.None[int](), option.Some(1)} r := list.Map(functional.Curry2To1(option.Map[int, string], f), input) fmt.Println(r) }
Output: [None Some("i:1")]
func Map2 ¶
Map2 applies f to the values in both o1 and o2 as the first and second parameters and returns the result as an Option. If either option is None, it returns None.
func Map3 ¶
func Map3[T1, T2, T3, R any](f func(T1, T2, T3) R, o1 Option[T1], o2 Option[T2], o3 Option[T3]) Option[R]
Map3 applies f to the values in o1, o2, and o3 as the first, second, and third parameters and returns the result as an Option. If any of the options are None, it returns None.
func OfNullable ¶
OfNullable returns None if value is nil. Otherwise it returns Some of the value (after dereferencing the pointer).
type OptionalCheckOnly ¶
OptionalCheckOnly is an interface for Option-like types when Value is not required.