Documentation
¶
Overview ¶
Optional values.
Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not.
Index ¶
- func Compare[T cmp.Ordered](x, y Option[T]) int
- func Equal[T cmp.Ordered](x, y Option[T]) bool
- func IsSorted[T cmp.Ordered](s []Option[T]) bool
- func Less[T cmp.Ordered](x, y Option[T]) bool
- func MapOr[T, U any](o Option[T], def U, f func(T) U) U
- func MapOrElse[T, U any](o Option[T], def func() U, f func(T) U) U
- func Sort[T cmp.Ordered](s []Option[T])
- func Unzip[T, U any](x Option[tuple.Tuple2[T, U]]) (Option[T], Option[U])
- type Option
- func And[T, U any](opta Option[T], optb Option[U]) Option[U]
- func AndThen[T, U any](o Option[T], f func(T) Option[U]) Option[U]
- func Err[T any](r res.Result[T]) Option[error]
- func Flatten[T any](o Option[Option[T]]) Option[T]
- func Map[T, U any](o Option[T], f func(T) U) Option[U]
- func None[T any]() Option[T]
- func Ok[T any](r res.Result[T]) Option[T]
- func Some[T any](value T) Option[T]
- func Wrap[T any](value *T) Option[T]
- func Zip[T, U any](x Option[T], y Option[U]) Option[tuple.Tuple2[T, U]]
- func ZipWith[T, U, R any](x Option[T], y Option[U], f func(T, U) R) Option[R]
- func (o Option[T]) And(optb Option[T]) Option[T]
- func (o Option[T]) AndThen(f func(T) Option[T]) Option[T]
- func (o Option[T]) Expect(msg string) T
- func (o Option[T]) Filter(f func(T) bool) Option[T]
- func (o *Option[T]) GetOrInsert(value T) *T
- func (o *Option[T]) GetOrInsertDefault() *T
- func (o *Option[T]) GetOrInsertWith(f func() T) *T
- func (o *Option[T]) Insert(value T) *T
- func (o Option[T]) Inspect(f func(T)) Option[T]
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsNoneOr(f func(T) bool) bool
- func (o Option[T]) IsSome() bool
- func (o Option[T]) IsSomeAnd(f func(T) bool) bool
- func (o Option[T]) Iter() iter.Seq[T]
- func (o Option[T]) Map(f func(T) T) Option[T]
- func (o Option[T]) MapOr(def T, f func(T) T) T
- func (o Option[T]) MapOrElse(def func() T, f func(T) T) T
- func (o Option[T]) OkOr(err error) res.Result[T]
- func (o Option[T]) OkOrElse(err func() error) res.Result[T]
- func (o Option[T]) Or(optb Option[T]) Option[T]
- func (o Option[T]) OrElse(f func() Option[T]) Option[T]
- func (o *Option[T]) Replace(value T) Option[T]
- func (o Option[T]) String() string
- func (o *Option[T]) Take() Option[T]
- func (o *Option[T]) TakeIf(f func(T) bool) Option[T]
- func (o Option[T]) Unwrap() T
- func (o Option[T]) UnwrapOr(def T) T
- func (o Option[T]) UnwrapOrDefault() (v T)
- func (o Option[T]) UnwrapOrElse(f func() T) T
- func (o Option[T]) Xor(optb Option[T]) Option[T]
- type OptionSlice
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
Equal compares two Option values for equality.
It returns true if both options contain the same value, or if both options are None.
func MapOr ¶
Returns the provided default result (if none), or applies a function to the contained value (if any).
Example ¶
sizeof := func(s string) int { return len(s) }
some := Some("hello world!")
fmt.Println(MapOr(some, 42, sizeof))
none := None[string]()
fmt.Println(MapOr(none, 42, sizeof))
Output: 12 42
func MapOrElse ¶
Computes a default function result (if none), or applies a different function to the contained value (if any).
Example ¶
sizeof := func(s string) int { return len(s) }
some := Some("hello world!")
fmt.Println(MapOrElse(some, func() int { return 42 }, sizeof))
none := None[string]()
fmt.Println(MapOrElse(none, func() int { return 42 }, sizeof))
Output: 12 42
func Unzip ¶
Unzips an option containing a tuple of two options.
If x is Some((a, b)) this method returns (Some(a), Some(b)). Otherwise, (None, None) is returned.
Example ¶
x := Some(tuple.New2("hello", 42))
y := None[tuple.Tuple2[string, int]]()
fmt.Println(Unzip(x))
fmt.Println(Unzip(y))
Output: Some(hello) Some(42) None None
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
The Option type.
func AndThen ¶
Returns None if the option is None, otherwise calls f with the wrapped value and returns the result.
func Flatten ¶
Converts from Option[Option[T]] to Option[T].
Example ¶
x := Some(Some(6)) fmt.Println(Flatten(x)) y := Some(None[int]()) fmt.Println(Flatten(y))
Output: Some(6) None
func Map ¶
Maps an Option<T> to Option<U> by applying a function to a contained value (if Some) or returns None (if None).
Example ¶
sizeof := func(s string) int { return len(s) }
some := Some("hello world!")
fmt.Println(Map(some, sizeof))
none := None[string]()
fmt.Println(Map(none, sizeof))
Output: Some(12) None
func Zip ¶
Zips x with y Option.
If x is Some(s) and y is Some(o), this method returns Some((s, o)). Otherwise, None is returned.
Example ¶
x := Some(42)
y := Some("hello")
z := None[rune]()
fmt.Println(Zip(x, y))
fmt.Println(Zip(x, z))
Output: Some((42, hello)) None
func ZipWith ¶
Zips x and y Option with function f.
If x is Some(s) and y is Some(o), this method returns Some(f(s, o)). Otherwise, None is returned.
Example ¶
type Point struct {
x, y float64
}
NewPoint := func(x, y float64) Point { return Point{x, y} }
x := Some(17.5)
y := Some(42.7)
z := None[float64]()
fmt.Println(ZipWith(x, y, NewPoint))
fmt.Println(ZipWith(x, z, NewPoint))
Output: Some({17.5 42.7}) None
func (Option[T]) AndThen ¶
Returns None if the option is None, otherwise calls f with the wrapped value and returns the result.
func (Option[T]) Expect ¶
Returns the contained Some value, orp panics if the value is a None with a custom panic message provided by msg.
func (Option[T]) Filter ¶
Returns None if the option is None, otherwise calls f with the wrapped value and returns:
- Some(t) if f returns true (where t is the wrapped value), and
- None if f returns false.
func (*Option[T]) GetOrInsert ¶
func (o *Option[T]) GetOrInsert(value T) *T
Inserts value into the option if it is None, then returns a pointer to the contained value.
func (*Option[T]) GetOrInsertDefault ¶
func (o *Option[T]) GetOrInsertDefault() *T
Inserts the default value into the option if it is None, then returns a mutable reference to the contained value.
func (*Option[T]) GetOrInsertWith ¶
func (o *Option[T]) GetOrInsertWith(f func() T) *T
Inserts a value computed from f into the option if it is None, then returns a mutable reference to the contained value.
func (*Option[T]) Insert ¶
func (o *Option[T]) Insert(value T) *T
Inserts value into the option, then returns a pointer to it.
func (Option[T]) IsNoneOr ¶
Returns true if the option is a None or the value inside of it matches a predicate.
func (Option[T]) IsSomeAnd ¶
Returns true if the option is a Some and the value inside of it matches a predicate.
func (Option[T]) Map ¶
Maps an Option<T> by applying a function to a contained value (if Some) or returns None (if None).
func (Option[T]) MapOr ¶
func (o Option[T]) MapOr(def T, f func(T) T) T
Returns the provided default result (if none), or applies a function to the contained value (if any).
func (Option[T]) MapOrElse ¶
func (o Option[T]) MapOrElse(def func() T, f func(T) T) T
Computes a default function result (if none), or applies a different function to the contained value (if any).
func (Option[T]) OkOr ¶
Transforms the Option[T] into a Result[T], mapping Some(v) to Ok(v) and None to Err(err).
func (Option[T]) OkOrElse ¶
Transforms the Option[T] into a Result[T], mapping Some(v) to Ok(v) and None to Err(err()).
func (Option[T]) OrElse ¶
Returns the option if it contains a value, otherwise calls f and returns the result.
func (*Option[T]) Replace ¶
Replaces the actual value in the option by the value given in parameter, returning the old value if present, leaving a Some in its place without deinitializing either one.
func (*Option[T]) TakeIf ¶
Takes the value out of the option, but only if the predicate evaluates to true on a mutable reference to the value.
func (Option[T]) UnwrapOr ¶
func (o Option[T]) UnwrapOr(def T) T
Returns the contained Some value or a provided default.
func (Option[T]) UnwrapOrDefault ¶
func (o Option[T]) UnwrapOrDefault() (v T)
Returns the contained Some value or a default.
func (Option[T]) UnwrapOrElse ¶
func (o Option[T]) UnwrapOrElse(f func() T) T
Returns the contained Some value or computes it from a closure.
type OptionSlice ¶
OptionSlice attaches the methods of sort.Interface to []Option[T], sorting in increasing order.
func (OptionSlice[T]) IsSorted ¶
func (s OptionSlice[T]) IsSorted() bool
IsSorted reports whether data is sorted.
func (OptionSlice[T]) Len ¶
func (s OptionSlice[T]) Len() int
Len is the number of elements in the collection.
func (OptionSlice[T]) Less ¶
func (s OptionSlice[T]) Less(i, j int) bool
Less reports whether the element with index i must sort before the element with index j.
func (OptionSlice[T]) Sort ¶
func (s OptionSlice[T]) Sort()
Sort is a convenience method: x.Sort() calls Sort(x).
func (OptionSlice[T]) Swap ¶
func (s OptionSlice[T]) Swap(i, j int)
Swap swaps the elements with indexes i and j.