# `Electric.Replication.Eval.Walker`
[🔗](https://github.com/electric-sql/electric/tree/%40core/sync-service%401.6.2/packages/sync-service/lib/electric/replication/eval/walker.ex#L14)

# `children_map`

```elixir
@type children_map() :: %{required(atom()) =&gt; nil | struct() | [struct()]}
```

# `accumulating_fold`

```elixir
@spec accumulating_fold(
  target :: struct() | nil,
  fold_fn :: fold_fn,
  acc_fn :: acc_fn,
  acc :: acc,
  ctx :: context
) :: {:error, any()} | {:ok, {result | nil, acc}}
when acc: any(),
     context: any(),
     result: any(),
     fold_fn: (struct(), children_map(), acc, context -&gt;
                 {:ok, result} | {:error, any()}),
     acc_fn: (struct(), result, children_map(), acc, context -&gt;
                {:ok, acc} | {:error, any()})
```

Given a `Electric.Walkable` structure, visit every node and apply the `fold_fn` to it, then apply the `acc_fn` to the result and the accumulated value to
get a new structure.

`fold_fn` is called with the current node, the result of processing the children nodes, the accumulated value and the context.
Result of processing children nodes is a map with the same keys as the node, but with replaced children instead of originals.
This function is expected to return a replacement for the current node, and that replacement will be propagated to the parent node.

`acc_fn` is called with the current node, the result of processing the current node, the result of processing the children nodes, the accumulated value and the context.
This function is expected to return a new accumulated value.

Both `fold_fn` and `acc_fn` are expected to return an ok tuple, or an error tuple, any other value will raise an error.
Returning the error tuple will halt the traversal.

Tree traversal is depth-first, with accumulator being updated after each node is processed. Next nodes will see the updated accumulator.

This function takes an optional `ctx` argument, which is passed to `fold_fn` and `acc_fn` as the last argument.

# `fold`

```elixir
@spec fold(target :: struct() | nil, fold_fn :: fold_fn, ctx :: context) ::
  {:error, any()} | {:ok, result}
when context: any(),
     result: any(),
     fold_fn: (struct(), children_map(), context -&gt;
                 {:ok, result} | {:error, any()})
```

Given a `Electric.Walkable` structure, visit every node and apply the `fold_fn` to it to get a new structure.

`fold_fn` is called with the current node, the result of processing the children nodes and the context.
Result of processing children nodes is a map with the same keys as the node, but with replaced children instead of originals.
This function is expected to return a replacement for the current node, and that replacement will be propagated to the parent node.
Returning the error tuple will halt the traversal.

`fold_fn` is expected to return an ok tuple, or an error tuple, any other value will raise an error.

This function takes an optional `ctx` argument, which is passed to `fold_fn` as the last argument.

# `reduce`

```elixir
@spec reduce(target :: struct() | nil, reduce_fn, acc, context) ::
  {:error, any()} | {:ok, acc}
when acc: any(),
     context: any(),
     reduce_fn: (struct(), acc, context -&gt; {:ok, acc} | {:error, any()})
```

Given a `Electric.Walkable` structure, visit every node and apply the `reduce_fn` to it to get an accumulated value.

`reduce_fn` is called with the current node, the accumulated value and the context.
This function is expected to return an ok tuple, or an error tuple, any other value will raise an error.
Returning the error tuple will halt the traversal.

This function takes an optional `ctx` argument, which is passed to `reduce_fn` as the last argument.

# `reduce!`

```elixir
@spec reduce!(target :: struct() | nil, reduce_fn, acc, context) :: acc
when acc: any(),
     context: any(),
     reduce_fn: (struct(), acc, context -&gt; {:ok, acc} | {:error, any()})
```

Same as `reduce/4`, but raises on error instead of returning an error tuple.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
