asLens property

Lens<A, B> get asLens

Downgrades this isomorphism to a Lens.

Every isomorphism is a lens (you can always get and set), but not every lens is an isomorphism. This returns a lens where:

  • get uses to to extract the B value from an A
  • set uses from to reconstruct the A value from a B

The old value is ignored during set (since isomorphisms can fully reconstruct from just the new part).

Example:

final iso = Iso(to: (s) => int.parse(s), from: (i) => i.toString());
final lens = iso.asLens;
assert(lens.get('123') == 123);
assert(lens.set('old', 456) == '456');

See also: Lens

Implementation

Lens<A, B> get asLens => Lens(get: to, set: (_, b) => from(b));