Created
September 30, 2015 20:43
-
-
Save JoelPM/a7768867aeb69a1b8259 to your computer and use it in GitHub Desktop.
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 Struct do | |
defp filter_nil({_,nil}), do: false | |
defp filter_nil(_), do: true | |
def destruct(%{:__struct__ => _} = v), do: Map.from_struct(v) |> destruct | |
def destruct(v) when is_list(v), do: Enum.map(v, &destruct/1) | |
def destruct(v) when is_map(v), do: Enum.filter_map(v, &filter_nil/1, &destruct/1) |> Enum.into %{} | |
def destruct({k,v}), do: {k,destruct(v)} | |
def destruct(v), do: v | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment