Skip to content

Latest commit

 

History

History
87 lines (73 loc) · 2.87 KB

elixir_cheatsheet.adoc

File metadata and controls

87 lines (73 loc) · 2.87 KB

Elixir Cheatsheet

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

Sigil Explanation

~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

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

Code Explanation Code Explanation

\\

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.

Various metaprogramming variables and macros.

Expression Explanation

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(expression)

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 Keyword List Map Struct

Define

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

Create

[1,2,3]

[a: :foo]

%{a: :foo}

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

Match

[a, b | rest] = list

[a: value] = list

%{a: value} = map

%Person{first_name: name} = struct

Update

[new_value | list]

%{map | a: new_value}

%{first_name: new_value}

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