Skip to content

Instantly share code, notes, and snippets.

@h4cc h4cc/helper.ex
Created Apr 11, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

am-kantox commented Apr 11, 2018

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
You can’t perform that action at this time.