modify method

Whole modify(
  1. Whole whole,
  2. Part f(
    1. Part
    )
)

Applies a transformation to the focused Part and updates the Whole.

This is a convenience method that combines get and set:

  1. Extracts the current part: get(whole)
  2. Applies the function: f(part)
  3. 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)));