Modules
Types
Integer! 1234 0xcafe 0177 0b100 10_000
cheat
sheet
[Link]
v1.0
Updated 10/15/2014
Float !
1.0 3.1415 6.02e23
Atom !
:foo :me@home :"with spaces"
Tuple
{ 1, 2, :ok, "xy" } (like array)
List !
[ 1, 2, 3 ]
[ head | tail ]
'abc'
here doc '''
(see Enum and List modules)
Keyword List (can duplicate keys)
[ a: "Foo", b: 123 ]
Map (no duplicate keys)
%{ key => value, key => value }
Binary << 1, 2 >> or "abc"
""" here doc """
"#{interpolated}"
<< name::prop-prop-prop >>
binary, bits, bitstring, bytes, oat,
integer, utf8, utf16, utf32, size(n),
signed/unsigned, big/little native
Command line
elixir [options] [Link]/[Link]
iex
iex -S script (e.g., iex -S mix)
iex --name local
iex --sname [Link]
--cookie [Link] or use
$HOME/.[Link]
Truth!
#iex:break!
back to prompt
c "[Link]" compile
r Module
reload
h function_name help
v [n]
session history
Operators
=== !== and or not xor
(strict)
== != && || !
>, >=, <, <=
(relaxed)
true, false, nil
Range! a..b
Anonymous Functions
mix new / run / test / deps / etc.
[Link] specifies build details
iex Commands
(like linked list)
fn parms [guard]-> body
parms [guard] -> body
end
call with func.()
Named Functions
(Only in modules, records, etc)
def name(parms) [guard] do
expression
end
def name(parms) [guard], do: expr
div, rem! !
(integer)
Default params: parameter \\ default
binary1 <> binary2!
(concat)
defp for private functions
list1 ++ list2 !
(concat)
list1 -- list2!
(set diff)
Multiple heads with different params and/
or guards allowed.
(no reassign)
import Module [,only:|except:]
alias mod_path [, as: Name]
@attribute_name value
Call Erlang:
!
:module.function_name
Guard Clause
Part of pattern match
when expr
where operators in expr are limited to:
==, !=, ===, !==, >, <, <=, >=,
or, and, not, !, +, -, *, /, in,
is_atom is_binary is_bitstring is_boolean
is_exception is_oat is_function is_integer
is_nil is_list is_number is_pid is_port
is_reference is_tuple,
abs(num), bit_size(bits), byte_size(bits),
div(num,num), elem(tuple, n), oat(term),
hd(list), length(list), node(),
node(pid|ref|port), rem(num,num),
round(num), self(), tl(list), trunc(num),
tuple_size(tuple)
<> and ++ (left side literal)
for generator/filter [, into: value ], do: expr
(float)
^term
use Module
!
calls Module.__using__
Comprehensions
! (membership)
require Module (used for macros)
Shortcut: &(...)
&1,&2 as parameters
+, -, *, /! !
a in enum
defmodule mod_name do
@moduledoc "description"
@doc "description"
function/macro
end
Generators are:
!
pattern <- list
With binaries as:
for << ch <- "hello" >>, do: expr
do: vs do/end
something do
expr
end
something, do: expr
else, rescue, try, ensure also generate
keyword args, and are then compiled
Capture a function with:
!
&mod_name.func_name/arity
(Can omit mod_name)
Copyright 2013-2014 The Pragmatic Programmers, LLC. Feel free to use without modification for noncommercial applications. Content/design by Andy Hunt & Dave Thomas.
Pipelines
Maps
%{ key => value, key => value }
expr |> f1 |> f2(a,b) |> f3(c)
value = map[key]
value = [Link] (if key is an atom)
newmap = %{ oldmap | key => newval}
Dict.put_new/3 to add a key
(same as)
f3(f2(f1(expr), a, b), c)
Control Flow
Protocols
defprotocol [Link] do
@moduledoc description
@only [list of types] (optional)
def name(parms)
end
dempl [Link], for: type do
@moduledoc description
def name(type, value) do
expr
end
end
Allowed types:
Any Atom BitString Function List Number
PID Port Record Reference Tuple
Regexp
if expr do
exp
else
unless expr do
exp
else
exp
end
exp
end
case expr do
match [guard] -> exp
match [guard] -> exp
end
cond do
bool -> exp
bool -> exp
end
Metaprogramming
defmacro macroname(parms) do
parms are quoted args
!
!
return quoted code which
is inserted at call site
end
match beg of ml string
g!
i
m!
r!
s!
use named groups
case insensitive
^ and $ match each line in ml
reluctant (not greedy)
. matches newline
u!
x!
unicode patterns
ignore whitespace and comments
Structs
defmodule Name do
defstruct eld: default,
end
%Name{eld: value, eld: value, }
~r{pattern}opts
f
[Link]/books/elixir
quote do: returns internal rep.
quote bind_quoted: [name: name]
do: ...
new_struct = %{ var | eld: new_value }
unquote do: only inside quote, injects
code fragment without evaluation
Sigils
~type{ content }
Processes
pid = spawn(anon_function)
pid = spawn(mod, func, args)
(also spawn_link)
receive do
{sender, msg, } ->
send sender { :ok, value }
after timeout ->
...
end
Delimiter: { }, [ ], ( ), / /, | |, " ", or ' '
%S string (no interpolation)
%s string (with interpolation)
%C character list (no interpolation)
%c character list (with interpolation)
%R regexp
%r regexp w/interpolation
%W words (white space delim)
%w words w/interpolation
Predefined Names
__MODULE__ __FILE__ __DIR__ __ENV__
__CALLER__ (macros only)
Copyright 2013-2014 The Pragmatic Programmers, LLC. Feel free to use without modification for noncommercial applications. Content/design by Andy Hunt & Dave Thomas.