Electric.Replication.Eval.Walker (electric v1.1.0)

View Source

Summary

Functions

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.

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

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

Types

children_map()

@type children_map() :: %{required(atom()) => nil | struct() | [struct()]}

Functions

accumulating_fold(tree, fold_fn, acc_fn, acc, ctx \\ [])

@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 ->
                 {:ok, result} | {:error, any()}),
     acc_fn: (struct(), result, children_map(), acc, context ->
                {: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(tree, fold_fn, ctx \\ [])

@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 ->
                 {: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(tree, reduce_fn, acc, ctx \\ [])

@spec reduce(
  target :: struct() | nil,
  reduce_fn :: reduce_fn,
  acc :: acc,
  ctx :: context
) ::
  {:error, any()} | {:ok, acc}
when acc: any(),
     context: any(),
     reduce_fn: (struct(), acc, context -> {: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.