option

package
v0.0.0-...-020adb6 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeriveMonoid

func DeriveMonoid[T any](s algebra.Semigroup[T]) algebra.Monoid[Option[T]]

DeriveMonoid derives Monoid[Option[T]] from Semigroup[T]. T doesn't need to be Monoid since the None() is the identity element of the monoid.

func DeriveSemigroup

func DeriveSemigroup[T any](s algebra.Semigroup[T]) algebra.Semigroup[Option[T]]

DeriveSemigroup derives Semigroup[Option[T]] from Semigroup[T]

func Equal

func Equal[T comparable](x, y Option[T]) bool

Equal returns true if and only if the two value x and y are the same, that is, both x and y are nil or both have the same value in the sense of ==.

func Find

func Find[T any](xs Option[T], fn func(T) bool) (T, bool)

Find returns a first element in xs that satisfies the given predicate fn. It returns false as a second return value if no elements are found.

func FindElem

func FindElem[T any](eq cmp.Eq[T]) func(xs Option[T], e T) (T, bool)

FindElem returns a first element in xs that equals to e in the sense of given Eq. It returns false as a second return value if no elements are found.

func Fold

func Fold[T any, U any](init T, xs Option[U], fn func(T, U) T) T

Fold accumulates every element in a collection by applying fn.

func ForEach

func ForEach[T any](xs Option[T], fn func(T))

ForEach applies fn to each element in xs.

func IsSome

func IsSome[T any](x Option[T]) bool

IsSome returns true if and only if x has a value.

func Max

func Max[T any](ord cmp.Ord[T]) func(xs Option[T]) (T, bool)

Max returns the largest element with respect to the given Ord. It returns <zero value>, false if the collection is empty.

func MaxBy

func MaxBy[T any](xs Option[T], less func(T, T) bool) (T, bool)

MaxBy returns the smallest element with respect to the given function. It returns <zero value>, false if the collection is empty.

func Min

func Min[T any](ord cmp.Ord[T]) func(xs Option[T]) (T, bool)

Min returns the smallest element with respect to the given Ord. It returns <zero value>, false if the collection is empty.

func MinBy

func MinBy[T any](xs Option[T], less func(T, T) bool) (T, bool)

MinBy returns the smallest element with respect to the given function. It returns <zero value>, false if the collection is empty.

func Sum

func Sum[T any](m algebra.Monoid[T]) func(xs Option[T]) T

Sum sums up all values in xs. It returns m.Empty() when xs is empty.

func SumWithInit

func SumWithInit[T any](s algebra.Semigroup[T]) func(init T, xs Option[T]) T

SumWithInit sums up init and all values in xs.

func Unwrap

func Unwrap[T any](x Option[T]) T

Unwrap returns a value that x has. It panics if x has no value.

func UnwrapOr

func UnwrapOr[T any](x Option[T], def T) T

UnwrapOr returns a value that x has. It returns def if x has no value.

func UnwrapOrElse

func UnwrapOrElse[T any](x Option[T], def func() T) T

UnwrapOrElse returns a value that x has. It returns def() if x has no value.

Types

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

Option is an optional value.

func Filter

func Filter[T any](xs Option[T], fn func(T) bool) Option[T]

Filter returns a collection that only returns elements that satisfies given predicate.

func FromIterator

func FromIterator[T any](it iterator.Iterator[T]) Option[T]

FromIterator returns an Option that has the first element in the given Iterator. It returns None() if the iterator has no value.

func Map

func Map[T, U any](xs Option[T], fn func(T) U) Option[U]

Map returns a collection that applies fn to each element of xs.

func None

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

None returns an Option that has no value.

func Some

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

Some returns an Option that consists of x.

func (Option[T]) Iter

func (x Option[T]) Iter() iterator.Iterator[T]

Iter returns an Iterator that returns a single value that x has, or no value if x is nil.

Jump to

Keyboard shortcuts

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