DeferredConfig v0.1.1 ReplacingWalk

A hastily constructed replacing walk for use with DeferredConfig; not very performant, but for transforming data in options and config, can be convenient.

Summary

Functions

Recursive replacing walk that uses recognize and transform functions to return a transformed version of arbitrary data

Functions

walk(m, recognize, transform)

Recursive replacing walk that uses recognize and transform functions to return a transformed version of arbitrary data.

iex> ReplacingWalk.walk [1, 2, 3], &(&1 == 2), &(&1 * &1)
[1,4,3]

iex> ReplacingWalk.walk( [1, [2, [3, 2]]],
...>                        &(&1 == 2), 
...>                        &(&1 * &1) 
...> )
[1,[4, [3, 4]]]

It works for Maps:

iex> ReplacingWalk.walk %{2 => 1, 1 => 2}, &(&1 == 2), &(&1 * &1)
%{4 => 1, 1 => 4}

Structs in general are considered as leaf nodes; we support structs that implement Enumerable, but **currently we expect their Enumerable implementation to work like a Map. If you feed this an Enumerable struct that doesn’t iterate like Map — ie, doesn’t iterate over {k, v} — it will die. (See an example in tests).

We may change that behavior in the future — either removing support for arbitrary Enumerables, or provision another protocol that can be implemented to make a data type replacing-walkable.

Created quickly for :deferred_config, so it’s probably got some holes; tests that break it are welcome.