# PLens

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

A Lens (or Functional Reference) is an optic that can focus into a structure for getting, setting or modifying the focus (target).

A (polymorphic) PLens is useful when setting or modifying a value for a constructed type.

A PLens can be seen as a pair of functions: - `get: (S) -> A` meaning we can focus into `S` and extract an `A`. - `set: (B, S) -> T` meaning we can focus into an `S` and set a value `B` for a target `A` and obtain a modified source.

The type arguments are: - `S` is the source of a PLens. - `T` is the modified source of a PLens. - `A` is the focus of a PLens. - `B` is the modified focus of a PLens.

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

Composes a `PLens` with a `PLens`.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

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

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

Composes a `PLens` with a `PIso`.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

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

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

Composes a `PLens` with a `Getter`.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

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

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

Composes a `PLens` with a `PPrism`.

#### Declaration

Swift

``public static func + <C, D>(lhs: PLens<S, T, A, B>, rhs: PPrism<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 `PAffineTraversal` resulting from the sequential application of the two provided optics.

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

Composes a `PLens` with a `PAffineTraversal`.

#### Declaration

Swift

``public static func + <C, D>(lhs: PLens<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 `PAffineTraversal` resulting from the sequential application of the two provided optics.

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

Composes a `PLens` with a `PSetter`.

#### Declaration

Swift

``public static func + <C, D>(lhs: PLens<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 `PLens` with a `Fold`.

#### Declaration

Swift

``public static func + <C>(lhs: PLens<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 `PLens` with a `PTraversal`.

#### Declaration

Swift

``public static func + <C, D>(lhs: PLens<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(get:set:) ```

Initializes a `PLens` with its `get` and `set` functions.

#### Declaration

Swift

``public init(get: @escaping (S) -> A, set: @escaping (S, B) -> T)``

#### Parameters

 ``` get ``` Getter function for the lens. ``` set ``` Setter function for the lens.
• ``` get(_:) ```

Obtains the focus of this lens.

#### Declaration

Swift

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

#### Parameters

 ``` s ``` Source.

#### Return Value

Focus for the provided source.

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

Sets the focus of this lens.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

Modified source.

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

Modifies the focus of this lens using a `Functor` function.

#### Declaration

Swift

``public func modifyF<F: Functor>(_ 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 `Functor`.

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

Lifts a `Functor` function between the targets to a function between the sources.

#### Declaration

Swift

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

#### Parameters

 ``` f ``` Modifying function between the targets.

#### Return Value

A `Functor` function between the sources.

• ``` choice(_:) ```

Joins two lenses with the same focus.

#### Declaration

Swift

``public func choice<S1, T1>(_ other: PLens<S1, T1, A, B>) -> PLens<Either<S, S1>, Either<T, T1>, A, B>``

#### Parameters

 ``` other ``` A lens with the same focus as this one.

#### Return Value

A lens whose source is a pair of the two original lenses.

• ``` split(_:) ```

Pairs two disjoint lenses.

#### Declaration

Swift

``public func split<S1, T1, A1, B1>(_ other: PLens<S1, T1, A1, B1>) -> PLens<(S, S1), (T, T1), (A, A1), (B, B1)>``

#### Parameters

 ``` other ``` A disjoint lens.

#### Return Value

A lens that operates on tuples of the original and parameter sources and targets.

• ``` first() ```

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

#### Declaration

Swift

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

#### Return Value

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

• ``` second() ```

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

#### Declaration

Swift

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

#### Return Value

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

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

Composes this lens with a `PLens`.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

A `PLens` resulting from the sequential application of the two optics.

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

Composes this lens with a `PIso`.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

A `PIso` resulting from the sequential application of the two optics.

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

Composes this lens with a `Getter`.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

A `Getter` resulting from the sequential application of the two optics.

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

Composes this lens with a `PPrism`.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

A `PAffineTraversal` resulting from the sequential application of the two optics.

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

Composes this lens with a `PAffineTraversal`.

#### 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 `PAffineTraversal` resulting from the sequential application of the two optics.

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

Composes this lens 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 optics.

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

Composes this lens 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 optics.

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

Composes this lens with a `PLens`.

#### 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 optics.

• ``` asGetter ```

Obtains a `Getter` from this lens.

#### Declaration

Swift

``public var asGetter: Getter<S, A> { get }``
• ``` asAffineTraversal ```

Obtains a `PAffineTraversal` from this lens.

#### Declaration

Swift

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

Obtains a `PSetter` from this lens.

#### Declaration

Swift

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

Obtains a `Fold` from this lens.

#### Declaration

Swift

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

Obtains a `PTraversal` from this lens.

#### Declaration

Swift

``public var asTraversal: PTraversal<S, T, A, B> { get }``
• ``` modify(_:_:) ```

Modifies the focus of this lens 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 that modifies the targets, to a function that modifies the sources.

#### Declaration

Swift

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

#### Parameters

 ``` f ``` Modifying function.

#### Return Value

Function that modifies sources.

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

Retrieves the target of this lens if it matches a predicate.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

An optional value that is present if the source matches the predicate.

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

Checks if the target of this lens matches a predicate.

#### Declaration

Swift

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

#### Parameters

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

#### Return Value

Boolean value indicating if the target of the provided source matches the predicate.

• ``` ask() ```

Extracts the value viewed through the get function.

#### Declaration

Swift

``public func ask() -> Reader<S, A>``

#### Return Value

A `Reader` from source to target.

• ``` toReader() ```

Extracts the value viewed through the get function.

#### Declaration

Swift

``public func toReader() -> Reader<S, A>``

#### Return Value

A `Reader` from source to target.

• ``` asks(_:) ```

Extracts the value viewed through the get function and applies the provided function to it.

#### Declaration

Swift

``public func asks<C>(_ f: @escaping (A) -> C) -> Reader<S, C>``

#### Parameters

 ``` f ``` Function to apply to the focus.

#### Return Value

A `Reader` from source to the target modified by the provided function.

• ``` extract() ```

Extracts the focus view through the `PLens`.

#### Declaration

Swift

``public func extract() -> State<S, A>``

#### Return Value

A `State` of the source and target.

• ``` toState() ```

Extracts the focus view through the `PLens`.

#### Declaration

Swift

``public func toState() -> State<S, A>``

#### Return Value

A `State` of the source and target.

• ``` extractMap(_:) ```

Extracts the focus view through the `PLens` and applies the provided function to it.

#### Declaration

Swift

``public func extractMap<C>(_ f: @escaping (A) -> C) -> State<S, C>``

#### Return Value

A `State` of the source and target, modified by the provided function.

• ``` merge(_:) ```

Combine this lens with another with the same source but different focus.

#### Declaration

Swift

``func merge<AA, BB>(_ other: PLens<S, S, AA, BB>) -> PLens<S, S, (A, AA), (B, BB)>``

#### Parameters

 ``` other ``` A lens with the same source but different focus.

#### Return Value

A lens that lets us focus on the two foci at the same time.