Elixir Cheatsheet

Sigils

Sigils support 8 different delimiters - //, ||, "", '', (), [], {}, <>.

Sigil Explaination

~r()

Regular Expression

~s()

String (binary)

~c()

Char list

~w()s

Word list of strings (binaries)

~w()c

Word list of char lists

~w()a

Word list of atoms

Escape Codes

The following escape codes can be used in strings and char lists:

Code Explaination Code Explaination

\\

single backslash

\a

bell/alert

\b

backspace

\d

delete

\e

escape

\f

form feed

\n

newline

\r

carriage return

\s

space

\t

tab

\v

vertical tab

\0

null byte

\xDD

represents a single byte in hexadecimal (such as \x13)

\uDDDD and \u{D…​}

represents a Unicode codepoint in hexadecimal (such as \u{1F600})

A double quote literal inside a double quoted string also needs to be escaped.

Metaprogramming

Various metaprogramming variables and macros.

Expression Explaination

ENV

A struct containing details about the compilation environment

defmacro (name) do …​ end

Define a macro

quote do …​ end

Convert the code inside the block to AST

unquote(expresion)

Use to inject values from outside of the quote block

def using

The using macro

_\_MODULE\_\_

Expands to the module name of the current module

CALLER

DIR

STACKTRACE

Structs, Maps, and Lists

Operation List Map Struct

Define

---- defmodule Person do defstruct first_name: "", last_name: "" end ----

Create

[1,2,3]

%{a: :foo}

%Person{first_name: "Joe", last_name: "Armstrong"}

Match

[a, b | rest] = list

%{a: value} = map

%Person{first_name: name} = struct

Update

Comprehensions

for var1 <- generator1,
    var2 <- generator2,
    new_var = some_expr(var1, var2),
    a_check?(new_var) do
    operation_to_generate_final_value(var1, var2)
end