Documentation
¶
Overview ¶
Package optpair provides a way to represent optional pairs of values in Go.
Index ¶
- func Compare[V1, V2 cmp.Ordered](a, b Pair[V1, V2]) int
- func IsNone[V1, V2 any](pair Pair[V1, V2]) bool
- func IsSome[V1, V2 any](pair Pair[V1, V2]) bool
- func Left[V1, V2 any](pair Pair[V1, V2]) optval.Value[V1]
- func Less[V1, V2 cmp.Ordered](a, b Pair[V1, V2]) bool
- func OrZero[V1, V2 any](pair Pair[V1, V2]) (V1, V2)
- func Right[V1, V2 any](pair Pair[V1, V2]) optval.Value[V2]
- func Split[V1, V2 any](pair Pair[V1, V2]) (optval.Value[V1], optval.Value[V2])
- func Unwrap[V1, V2 any](pair Pair[V1, V2]) (V1, V2, bool)
- func UnwrapFilter[V1, V2 any](pairs iter.Seq[Pair[V1, V2]]) iter.Seq2[V1, V2]
- type Pair
- func ByKey[K comparable, V any, M ~map[K]V](key K, m M) Pair[K, V]
- func Filter[V1, V2 any, F ~func(V1, V2) bool](pair Pair[V1, V2], f F) Pair[V1, V2]
- func FindSome[V1, V2 any](pairs iter.Seq[Pair[V1, V2]]) Pair[V1, V2]
- func FlatMap[V1, V2, R1, R2 any, F ~func(V1, V2) Pair[R1, R2]](pair Pair[V1, V2], f F) Pair[R1, R2]
- func FromValue[V, R1, R2 any, F ~func(V) (R1, R2)](value optval.Value[V], f F) Pair[R1, R2]
- func JoinAnd[V1, V2 any](v1 optval.Value[V1], v2 optval.Value[V2]) Pair[V1, V2]
- func JoinOr[V1, V2 any](v1 optval.Value[V1], v2 optval.Value[V2]) Pair[V1, V2]
- func Map[V1, V2, R1, R2 any, F ~func(V1, V2) (R1, R2)](pair Pair[V1, V2], f F) Pair[R1, R2]
- func New[V1, V2 any](v1 V1, v2 V2, valid bool) Pair[V1, V2]
- func None[V1, V2 any]() Pair[V1, V2]
- func Or[V1, V2 any](pairs ...Pair[V1, V2]) Pair[V1, V2]
- func Some[V1, V2 any](v1 V1, v2 V2) Pair[V1, V2]
- func Swap[V1, V2 any](pair Pair[V1, V2]) Pair[V2, V1]
- func (p Pair[V1, V2]) IsNone() bool
- func (p Pair[V1, V2]) IsSome() bool
- func (p Pair[V1, V2]) Left() optval.Value[V1]
- func (p Pair[V1, V2]) Or(other Pair[V1, V2]) Pair[V1, V2]
- func (p Pair[V1, V2]) OrElse(f func() Pair[V1, V2]) Pair[V1, V2]
- func (p Pair[V1, V2]) OrElseSome(f func() (V1, V2)) (V1, V2)
- func (p Pair[V1, V2]) OrSome(v1 V1, v2 V2) (V1, V2)
- func (p Pair[V1, V2]) OrZero() (V1, V2)
- func (p *Pair[V1, V2]) Replace(v1 V1, v2 V2) Pair[V1, V2]
- func (p *Pair[V1, V2]) Reset()
- func (p Pair[V1, V2]) Right() optval.Value[V2]
- func (p Pair[V1, V2]) Split() (optval.Value[V1], optval.Value[V2])
- func (p Pair[V1, V2]) Swap() Pair[V2, V1]
- func (p *Pair[V1, V2]) Take() Pair[V1, V2]
- func (p Pair[V1, V2]) Unwrap() (V1, V2, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Left ¶
Left returns the left value of the given pair if it is Some. Otherwise, it returns None.
func OrZero ¶ added in v0.3.0
OrZero returns the inner values of the given pair if it is Some. Otherwise, it returns zero initialized values.
func Right ¶
Right returns the right value of the given pair if it is Some. Otherwise, it returns None.
Types ¶
type Pair ¶
type Pair[V1, V2 any] struct { // contains filtered or unexported fields }
Pair represents an optional pair of values of types V1 and V2. It can be either Some or None. If it is Some, it contains the inner values of types V1 and V2.
func ByKey ¶
func ByKey[K comparable, V any, M ~map[K]V](key K, m M) Pair[K, V]
ByKey returns Some in case it is found in the provided map by the given key. Otherwise, it returns None.
func Filter ¶
Filter calls the given function f with the inner values of the given pair and returns the same pair if the function returns true. Otherwise, it returns None.
func FindSome ¶ added in v0.3.0
FindSome returns the first optional pair that is Some or None if all pairs are None. It returns None if the sequence is empty.
func FlatMap ¶
FlatMap transforms optional Pair[V1, V2] to Pair[R1, R1] using the given function f. If the provided Pair is None, it returns None. If f returns None, it returns None.
func FromValue ¶
FromValue converts optval.Value to Pair using the given function f.
func JoinAnd ¶
JoinAnd returns a Some pair containing the inner values of v1 and v2 if they are both optval.Some. Otherwise, it returns None.
func JoinOr ¶
JoinOr returns a Some pair containing the inner values of v1 and v2 if at least one of them is optval.Some. Otherwise, it returns None. If some of the values are None, they are replaced with zero values.
func Map ¶
Map transforms optional Pair[V1, V2] to Pair[R1, R1] using the given function f. If the provided Pair is None, it returns None.
func Or ¶ added in v0.3.0
Or returns the first Some pair from the given pairs. If all pairs are None, it returns None.
func (Pair[V1, V2]) OrElse ¶
OrElse returns p if the inner values are present, otherwise it calls f and returns its result.
func (Pair[V1, V2]) OrElseSome ¶
func (p Pair[V1, V2]) OrElseSome(f func() (V1, V2)) (V1, V2)
OrElseSome returns the inner values if present, otherwise it calls f and returns its result.
func (Pair[V1, V2]) OrSome ¶
func (p Pair[V1, V2]) OrSome(v1 V1, v2 V2) (V1, V2)
OrSome returns the inner values if present, otherwise it returns the given values.
func (Pair[V1, V2]) OrZero ¶
func (p Pair[V1, V2]) OrZero() (V1, V2)
OrZero returns the inner values if present, otherwise it returns zero initialized values.
func (Pair[V1, V2]) Right ¶ added in v0.3.0
Right returns the right value of the pair if it is Some.
func (Pair[V1, V2]) Split ¶ added in v0.3.0
Split returns the inner values of the pair as [optval.Value]s.