Last active
August 9, 2020 03:26
-
-
Save glinesbdev/c84376e4886b456ed35201a3dae83f71 to your computer and use it in GitHub Desktop.
Reducing Reducers - Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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