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 aPLens
.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 aPIso
.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 aPPrism
.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 aPAffineTraversal
.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 aPTraversal
.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. -
Initializes a
PLens
with itsget
andset
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.
-
Obtains the focus of this lens.
Declaration
Swift
public func get(_ s: S) -> A
Parameters
s
Source.
Return Value
Focus for the provided source.
-
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.
-
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.
-
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. -
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. -
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. -
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. -
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. -
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. -
Obtains a
PAffineTraversal
from this lens.Declaration
Swift
public var asAffineTraversal: PAffineTraversal<S, T, A, B> { get }
-
Obtains a
PTraversal
from this lens.Declaration
Swift
public var asTraversal: PTraversal<S, T, A, B> { get }
-
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.
-
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.
-
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.
-
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.
-
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.