--- id: kw-reducer name: reducer category: structure kind: keyword tokens: reducer sig: reducer State on Action(st: mut State, reads..., a: Action) { ... } / reducer Record in State.table on Action(r: mut Row, reads..., a: Action) { ... } tip: Applies an action to one state, or to the one table row its @Target names; it may read others. order: 62 --- A reducer writes exactly one state when its action is dispatched: the first parameter is that state, mut, the last is the action, and any states between are read-only. A change that must touch several states in a set order is a chain: a reducer dispatches the next action. ```ludic state Pos { x: int = 0 } action Move { dx: int = 0 } reducer Pos on Move(p: mut Pos, a: Move) { p.x = p.x + a.dx } ``` Written in State.table, a reducer runs for the one row of that Table<Record> the action's @Target field names: it is handed a Row<Record>, reaches r.rec and r.h, and changes a field a column mirrors (@Column) only through a @RowVerb. A handle whose row is gone runs nothing. ```ludic # doc-check: skip — needs ludic.base's Table reducer Deer in Herd.deer on Spook(r: mut Row, a: Spook) { r.rec.fear += a.by } ```