View Source Sourceror.Code.Map (Sourceror v1.11.0)

Utilities for working with maps.

Summary

Functions

Puts a value into nested maps at the given path.

Puts a value at a path into a map, calling updater on the zipper at the value if the key is already present.

Puts a key into a map, calling updater on the zipper at the value if the key is already present.

Functions

Puts a value into nested maps at the given path.

Examples

iex> Sourceror.Code.Map.mappify([:foo, :bar], 1)
{:%{}, [], [{{:__block__, [format: :keyword], [:foo]}, {:%{}, [], [{{:__block__, [format: :keyword], [:bar]}, 1}}]}}]}
Link to this function

put_in_map(zipper, path, value, updater \\ nil)

View Source
@spec put_in_map(
  Sourceror.Zipper.t(),
  [term()],
  term(),
  (Sourceror.Zipper.t() -> {:ok, Sourceror.Zipper.t()} | :error) | nil
) :: {:ok, Sourceror.Zipper.t()} | :error

Puts a value at a path into a map, calling updater on the zipper at the value if the key is already present.

Examples

iex> zipper = Sourceror.parse_string!("%{foo: 1}") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Map.put_in_map(zipper, [:bar], 2)
iex> Sourceror.to_string(zipper.node) |> String.contains?("bar:")
true

See also set_map_key/4.

Link to this function

set_map_key(zipper, key, value, updater)

View Source
@spec set_map_key(Sourceror.Zipper.t(), term(), term(), (Sourceror.Zipper.t() ->
                                                     {:ok, Sourceror.Zipper.t()}
                                                     | :error)) ::
  {:ok, Sourceror.Zipper.t()} | :error

Puts a key into a map, calling updater on the zipper at the value if the key is already present.

Examples

iex> zipper = Sourceror.parse_string!("%{foo: 1}") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Map.set_map_key(zipper, :bar, 2, fn z -> {:ok, z} end)
iex> Sourceror.to_string(zipper.node) |> String.contains?("bar:")
true

See also put_in_map/4.