Skip to content

Instantly share code, notes, and snippets.

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

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!
@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,, &Access.key(&1, %{})), value)

This comment has been minimized.

Copy link

am-kantox commented Apr 11, 2018

FWIW, less elegant but working for keywords and lists solution:

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.