Last active
June 23, 2018 04:04
-
-
Save mkreyman/76e0637e8c88e4144ea98fac33a3a64a to your computer and use it in GitHub Desktop.
Creating a struct from a map with string keys
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
def to_struct(kind, attrs) do | |
struct = struct(kind) | |
Enum.reduce Map.to_list(struct), struct, fn {k, _}, acc -> | |
case Map.fetch(attrs, Atom.to_string(k)) do | |
{:ok, v} -> %{acc | k => v} | |
:error -> acc | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment