Recursion schemes as composable optics, built on the core optic surface.
- cata is a
Getter[S, A] driven by Plated[S] — the structural fold, generalising Plated.transform from S => S to S => A.
- ana is a
Review[S, Seed] — the unfold (build S from a seed), taking a Coalg.
- hylo is a fused
Getter[Seed, A] — refold with no intermediate S built.
Because they produce core optic types, they compose with the rest of the optic algebra: someLens.andThen(cata(alg)), and the materializing ana(…).cross(cata(…)) (via the core Optic.cross combinator) — the latter equal to hylo on the same computation (the hylo law).
The fold schemes (cata / hylo) take an algebra (N, PSVec[R]) => R — a node plus its already-folded children (paramorphism-flavored). The build scheme (ana) takes a Coalg, the canonical anamorphism shape: a seed yields its child seeds together with how to assemble the node. Both run on one stack-safe engine: a < 512-deep on-stack fast path (no heap frames) that falls back, per deep subtree, to a heap ArrayDeque machine — the same hybrid as Plated.transform. Shallow trees pay no frame allocation; arbitrarily deep ones stay stack-safe.