Skip to content

danielberkompas/destructure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Destructure

Hex.pm Build Status

Adds Javascript-style destructuring to Elixir. When working with a map, instead of writing match operation like this:

def full_name(%{first_name: first_name, last_name: last_name}) do
  "#{first_name} #{last_name}"
end

You can write:

import Destructure

def full_name(d%{first_name, last_name}) do
  "#{first_name} #{last_name}"
end

It also works with structs and keyword.

import Destructure

def full_name(d%Person{first_name, last_name}) do
  "#{first_name} #{last_name}"
end
def full_name(d[first_name, last_name]) do
  "#{first_name} #{last_name}"
end

You can also do it in a case statement.

case post(url, data) do
  {:ok, d%{body}} -> # instead of {:ok, %{body: body}}
    # use body variable
  _other ->
    # ...
end

Unlike Javascript, you can still bind custom variables:

d(%{first, last, email: mail}) = %{...}

See the Hex Documentation for more details.

Installation

Add destructure to your list of dependencies in mix.exs:

def deps do
  [{:destructure, "~> 0.2.3"}]
end

About

Javascript-style destructuring for Elixir

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published