Capstone.Vendor.Sourceror.Code.Map (Capstone v0.33.3)

Copy Markdown View Source

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

mappify(list, value)

Puts a value into nested maps at the given path.

Examples

iex> Capstone.Vendor.Sourceror.Code.Map.mappify([:foo, :bar], 1)
{:%{}, [], [{{:__block__, [format: :keyword], [:foo]}, {:%{}, [], [{{:__block__, [format: :keyword], [:bar]}, 1}}]}}]}

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

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

See also set_map_key/4.

set_map_key(zipper, key, value, updater)

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

Examples

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

See also put_in_map/4.