optional

package
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2023 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Overview

Optional is an optic used to zoom inside a product. Unlike the `Lens`, the element that the `Optional` focuses on may not exist.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compose

func Compose[S, A, B any](ab Optional[A, B]) func(Optional[S, A]) Optional[S, B]

Compose combines two Optional and allows to narrow down the focus to a sub-Optional

func ComposeRef

func ComposeRef[S, A, B any](ab Optional[A, B]) func(Optional[*S, A]) Optional[*S, B]

ComposeRef combines two Optional and allows to narrow down the focus to a sub-Optional

func FromPredicate

func FromPredicate[S, A any](pred func(A) bool) func(func(S) A, func(S, A) S) Optional[S, A]

FromPredicate creates an optional from getter and setter functions. It checks for optional values and the correct update procedure

func FromPredicateRef

func FromPredicateRef[S, A any](pred func(A) bool) func(func(*S) A, func(*S, A) *S) Optional[*S, A]

FromPredicate creates an optional from getter and setter functions. It checks for optional values and the correct update procedure

func IChain

func IChain[S, A, B any](ab func(A) O.Option[B], ba func(B) O.Option[A]) func(Optional[S, A]) Optional[S, B]

IChain implements a bidirectional mapping of the transform if the transform can produce optionals (e.g. in case of type mappings)

func IChainAny

func IChainAny[S, A any]() func(Optional[S, any]) Optional[S, A]

IChainAny implements a bidirectional mapping to and from any

func IMap

func IMap[S, A, B any](ab func(A) B, ba func(B) A) func(Optional[S, A]) Optional[S, B]

IMap implements a bidirectional mapping of the transform

func ModifyOption

func ModifyOption[S, A any](f func(A) A) func(Optional[S, A]) func(S) O.Option[S]

func SetOption

func SetOption[S, A any](a A) func(Optional[S, A]) func(S) O.Option[S]

Types

type Optional

type Optional[S, A any] struct {
	GetOption func(s S) O.Option[A]
	Set       func(a A) func(S) S
}

Optional is an optional reference to a subpart of a data type

func Id

func Id[S any]() Optional[S, S]

Id returns am optional implementing the identity operation

func IdRef

func IdRef[S any]() Optional[*S, *S]

Id returns am optional implementing the identity operation

func MakeOptional

func MakeOptional[S, A any](get func(S) O.Option[A], set func(S, A) S) Optional[S, A]

MakeOptional creates an Optional based on a getter and a setter function. Make sure that the setter creates a (shallow) copy of the data. This happens automatically if the data is passed by value. For pointers consider to use `MakeOptionalRef` and for other kinds of data structures that are copied by reference make sure the setter creates the copy.

func MakeOptionalRef

func MakeOptionalRef[S, A any](get func(*S) O.Option[A], set func(*S, A) *S) Optional[*S, A]

MakeOptionalRef creates an Optional based on a getter and a setter function. The setter passed in does not have to create a shallow copy, the implementation wraps the setter into one that copies the pointer before modifying it

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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