I have this nasty piece of code that could be much simplified using put_in if there wasn't a list in a nested data structure.
This is what I have working:
spec = %{
commands: [
%{name: "abc", ...},
%{name: "def", ...},
...
]
}
...
# we have {key, val} defined
index = Enum.find_index(spec.commands, &(&
1.name == key))
if !index, do: config_error("Unrecognized command: #{key}")
cmdspec = Enum.at(spec.commands, index)
Map.update!(spec, :commands, fn list ->
List.replace_at(list, index, Map.put(cmdspec, :action, val))
end)
What I would love to be able to write instead:
index = Enum.find_index(spec.commands, &(&
1.name == key))
if !index, do: config_error("Unrecognized command: #{key}")
put_in(spec.commands<[index]>[:action], val)
where <[index]> is a hypothetical indexing operation.
I could actually make the following work
put_in(spec.commands[index][:action], val)
if I converted 'commands' into a map that looks like this: %{ 0 => %{...}, 1 => %{...} }. But that seems too ugly, I'd rather go with the first approach then.
Any comments?
---
Best regards
Alexei Sholik