opt

package
v0.0.0-...-e1eb702 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: MIT Imports: 6 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare[T cmp.Ordered](x, y Option[T]) int

Compare compares x and y and returns:

-1 if x < y;
0 if x == y;
+1 if x > y.

func Equal

func Equal[T cmp.Ordered](x, y Option[T]) bool

Equal compares two Option values for equality.

It returns true if both options contain the same value, or if both options are None.

func IsSorted

func IsSorted[T cmp.Ordered](s []Option[T]) bool

IsSorted reports whether the slice s is sorted in increasing order.

func Less

func Less[T cmp.Ordered](x, y Option[T]) bool

Less reports whether x is less than y.

func MapOr

func MapOr[T, U any](o Option[T], def U, f func(T) U) U

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

func MapOrElse[T, U any](o Option[T], def func() U, f func(T) U) U

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 Sort

func Sort[T cmp.Ordered](s []Option[T])

Sort a slice of options in increasing order.

func Unzip

func Unzip[T, U any](x Option[tuple.Tuple2[T, U]]) (Option[T], Option[U])

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 And

func And[T, U any](opta Option[T], optb Option[U]) Option[U]

Returns None if the option is None, otherwise returns optb.

func AndThen

func AndThen[T, U any](o Option[T], f func(T) Option[U]) Option[U]

Returns None if the option is None, otherwise calls f with the wrapped value and returns the result.

func Err

func Err[T any](r res.Result[T]) Option[error]

Converts from Result<T, E> to Option<E>.

func Flatten

func Flatten[T any](o Option[Option[T]]) Option[T]

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

func Map[T, U any](o Option[T], f func(T) U) Option[U]

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 None

func None[T any]() Option[T]

No value.

func Ok

func Ok[T any](r res.Result[T]) Option[T]

Converts from Result[T] to Option[T].

func Some

func Some[T any](value T) Option[T]

Some value of type T.

func Wrap

func Wrap[T any](value *T) Option[T]

Wrap a optional value of type T.

func Zip

func Zip[T, U any](x Option[T], y Option[U]) Option[tuple.Tuple2[T, U]]

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

func ZipWith[T, U, R any](x Option[T], y Option[U], f func(T, U) R) Option[R]

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]) And

func (o Option[T]) And(optb Option[T]) Option[T]

Returns None if the option is None, otherwise returns optb.

func (Option[T]) AndThen

func (o Option[T]) AndThen(f func(T) Option[T]) Option[T]

Returns None if the option is None, otherwise calls f with the wrapped value and returns the result.

func (Option[T]) Expect

func (o Option[T]) Expect(msg string) T

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

func (o Option[T]) Filter(f func(T) bool) Option[T]

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]) Inspect

func (o Option[T]) Inspect(f func(T)) Option[T]

Calls a function with a reference to the contained value if Some.

func (Option[T]) IsNone

func (o Option[T]) IsNone() bool

Returns true if the option is a None value.

func (Option[T]) IsNoneOr

func (o Option[T]) IsNoneOr(f func(T) bool) bool

Returns true if the option is a None or the value inside of it matches a predicate.

func (Option[T]) IsSome

func (o Option[T]) IsSome() bool

Returns true if the option is a Some value.

func (Option[T]) IsSomeAnd

func (o Option[T]) IsSomeAnd(f func(T) bool) bool

Returns true if the option is a Some and the value inside of it matches a predicate.

func (Option[T]) Iter

func (o Option[T]) Iter() iter.Seq[T]

Returns an iterator over the possibly contained value.

func (Option[T]) Map

func (o Option[T]) Map(f func(T) T) Option[T]

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

func (o Option[T]) OkOr(err error) res.Result[T]

Transforms the Option[T] into a Result[T], mapping Some(v) to Ok(v) and None to Err(err).

func (Option[T]) OkOrElse

func (o Option[T]) OkOrElse(err func() error) res.Result[T]

Transforms the Option[T] into a Result[T], mapping Some(v) to Ok(v) and None to Err(err()).

func (Option[T]) Or

func (o Option[T]) Or(optb Option[T]) Option[T]

Returns the option if it contains a value, otherwise returns optb.

func (Option[T]) OrElse

func (o Option[T]) OrElse(f func() Option[T]) Option[T]

Returns the option if it contains a value, otherwise calls f and returns the result.

func (*Option[T]) Replace

func (o *Option[T]) Replace(value T) Option[T]

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]) String

func (o Option[T]) String() string

func (*Option[T]) Take

func (o *Option[T]) Take() Option[T]

Takes the value out of the option, leaving a None in its place.

func (*Option[T]) TakeIf

func (o *Option[T]) TakeIf(f func(T) bool) Option[T]

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]) Unwrap

func (o Option[T]) Unwrap() T

Returns the contained Some 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.

func (Option[T]) Xor

func (o Option[T]) Xor(optb Option[T]) Option[T]

Returns Some if exactly one of self, optb is Some, otherwise returns None.

type OptionSlice

type OptionSlice[T cmp.Ordered] []Option[T]

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.

Jump to

Keyboard shortcuts

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