pub fn reverse<'a, PointerBrand, S, T, A, B, O>(
optic: O,
) -> ReversedOptic<'a, PointerBrand, S, T, A, B, O>where
PointerBrand: ToDynCloneFn,
S: 'a,
T: 'a,
A: 'a,
B: 'a,Expand description
Reverses an optic using the Reverse profunctor.
Given an optic from S -> T focusing on A -> B, produces a reversed optic
that can be used as:
- An
IsoOpticfromB, AtoT, S(when the inner optic implementsIsoOptic) - A
ReviewOpticfromB -> Afocusing onT -> S(when the inner optic implementsLensOptic- covers isos and lenses) - A
GetterOpticfromAtoS(when the inner optic implementsPrismOpticwith simple typesS = T, A = B) - A
FoldOpticfromAtoS(same conditions as getter)
Corresponds to PureScript’s re t = unwrap (t (Reverse identity)).
§Type Signature
forall PointerBrand S T A B O. ToDynCloneFn PointerBrand => O -> ReversedOptic PointerBrand S T A B O
§Type Parameters
'a: The lifetime of the values.PointerBrand: The cloneable function pointer brand.S: The source type of the original optic.T: The target type of the original optic.A: The focus source type of the original optic.B: The focus target type of the original optic.O: The inner optic type.
§Parameters
optic: The optic to reverse.
§Returns
A ReversedOptic wrapping the inner optic.
§Examples
use fp_library::{
brands::{
optics::*,
*,
},
classes::optics::ReviewOptic,
types::optics::{
LensPrime,
Tagged,
reverse,
},
};
// Create a lens for (i32, String) focusing on the first element
let fst: LensPrime<RcBrand, (i32, String), i32> =
LensPrime::from_view_set(|(x, _)| x, |((_, s), x)| (x, s));
// Reverse it to get a ReviewOptic
let reversed = reverse::<RcBrand, _, _, _, _, _>(fst);
// Use ReviewOptic: given a Tagged<(i32, String), (i32, String)>, produce Tagged<i32, i32>
let result = ReviewOptic::evaluate(&reversed, Tagged::new((42, "hello".to_string())));
assert_eq!(result.0, 42);