optional

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optional

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

Optional is a value that may or may not be present.

func As added in v0.3.0

func As[T any](v any) Optional[T]

As try to convert the value to the given type.

if the value is not convertible to the given type, then the function returns None, otherwise, it returns Some.

func FromPair added in v0.3.0

func FromPair[T any](data T, ok bool) Optional[T]

FromPair returns an Optional from a value.

func FromPtr added in v0.3.0

func FromPtr[T any, P ~*T](data P) Optional[T]

FromPtr returns an Optional from a value.

func Map

func Map[A, B any](opt Optional[A], f func(A) B) Optional[B]

func None

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

None returns an Optional from a value.

func Some

func Some[T any](data T) Optional[T]

Some returns an Optional from a value.

Oops!! We can't restrict T is not pointer type. Holy shit!!!

func (Optional[T]) And

func (self Optional[T]) And(opt Optional[T]) Optional[T]

And returns None if the option is None, otherwise returns given opt.

func (Optional[T]) AndThen added in v0.2.0

func (self Optional[T]) AndThen(f func(T) Optional[T]) Optional[T]

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

func (Optional[T]) CloneBy added in v0.3.0

func (self Optional[T]) CloneBy(clone func(T) T) Optional[T]

func (Optional[T]) EqBy added in v0.4.0

func (self Optional[T]) EqBy(opt Optional[T], eq func(T, T) bool) bool

func (Optional[T]) Filter

func (self Optional[T]) Filter(f func(T) bool) Optional[T]

func (Optional[T]) IfPresent

func (self Optional[T]) IfPresent(f func(T))

func (Optional[T]) IsNone

func (self Optional[T]) IsNone() bool

IsNone returns true if the Optional is None.

func (Optional[T]) IsSome

func (self Optional[T]) IsSome() bool

IsSome returns true if the Optional is Some.

func (Optional[T]) IsSomeAnd added in v0.2.0

func (self Optional[T]) IsSomeAnd(f func(T) bool) bool

IsSomeAnd returns true if the Optional is Some and satisfies the given predicate.

func (Optional[T]) Map

func (self Optional[T]) Map(f func(T) T) Optional[T]

Map returns None if the option is None, otherwise calls the given function and returns the result.

func (Optional[T]) MapOr added in v0.2.0

func (self Optional[T]) MapOr(v T, f func(T) T) T

MapOr returns None if the option is None, otherwise calls the given function and returns the result.

func (Optional[T]) MapOrElse added in v0.2.0

func (self Optional[T]) MapOrElse(df func() T, f func(T) T) T

MapOrElse returns None if the option is None, otherwise calls the given function and returns the result.

func (Optional[T]) MarshalJSON added in v0.3.0

func (self Optional[T]) MarshalJSON() ([]byte, error)

func (Optional[T]) Or

func (self Optional[T]) Or(opt Optional[T]) Optional[T]

Or returns the given opt if the option is None, otherwise returns the option.

func (Optional[T]) OrElse

func (self Optional[T]) OrElse(f func() Optional[T]) Optional[T]

OrElse returns the Optional if it contains a value, otherwise calls f and returns the result.

func (Optional[T]) String added in v0.1.1

func (self Optional[T]) String() string

func (Optional[T]) UnmarshalJSON added in v0.3.0

func (self Optional[T]) UnmarshalJSON(v []byte) error

func (Optional[T]) Value

func (self Optional[T]) Value() T

Value returns the value of the Optional.

func (Optional[T]) ValueOr added in v0.2.0

func (self Optional[T]) ValueOr(v T) T

ValueOr returns the Optional if it is Some, otherwise returns the given default value.

func (Optional[T]) ValueOrElse added in v0.2.0

func (self Optional[T]) ValueOrElse(f func() T) T

ValueOrElse returns the Optional if it is Some, otherwise returns the given default value.

func (Optional[T]) ValueOrZero added in v0.2.0

func (self Optional[T]) ValueOrZero() T

ValueOrZero returns the Optional if it is Some, otherwise returns the zero value of the type.

func (Optional[T]) Xor added in v0.2.0

func (self Optional[T]) Xor(opt Optional[T]) Optional[T]

Xor returns None if the option is None, otherwise returns the given opt.

Jump to

Keyboard shortcuts

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