Optics

optics/optional

Optional is an optic used to zoom inside a product.

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.

Functions

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[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[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[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[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[S, A any]() func(Optional[S, any]) Optional[S, A]

IChainAny implements a bidirectional mapping to and from any

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 Id[S any]() Optional[S, S]

Id returns am optional implementing the identity operation

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

Id returns am optional implementing the identity operation

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[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

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

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

Types

type Optional

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

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