Skip to content

Commit

Permalink
Add support for keyword list using square brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Fauzan committed Jun 17, 2017
1 parent 9cdce66 commit d22066e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/destructure.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ defmodule Destructure do
iex> d({name}) = [name: "Daniel"]
...> name
"Daniel"
iex> d([foo]) = [foo: "bar"]
...> foo
"bar"
With multiple keys:
Expand Down Expand Up @@ -116,6 +119,11 @@ defmodule Destructure do
[pattern(first), pattern(second)]
end

# Handle keyword list using square bracket
defmacro d(list) when is_list(list) do
Enum.map(list, &pattern/1)
end

defp pattern({key, _, _} = variable) do
{key, variable}
end
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
%{"earmark": {:hex, :earmark, "1.0.2", "a0b0904d74ecc14da8bd2e6e0248e1a409a2bc91aade75fcf428125603de3853", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.14.3", "e61cec6cf9731d7d23d254266ab06ac1decbb7651c3d1568402ec535d387b6f7", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]}}
%{"earmark": {:hex, :earmark, "1.0.2", "a0b0904d74ecc14da8bd2e6e0248e1a409a2bc91aade75fcf428125603de3853", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.14.3", "e61cec6cf9731d7d23d254266ab06ac1decbb7651c3d1568402ec535d387b6f7", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}}

0 comments on commit d22066e

Please sign in to comment.