Elixir: Cheatsheet

1. Nil Catcher

UndefinedFunctionError
function nil.value/0 is undefined

Add this to your helper or base view:

def get(map, fields) when is_list(fields),
do: Enum.reduce(fields, map, fn field, map -> if map, do: Map.get(map, field) end)

Or this:

def get(map, fields) when is_list(fields),
do: Kernel.get_in(map, fields)
reference: Kernel.get_in/2

To use it, just do this:

<%= get(@music, ~w(singer name)a) %>
This will never return an UndefinedFunctionError

2. Shortened List Manipulations

data 
|> Enum.map(fn f -> %{name: f.name} end)
data
|> Enum.filter(fn f -> f.status == "active" end)
data
|> Enum.find(fn f -> f.id == id end)

You can try this:

data 
|> Enum.map(& %{name: &1.name } end)
data
|> Enum.filter(& &1.status == "active")
data
|> Enum.find(& &1.id == sample_id)
Cool right ??? 😅

3. Shorthand Conditions

data = %{active: true}if data.active do
"Active"
else
"Not Active"
end
# Shorthand
if data.active, do: "Active", else: "Not Active"

You can also use this on your methods:

Instead of doing:def is_active() do
...
end
You can try doing: def is_active(), do: ...

Example:

data = %{active: true}def is_active(data) do
if data.active do
"Active"
else
"Not Active"
end
end
# The code above is not safe, if data is nil it will return an UndefinedFunctionError, You can use pattern matching with shorthand method:def is_active(%{active: true}), do: "Active"
def is_active(_), do: "Not Active"
# Whats great here is that, no matter what type of value you pass on the method(list, nil or an atom), as long as it does not match the first method, it'll always go to the default method catcher, returning "Not Active".

4. Guards and Pattern Match

data = %{name: "alvin"}# This is a guard
def cool_method(data) when not is_nil(data), do: ...
def cool_method(_), do: ...
or # This is a pattern match
def cool_method(%{name: "alvin"}), do: ...
def cool_method(data), do: ...
# Combination of both
def cool_method(%{name: name} = data) when not is_nil(name), do: ...
def cool_method(_), do: ...

--

--

--

www.alvinrapada.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The super rich white exploits the rich white. The rich white

Comments in Code: How To Avoid Clutter and Make Your Code More Readable

Bulk Create Google Drive Folder Structures & Files

Convert string of integers to an Int: w/o Int(“1234”, radix:10) — iOS Swift

Establish VPN Connect Between Azure Vnet and Local RRAS

LiveRamp Is Migrating to Google Cloud Platform

A bit of automation with Gradle Tasks

ROS2 Introduction with Turtlesim Package

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alvin Rapada

Alvin Rapada

www.alvinrapada.com

More from Medium

Solving a few functional programming exercises using Elixir

How to create your first project in Rust

Securing Tokens (JWTs, etc.) in Browsers (trying to be novel)

Elixir — A Tincture for Functional Programming Part 1.5 Recursion

Ouroboros