optpair

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: 5 Imported by: 1

Documentation

Overview

Package optpair provides a way to represent optional pairs of values in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare[V1, V2 cmp.Ordered](a, b Pair[V1, V2]) int

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

func IsNone added in v0.3.0

func IsNone[V1, V2 any](pair Pair[V1, V2]) bool

IsNone returns true if the given pair is None.

func IsSome added in v0.3.0

func IsSome[V1, V2 any](pair Pair[V1, V2]) bool

IsSome returns true if the given pair is Some.

func Left

func Left[V1, V2 any](pair Pair[V1, V2]) optval.Value[V1]

Left returns the left value of the given pair if it is Some. Otherwise, it returns None.

func Less

func Less[V1, V2 cmp.Ordered](a, b Pair[V1, V2]) bool

Less returns true if a is less b. Some is considered less than None.

func OrZero added in v0.3.0

func OrZero[V1, V2 any](pair Pair[V1, V2]) (V1, V2)

OrZero returns the inner values of the given pair if it is Some. Otherwise, it returns zero initialized values.

func Right[V1, V2 any](pair Pair[V1, V2]) optval.Value[V2]

Right returns the right value of the given pair if it is Some. Otherwise, it returns None.

func Split

func Split[V1, V2 any](pair Pair[V1, V2]) (optval.Value[V1], optval.Value[V2])

Split returns the inner values of the given pair as [optval.Value]s.

func Unwrap added in v0.3.0

func Unwrap[V1, V2 any](pair Pair[V1, V2]) (V1, V2, bool)

Unwrap returns the inner values of the given pair if it is Some. Otherwise, it returns zero values and false.

func UnwrapFilter

func UnwrapFilter[V1, V2 any](pairs iter.Seq[Pair[V1, V2]]) iter.Seq2[V1, V2]

UnwrapFilter returns a sequence of pairs that contain only Some values from the given pairs.

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

func Filter[V1, V2 any, F ~func(V1, V2) bool](pair Pair[V1, V2], f F) Pair[V1, V2]

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

func FindSome[V1, V2 any](pairs iter.Seq[Pair[V1, V2]]) Pair[V1, V2]

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

func FlatMap[V1, V2, R1, R2 any, F ~func(V1, V2) Pair[R1, R2]](pair Pair[V1, V2], f F) Pair[R1, R2]

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

func FromValue[V, R1, R2 any, F ~func(V) (R1, R2)](value optval.Value[V], f F) Pair[R1, R2]

FromValue converts optval.Value to Pair using the given function f.

func JoinAnd

func JoinAnd[V1, V2 any](v1 optval.Value[V1], v2 optval.Value[V2]) Pair[V1, V2]

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

func JoinOr[V1, V2 any](v1 optval.Value[V1], v2 optval.Value[V2]) Pair[V1, V2]

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

func Map[V1, V2, R1, R2 any, F ~func(V1, V2) (R1, R2)](pair Pair[V1, V2], f F) Pair[R1, R2]

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 New

func New[V1, V2 any](v1 V1, v2 V2, valid bool) Pair[V1, V2]

New constructs a new optional Pair.

func None

func None[V1, V2 any]() Pair[V1, V2]

None returns an empty optional Pair.

func Or added in v0.3.0

func Or[V1, V2 any](pairs ...Pair[V1, V2]) Pair[V1, V2]

Or returns the first Some pair from the given pairs. If all pairs are None, it returns None.

func Some

func Some[V1, V2 any](v1 V1, v2 V2) Pair[V1, V2]

Some returns an optional Pair that has the given inner pair of values.

func Swap

func Swap[V1, V2 any](pair Pair[V1, V2]) Pair[V2, V1]

Swap returns a new pair with swapped inner values.

func (Pair[V1, V2]) IsNone

func (p Pair[V1, V2]) IsNone() bool

IsNone returns true if the optional pair p has no inner values.

func (Pair[V1, V2]) IsSome

func (p Pair[V1, V2]) IsSome() bool

IsSome returns true if the optional pair p has inner values.

func (Pair[V1, V2]) Left added in v0.3.0

func (p Pair[V1, V2]) Left() optval.Value[V1]

Left returns the left value of the pair if it is Some.

func (Pair[V1, V2]) Or

func (p Pair[V1, V2]) Or(other Pair[V1, V2]) Pair[V1, V2]

Or returns p if it is Some or other given pair otherwise.

func (Pair[V1, V2]) OrElse

func (p Pair[V1, V2]) OrElse(f func() Pair[V1, V2]) Pair[V1, V2]

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

func (p *Pair[V1, V2]) Replace(v1 V1, v2 V2) Pair[V1, V2]

Replace returns a copy of p and resets it to Some with v1 and v2.

func (*Pair[V1, V2]) Reset

func (p *Pair[V1, V2]) Reset()

Reset resets p to None.

func (Pair[V1, V2]) Right added in v0.3.0

func (p Pair[V1, V2]) Right() optval.Value[V2]

Right returns the right value of the pair if it is Some.

func (Pair[V1, V2]) Split added in v0.3.0

func (p Pair[V1, V2]) Split() (optval.Value[V1], optval.Value[V2])

Split returns the inner values of the pair as [optval.Value]s.

func (Pair[V1, V2]) Swap added in v0.3.0

func (p Pair[V1, V2]) Swap() Pair[V2, V1]

Swap returns a new pair with swapped inner values.

func (*Pair[V1, V2]) Take

func (p *Pair[V1, V2]) Take() Pair[V1, V2]

Take returns a copy of p and resets it to None.

func (Pair[V1, V2]) Unwrap

func (p Pair[V1, V2]) Unwrap() (V1, V2, bool)

Unwrap returns the inner values of type V1 and V2 and true if present. Otherwise, it returns zero values and false.

Jump to

Keyboard shortcuts

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