Skip to main content

reverse

Function reverse 

Source
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 IsoOptic from B, A to T, S (when the inner optic implements IsoOptic)
  • A ReviewOptic from B -> A focusing on T -> S (when the inner optic implements LensOptic - covers isos and lenses)
  • A GetterOptic from A to S (when the inner optic implements PrismOptic with simple types S = T, A = B)
  • A FoldOptic from A to S (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);