Skip to content

Instantly share code, notes, and snippets.

@h4cc
Created April 11, 2018 12:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h4cc/d16487fc4e44fe7085d3971c77c1d3c1 to your computer and use it in GitHub Desktop.
Save h4cc/d16487fc4e44fe7085d3971c77c1d3c1 to your computer and use it in GitHub Desktop.
Helper function for put_in for populating data in deep maps.
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
@am-kantox
Copy link

FWIW, less elegant but working for keywords and lists solution: https://hexdocs.pm/iteraptor/Iteraptor.Extras.html#bury/4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment