destructure v0.3.0 Destructure View Source
Provides helpers for destructuring Elixir data structures. See the d/1
macro
for more information.
Link to this section Summary
Link to this section Functions
Easy destructuring of map
, structs
, keyword
, with atom keys.
String keys are supported via a special syntax d(s%{param})
.
Examples
The macro can be used in simple match statements:
iex> d(%{name}) = %{name: "Joe"}
...> name
"Joe"
Or in case/for/with statements.
iex> case %{name: "Mike"} do
...> d%{name} -> name
...> end
"Mike"
It can be used inside a complex match:
iex> %{body: d%{name}} = %{body: %{name: "Robert"}}
...> name
"Robert"
With multiple keys:
iex> d(%{first, last}) = %{first: "Daniel", last: "Berkompas"}
...> {first, last}
{"Daniel", "Berkompas"}
With multiple keys and custom variable naming:
iex> d(%{first, last, email: mail}) = %{first: "Daniel", last: "Berkompas", email: "top@secret.com"}
...> {first, last, mail}
{"Daniel", "Berkompas", "top@secret.com"}
For Maps with String Keys:
iex> s(%{name}) = %{"name" => "Daniel Berkompas"}
...> name
"Daniel Berkompas"
With multiple keys:
iex> s(%{name, email}) = %{"name" => "Daniel Berkompas", "email" => "top@secret.com"}
...> {name, email}
{"Daniel Berkompas", "top@secret.com"}
With multiple keys and custom variable naming:
iex> s(%{name, "email" => mail}) = %{"name" => "Daniel Berkompas", "email" => "top@secret.com"}
...> {name, mail}
{"Daniel Berkompas", "top@secret.com"}
For structs:
iex> d(%Person{name}) = %Person{name: "Daniel Berkompas"}
...> name
"Daniel Berkompas"
With multiple keys:
iex> d(%Person{name, email}) = %Person{name: "Daniel Berkompas", email: "top@secret.com"}
...> {name, email}
{"Daniel Berkompas", "top@secret.com"}
With multiple keys and custom variable naming:
iex> d(%Person{name, email: mail}) = %Person{name: "Daniel Berkompas", email: "top@secret.com"}
...> {name, mail}
{"Daniel Berkompas", "top@secret.com"}
For keyword lists:
iex> d([name]) = [name: "Daniel"]
...> name
"Daniel"
With multiple keys:
iex> d([first, last]) = [first: "Daniel", last: "Berkompas"]
...> [first, last]
["Daniel", "Berkompas"]
With multiple keys and custom assignment:
iex> d([first, last, email: mail]) = [first: "Daniel", last: "Berkompas", email: "top@secret.com"]
...> [first, last, mail]
["Daniel", "Berkompas", "top@secret.com"]
And in function definitions:
iex> defmodule Test do
...> import Destructure
...> def test(d%{name}) do
...> name
...> end
...> end
...> Test.test(%{name: "Daniel"})
"Daniel"