# Adapt


Handler context transformation. Contravariant on context, covariant on continuation.

## `adapt`

_adapt: lift a child-state handler through a `get`/`set` lens onto parent state; contravariant on context extraction, covariant on result incorporation._

```
adapt : { get : P -> S, set : P -> S -> P } -> Handler S -> Handler P
```

Transform a handler's state context.

Wraps a handler that works with child state `S` so it works with
parent state `P`, using a `get`/`set` lens. Propagates both
`resume` and `abort`.

```nix
counterHandler = { param, state }: { resume = null; state = state + param; };
adapted = adapt { get = s: s.counter; set = s: c: s // { counter = c; }; } counterHandler;
# adapted now works with { counter = 0; logs = []; } state
```

## `adaptHandlers`

_adaptHandlers: lift an entire handler set through the same `get`/`set` lens; equivalent to mapping `adapt` over each handler in the attrset._

```
adaptHandlers : { get : P -> S, set : P -> S -> P } -> Handlers S -> Handlers P
```

Adapt an entire handler set (attrset of handlers) to a different state context.
Applies the same `get`/`set` lens to every handler in the set.

```nix
stateHandlers = {
  get = { param, state }: { value = state; inherit state; };
  put = { param, state }: { value = null; state = param; };
};
adapted = adaptHandlers { get = s: s.data; set = s: d: s // { data = d; }; } stateHandlers;
```

