Elixir Cheat
Elixir Cheat
Types
Integer! 1234 0xcafe 0177 0b100 10_000
cheat sheet
elixir-lang.org
v1.2
Updated 1/4/2016
Float !
Atom !
Tuple
List !
[ 1, 2, 3 ]
[ head | tail ]
'abc'
''' here doc '''
Command line
elixir [options] le.ex/le.exs
iex
iex -S script (e.g., iex -S mix)
iex --name local
iex --sname fully.qualied.name
--cookie cookie.value or use
$HOME/.erlang.cookie
mix new / run / test / deps / etc.
mix.exs specifies build details
iex Commands
back to prompt
#iex:break
c "lename.exs" compile
r Module
reload
h function_name help
i var
display type info
v [n]
session history
Operators
Truth!
Range! a..b
Anonymous Functions
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
(relaxed)
+, -, *, /
(float)
div, rem
(integer)
(concat)
list1 ++ list2 !
(concat)
a in enum
^term
! (membership)
(no reassign)
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)
== != && || !
>, >=, <, <=
(set diff)
use Module
!
calls Module.__using__
Comprehensions
(strict)
list1 -- list2
Shortcut: &(...)
&1,&2 as parameters
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
&mod_name.func_name/arity
(Can omit mod_name)
Copyright 2013-2016 The Pragmatic Programmers, LLC. Free to use without modification for noncommercial applications. Content/design by Andy Hunt & Dave Thomas.
Pipelines
Maps
%{ key => value, key => value }
(same as)
Protocols
defprotocol module.name do
@moduledoc description
@only [list of types] (optional)
def name(parms)
end
dempl mod.name, 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
Regexp
f3(f2(f1(expr), a, b), c)
Control Flow
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
~r{pattern}opts
f
g
i
m
r
s
u
x
Unicode patterns
ignore whitespace and comments
Processes
pid = spawn(anon_function)
pid = spawn(mod, func, args)
(also spawn_link)
receive do
{ sender, msg, } ->
send sender { :ok, value }
after timeout ->
...
end
pragprog.com/books/elixir12
Structs
defmodule Name do
Metaprogramming
defmacro macroname(parms) do
parms are quoted args
!
!
end
quote do: returns internal rep.
quote bind_quoted: [name: name]
do: ...
Sigils
~type{ content }
Delimiter: { }, [ ], ( ), / /, | |, " ", or ' '
~S
~s
~C
~c
Predefined Names
~R
~r
~W
~w
regexp
regexp w/interpolation
words (white space delim)
words w/interpolation
Copyright 2013-2016 The Pragmatic Programmers, LLC. Free to use without modification for noncommercial applications. Content/design by Andy Hunt & Dave Thomas.