Skip to content

Instantly share code, notes, and snippets.

@gvaughn
Last active May 28, 2019 03:54
Show Gist options
  • Save gvaughn/497377d4195818c7e4c3caf22158c21e to your computer and use it in GitHub Desktop.
Save gvaughn/497377d4195818c7e4c3caf22158c21e to your computer and use it in GitHub Desktop.
Elixir get_in with nested maps and lists
defmodule DeeplyNested do
def get_nat_ip(input) do
steps = [first_map_with_key("accessConfigs"),
first_map_with_key("natIP")
]
get_in(input, steps)
end
defp first_map_with_key(key) do
fn :get, data, next ->
Enum.find_value(data, fn %{^key => val} -> next.(val) end)
end
end
end
input = [%{"accessConfigs" => [%{"kind" => "compute#accessConfig",
"name" => "External NAT", "natIP" => "146.148.23.208",
"type" => "ONE_TO_ONE_NAT"}]}]
IO.inspect DeeplyNested.get_nat_ip(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment