modify method
Whole
modify(
- Whole whole,
- Part f(
- Part
Applies a transformation to the focused Part and updates the Whole.
This is a convenience method that combines get and set:
- Extracts the current part:
get(whole) - Applies the function:
f(part) - Sets the result back:
set(whole, result)
Useful for incremental updates to nested structures.
Example:
final nameLens = Lens(
get: (person) => person.name,
set: (person, newName) => person.copyWith(name: newName),
);
final updated = nameLens.modify(person, (name) => name.toUpperCase());
Implementation
Whole modify(Whole whole, Part Function(Part) f) => set(whole, f(get(whole)));