package optional

import "github.com/IBM/fp-go/optics/optional"

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

Index

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) EM.Endomorphism[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

Source Files

optional.go

Directories

PathSynopsis
optics/optional/lens
optics/optional/prism
optics/optional/record
optics/optional/record/generic
Version
v1.0.151 (latest)
Published
Nov 23, 2024
Platform
linux/amd64
Imports
3 packages
Last checked
5 months ago

Tools for package owners.