optval

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package optval provides a way to represent optional values in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[T any](values ...Value[T]) []T

Collect collects all values from the given optional values and returns them as a slice.

func Compare

func Compare[T cmp.Ordered](a, b Value[T]) int

Compare compares two optional values. Some is considered less than None.

func IsNone added in v0.3.0

func IsNone[T any](v Value[T]) bool

IsNone returns true if the optional value v has no inner value.

func IsSome added in v0.3.0

func IsSome[T any](v Value[T]) bool

IsSome returns true if the optional value v has inner value.

func Less

func Less[T cmp.Ordered](a, b Value[T]) bool

Less returns true if the first value is less than the second one. Some is considered less than None.

func OrZero added in v0.3.0

func OrZero[T any](v Value[T]) T

OrZero returns the inner value of the optional value v if it is Some, otherwise it returns zero value.

func Unwrap added in v0.3.0

func Unwrap[T any](v Value[T]) (T, bool)

Unwrap returns the inner value of type T and true if it is Some. Otherwise it returns zero value and false.

func UnwrapFilter

func UnwrapFilter[T any](values iter.Seq[Value[T]]) iter.Seq[T]

UnwrapFilter returns a sequence of values that contain inner values from Some values.

Types

type Value

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

Value represents an optional value of type T. It can be either Some with inner value or None.

func ByKey

func ByKey[K comparable, V any, M ~map[K]V](key K, m M) Value[V]

ByKey returns Some value in case it is found in the provided map by the provided key.

func Filter

func Filter[T any, F ~func(T) bool](v Value[T], f F) Value[T]

Filter filters optional Value by the given predicate. If the value is Some and the predicate returns true, it returns the same value. Otherwise, it returns None.

func FindSome added in v0.3.0

func FindSome[T any](values iter.Seq[Value[T]]) Value[T]

FindSome returns the first optional value that is Some or None if all values are None. It returns None if the sequence is empty.

func FlatMap

func FlatMap[T, U any, F ~func(T) Value[U]](v Value[T], f F) Value[U]

FlatMap transforms optional Value[T] to optional Value[U] using the given function.

func Flatten

func Flatten[T any](v Value[Value[T]]) Value[T]

Flatten flattens optional Value[Value[T]] to Value[T].

func FromPtr

func FromPtr[T any](value *T) Value[T]

FromPtr returns Some with a copy of the given value if the provided pointer is not nil. Otherwise, it returns None.

func Key

func Key[K comparable, V any, M ~map[K]V](key K, m M) Value[K]

Key returns Some key in case it is found in the provided map.

func Map

func Map[T, U any, F ~func(T) U](v Value[T], f F) Value[U]

Map transforms optional Value[T] to optional Value[U] using the given function.

func MapFromPtr

func MapFromPtr[T, U any, F ~func(T) U](v *T, f F) Value[U]

MapFromPtr transforms v to optional Value using the given function. If v is nil, it returns None. Otherwise, it returns Some with the result of the function call.

func New

func New[T any](value T, valid bool) Value[T]

New constructs a new optional Value.

func None

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

None returns an optional Value that has no inner value.

func Or added in v0.3.0

func Or[T any](values ...Value[T]) Value[T]

Or returns the first optional value that is Some or None if all values are None.

func Some

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

Some returns an optional Value that has provided inner value.

func (Value[T]) CopyPtr

func (v Value[T]) CopyPtr() *T

CopyPtr returns a pointer to a copy of the inner value T if present, otherwise it returns nil.

func (Value[T]) IsNone

func (v Value[T]) IsNone() bool

IsNone returns true if the optional value v has no inner value.

func (Value[T]) IsSome

func (v Value[T]) IsSome() bool

IsSome returns true if the optional value v has inner value.

func (Value[T]) Or

func (v Value[T]) Or(other Value[T]) Value[T]

Or returns the optional value v if it is Some, otherwise it returns provided value.

func (Value[T]) OrElse

func (v Value[T]) OrElse(value func() Value[T]) Value[T]

OrElse returns the optional value v if it is Some, otherwise it calls provided function and returns its result.

func (Value[T]) OrElseSome

func (v Value[T]) OrElseSome(value func() T) T

OrElseSome returns the inner value T if it is Some, otherwise it calls provided function and returns its result.

func (Value[T]) OrSome

func (v Value[T]) OrSome(value T) T

OrSome returns the inner value T if present, otherwise it returns provided value.

func (Value[T]) OrZero

func (v Value[T]) OrZero() T

OrZero returns the inner value T if present, otherwise it returns zero value.

func (*Value[T]) Replace

func (v *Value[T]) Replace(value T) Value[T]

Replace returns a copy of optional value v and sets it to Some with the given value.

func (*Value[T]) Reset

func (v *Value[T]) Reset()

Reset resets the optional value v to None.

func (*Value[T]) Take

func (v *Value[T]) Take() Value[T]

Take returns a copy of optional value v and resets it to None.

func (Value[T]) Unwrap

func (v Value[T]) Unwrap() (T, bool)

Unwrap returns the inner value of type T and true if it is Some. Otherwise it returns zero value and false.

Jump to

Keyboard shortcuts

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