PPrism

``public class PPrism<S, T, A, B>``

A Prism is a loss less invertible optic that can look into a structure and optionally find its focus. It is mostly used for finding a focus that is only present under certain conditions, like in a sum type.

A (polymorphic) PPrism is useful when setting or modifying a value for a polymorphic sum type.

A PPrism gathres the two concepts of pattern matching and constructor and thus can be seen as a pair of functions: - `getOrModify` meaning it returns the focus of a PPRism or the original value. - `reverseGet` meaining we can construct the source type of a PPrism from a focus.

Type parameters: - `S`: Source. - `T`: Modified source. - `A`: Focus. - `B`: Modified focus.

• ``` +(_:_:) ```

Composes a `PPrism` with a `PPrism`.

Declaration

Swift

``public static func + <C, D>(lhs: PPrism<S, T, A, B>, rhs: PPrism<A, B, C, D>) -> PPrism<S, T, C, D>``

Parameters

 ``` lhs ``` Left side of the composition. ``` rhs ``` Right side of the composition.

Return Value

A `PPrism` resulting from the sequential application of the two provided optics.

• ``` +(_:_:) ```

Composes a `PPrism` with a `PIso`.

Declaration

Swift

``public static func + <C, D>(lhs: PPrism<S, T, A, B>, rhs: PIso<A, B, C, D>) -> PPrism<S, T, C, D>``

Parameters

 ``` lhs ``` Left side of the composition. ``` rhs ``` Right side of the composition.

Return Value

A `PPrism` resulting from the sequential application of the two provided optics.

• ``` +(_:_:) ```

Composes a `PPrism` with a `PLens`.

Declaration

Swift

``public static func + <C, D>(lhs: PPrism<S, T, A, B>, rhs: PLens<A, B, C, D>) -> PAffineTraversal<S, T, C, D>``

Parameters

 ``` lhs ``` Left side of the composition. ``` rhs ``` Right side of the composition.

Return Value

A `AffineTraversal` resulting from the sequential application of the two provided optics.

• ``` +(_:_:) ```

Composes a `PPrism` with a `AffineTraversal`.

Declaration

Swift

``public static func + <C, D>(lhs: PPrism<S, T, A, B>, rhs: PAffineTraversal<A, B, C, D>) -> PAffineTraversal<S, T, C, D>``

Parameters

 ``` lhs ``` Left side of the composition. ``` rhs ``` Right side of the composition.

Return Value

A `AffineTraversal` resulting from the sequential application of the two provided optics.

• ``` +(_:_:) ```

Composes a `PPrism` with a `PSetter`.

Declaration

Swift

``public static func + <C, D>(lhs: PPrism<S, T, A, B>, rhs: PSetter<A, B, C, D>) -> PSetter<S, T, C, D>``

Parameters

 ``` lhs ``` Left side of the composition. ``` rhs ``` Right side of the composition.

Return Value

A `PSetter` resulting from the sequential application of the two provided optics.

• ``` +(_:_:) ```

Composes a `PPrism` with a `Fold`.

Declaration

Swift

``public static func + <C>(lhs: PPrism<S, T, A, B>, rhs: Fold<A, C>) -> Fold<S, C>``

Parameters

 ``` lhs ``` Left side of the composition. ``` rhs ``` Right side of the composition.

Return Value

A `Fold` resulting from the sequential application of the two provided optics.

• ``` +(_:_:) ```

Composes a `PPrism` with a `PTraversal`.

Declaration

Swift

``public static func + <C, D>(lhs: PPrism<S, T, A, B>, rhs: PTraversal<A, B, C, D>) -> PTraversal<S, T, C, D>``

Parameters

 ``` lhs ``` Left side of the composition. ``` rhs ``` Right side of the composition.

Return Value

A `PTraversal` resulting from the sequential application of the two provided optics.

• ``` init(getOrModify:reverseGet:) ```

Initializes a Prism.

Declaration

Swift

``public init(getOrModify: @escaping (S) -> Either<T, A>, reverseGet: @escaping (B) -> T)``

Parameters

 ``` getOrModify ``` Gets the focus of the prism, if present. ``` reverseGet ``` Builds the source of the prism from its focus.
• ``` getOrModify(_:) ```

Retrieves the focus or modifies the source.

Declaration

Swift

``public func getOrModify(_ s: S) -> Either<T, A>``

Parameters

 ``` s ``` Source.

Return Value

Either the modified source or the focus of the prism.

• ``` reverseGet(_:) ```

Obtains a modified source.

Declaration

Swift

``public func reverseGet(_ b: B) -> T``

Parameters

 ``` b ``` Modified focus.

Return Value

Modified source.

• ``` modifyF(_:_:) ```

Modifies the focus of a PPrism with an `Applicative` function.

Declaration

Swift

``public func modifyF<F: Applicative>(_ s: S, _ f: @escaping (A) -> Kind<F, B>) -> Kind<F, T>``

Parameters

 ``` s ``` Source. ``` f ``` Modifying function.

Return Value

Modified source in the context of the `Applicative`.

• ``` liftF(_:) ```

Lifts an `Applicative` function operating on focus to one operating on source.

Declaration

Swift

``public func liftF<F: Applicative>(_ f: @escaping (A) -> Kind<F, B>) -> (S) -> Kind<F, T>``

Parameters

 ``` f ``` Modifying function.

Return Value

Lifted function operating on the source.

• ``` getOption(_:) ```

Retrieves the focus.

Declaration

Swift

``public func getOption(_ s: S) -> Option<A>``

Parameters

 ``` s ``` Source.

Return Value

An optional value that is present if the focus exists.

• ``` getOptional(_:) ```

Retrieves the focus.

Declaration

Swift

``public func getOptional(_ s: S) -> A?``

Parameters

 ``` s ``` Source.

Return Value

An optional value that is present if the focus exists.

• ``` set(_:_:) ```

Obtains a modified source.

Declaration

Swift

``public func set(_ s: S, _ b: B) -> T``

Parameters

 ``` s ``` Source. ``` b ``` Modified focus.

Return Value

Modified source.

• ``` setOption(_:_:) ```

Sets a modified focus.

Declaration

Swift

``public func setOption(_ s: S, _ b: B) -> Option<T>``

Parameters

 ``` s ``` Source. ``` b ``` Modified focus.

Return Value

Optional modified source.

• ``` setOptional(_:_:) ```

Sets a modified focus.

Declaration

Swift

``public func setOptional(_ s: S, _ b: B) -> T?``

Parameters

 ``` s ``` Source. ``` b ``` Modified focus.

Return Value

Optional modified source.

• ``` nonEmpty(_:) ```

Checks if the provided source is non-empty.

Declaration

Swift

``public func nonEmpty(_ s: S) -> Bool``

Parameters

 ``` s ``` Source.

Return Value

Boolean value indicating if the provided source is non-empty.

• ``` isEmpty(_:) ```

Checks if the provided source is empty.

Declaration

Swift

``public func isEmpty(_ s: S) -> Bool``

Parameters

 ``` s ``` Source.

Return Value

Boolean value indicating if the provided source is empty.

• ``` first() ```

Pairs this `PPrism` with another type, placing this as the first element.

Declaration

Swift

``public func first<C>() -> PPrism<(S, C), (T, C), (A, C), (B, C)>``

Return Value

A `PPrism` that operates on tuples where the second argument remains unchanged.

• ``` second() ```

Pairs this `PPrism` with another type, placing this as the second element.

Declaration

Swift

``public func second<C>() -> PPrism<(C, S), (C, T), (C, A), (C, B)>``

Return Value

A `PPrism` that operates on tuples where the first argument remains unchanged.

• ``` modify(_:_:) ```

Modifies the source with the provided function.

Declaration

Swift

``public func modify(_ s: S, _ f: @escaping (A) -> B) -> T``

Parameters

 ``` s ``` Source. ``` f ``` Modifying function.

Return Value

Modified source.

• ``` lift(_:) ```

Lifts a function modifying the focus to one modifying the source.

Declaration

Swift

``public func lift(_ f: @escaping (A) -> B) -> (S) -> T``

Parameters

 ``` f ``` Modifying function.

Return Value

Function that modifies the source.

• ``` modifyOption(_:_:) ```

Optionally modifies the source with a function.

Declaration

Swift

``public func modifyOption(_ s: S, _ f: @escaping (A) -> B) -> Option<T>``

Parameters

 ``` s ``` Source. ``` f ``` Modifying function.

Return Value

Optional modified source.

• ``` liftOption(_:) ```

Lifts a function modifying the focus to a function that optionally modifies the source.

Declaration

Swift

``public func liftOption(_ f: @escaping (A) -> B) -> (S) -> Option<T>``

Parameters

 ``` f ``` Modifying function.

Return Value

Function that optionally modifies the source.

• ``` find(_:_:) ```

Retrieves the focus if it matches a predicate.

Declaration

Swift

``public func find(_ s: S, _ predicate: @escaping (A) -> Bool) -> Option<A>``

Parameters

 ``` s ``` Source. ``` predicate ``` Testing predicate.

Return Value

An optional focus that is present if it matches the predicate.

• ``` exists(_:_:) ```

Checks if the focus matches a predicate.

Declaration

Swift

``public func exists(_ s: S, _ predicate: @escaping (A) -> Bool) -> Bool``

Parameters

 ``` s ``` Source. ``` predicate ``` Testing predicate.

Return Value

Boolean value indicating if the focus matches the predicate.

• ``` all(_:_:) ```

Checks if the focus matches a predicate.

Declaration

Swift

``public func all(_ s: S, _ predicate: @escaping(A) -> Bool) -> Bool``

Parameters

 ``` s ``` Source. ``` predicate ``` Testing predicate.

Return Value

Boolean value indicating if the focus matches the predicate.

• ``` left() ```

Creates the sum of this `PPrism` with another type, placing this as the left side.

Declaration

Swift

``public func left<C>() -> PPrism<Either<S, C>, Either<T, C>, Either<A, C>, Either<B, C>>``

Return Value

A `PPrism` that operates on `Either`s where the right side remains unchanged.

• ``` right() ```

Creates the sum of this `PPrism` with another type, placing this as the right side.

Declaration

Swift

``public func right<C>() -> PPrism<Either<C, S>, Either<C, T>, Either<C, A>, Either<C, B>>``

Return Value

A `PPrism` that operates on `Either`s where the left side remains unchanged.

• ``` compose(_:) ```

Composes this value with a `PPrism`.

Declaration

Swift

``public func compose<C, D>(_ other: PPrism<A, B, C, D>) -> PPrism<S, T, C, D>``

Parameters

 ``` other ``` Value to compose with.

Return Value

A `PPrism` resulting from the sequential application of the two provided optics.

• ``` compose(_:) ```

Composes this value with a `PIso`.

Declaration

Swift

``public func compose<C, D>(_ other: PIso<A, B, C, D>) -> PPrism<S, T, C, D>``

Parameters

 ``` other ``` Value to compose with.

Return Value

A `PPrism` resulting from the sequential application of the two provided optics.

• ``` compose(_:) ```

Composes this value with a `PLens`.

Declaration

Swift

``public func compose<C, D>(_ other: PLens<A, B, C, D>) -> PAffineTraversal<S, T, C, D>``

Parameters

 ``` other ``` Value to compose with.

Return Value

A `AffineTraversal` resulting from the sequential application of the two provided optics.

• ``` compose(_:) ```

Composes this value with a `AffineTraversal`.

Declaration

Swift

``public func compose<C, D>(_ other: PAffineTraversal<A, B, C, D>) -> PAffineTraversal<S, T, C, D>``

Parameters

 ``` other ``` Value to compose with.

Return Value

A `AffineTraversal` resulting from the sequential application of the two provided optics.

• ``` compose(_:) ```

Composes this value with a `PSetter`.

Declaration

Swift

``public func compose<C, D>(_ other: PSetter<A, B, C, D>) -> PSetter<S, T, C, D>``

Parameters

 ``` other ``` Value to compose with.

Return Value

A `PSetter` resulting from the sequential application of the two provided optics.

• ``` compose(_:) ```

Composes this value with a `Fold`.

Declaration

Swift

``public func compose<C>(_ other: Fold<A, C>) -> Fold<S, C>``

Parameters

 ``` other ``` Value to compose with.

Return Value

A `Fold` resulting from the sequential application of the two provided optics.

• ``` compose(_:) ```

Composes this value with a `PTraversal`.

Declaration

Swift

``public func compose<C, D>(_ other: PTraversal<A, B, C, D>) -> PTraversal<S, T, C, D>``

Parameters

 ``` other ``` Value to compose with.

Return Value

A `PTraversal` resulting from the sequential application of the two provided optics.

• ``` asAffineTraversal ```

Converts this value into a AffineTraversal.

Declaration

Swift

``public var asAffineTraversal: PAffineTraversal<S, T, A, B> { get }``
• ``` asSetter ```

Converts this value into a PSetter.

Declaration

Swift

``public var asSetter: PSetter<S, T, A, B> { get }``
• ``` asFold ```

Converts this value into a Fold.

Declaration

Swift

``public var asFold: Fold<S, A> { get }``
• ``` asTraversal ```

Converts this value into a PTraversal.

Declaration

Swift

``public var asTraversal: PTraversal<S, T, A, B> { get }``