Skip to content

Instantly share code, notes, and snippets.

@glinesbdev
Last active August 9, 2020 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glinesbdev/c84376e4886b456ed35201a3dae83f71 to your computer and use it in GitHub Desktop.
Save glinesbdev/c84376e4886b456ed35201a3dae83f71 to your computer and use it in GitHub Desktop.
Reducing Reducers - Elixir
defmodule Mapper do
def extract_associations(%{} = game) do
Enum.reduce(map, %{}, fn {key, value}, item ->
cond do
is_map(value) ->
put_map(item, key, extract_map_data(value))
is_list(value) ->
list_data = Enum.reduce(value, [], &[extract_map_data(&1) | &2])
put_map(item, key, list_data)
true ->
put_map(item, key, value)
end
end)
end
defp extract_map_data(map), do: Enum.reduce(map, %{}, fn {k, v}, acc -> put_map(acc, k, v) end
defp put_map(map, key, value), do: Map.put(map, String.to_existing_atom(key), value)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment