Created
April 11, 2018 12:18
-
-
Save h4cc/d16487fc4e44fe7085d3971c77c1d3c1 to your computer and use it in GitHub Desktop.
Helper function for put_in for populating data in deep maps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Helper do | |
# Helper function for setting values deep in a map. | |
# Thanks @michalmuskala! | |
# https://elixirforum.com/t/put-update-deep-inside-nested-maps-and-auto-create-intermediate-keys/7993/8 | |
@doc """ | |
Will set value at for keys deep inside data. | |
iex> Helper.put_in_deep(%{a: %{}}, [:a, :b, :c], 42) | |
%{a: %{b: %{c: 42}}} | |
""" | |
defp put_in_deep(data, keys, value) when is_list(keys) do | |
put_in(data, Enum.map(keys, &Access.key(&1, %{})), value) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FWIW, less elegant but working for keywords and lists solution: https://hexdocs.pm/iteraptor/Iteraptor.Extras.html#bury/4