elixir -v
1.3.1
Elixir is a dynamic functional compiled language that runs over an Erlang Virtual Machine called BEAM.
Erlang and its BEAM is well known for running low-lattency, distributed and fault-tolerant applications.
Elixir was designed to take all that advantages in a modern coding language.
Elixir data types are immutable.
In Elixir a function is usually described with its arity (number of arguments), such as: is_boolean/1
.
.exs
=> Elixir script file.ex
=> Elixir regular file.beam
=> Compiled Elixir file
elixir <script_file>.exs
=> run a script fileelixirc <file>.ex
=> compile a file toElixir.<File>.beam
foo function return tuple
=> the result of afoo
function is usually{:ok, result}
or{:error, reason}
foo! function may raise an error
=> the result of afoo!
is not wrapped in a tuple and it may raises an exception- Exceptions/Errors are not used for controlling flow
- Elixir uses fail fast idea and the supervision trees to control process health and possible restart processes.
#
=> single line comment
There are no multi-line comment
@moduledoc
=> module documentation@doc
=> function documentation@spec
=> function arguments/return specification@typedoc
=> type documentation@type
=> type definition@typep
=> private type definition
defmodule Math do
@moduledoc """
Provides math-related functions.
## Examples
iex> Math.sum(1, 2)
3
"""
@spec sum(number, number) :: number
@doc """
Calculates the sum of two numbers.
"""
def sum(a, b), do: a + b
end
h Math
h Math.sum
_
=> unbound variable
:observer.start
=> start a gui tool for inspection:erlang.memory
=> inspect memory:c.memory
=> inspect memory
:c.memory
# [
# total: 19262624,
# processes: 4932168,
# processes_used: 4931184,
# system: 14330456,
# atom: 256337,
# atom_used: 235038,
# binary: 43592,
# code: 5691514,
# ets: 358016
# ]
iex
=> open Interactive Elixiriex <file>
=> open Interactive Elixir loading a file<Ctrl>c + a
=> close iexi <object>
=> information about an objecth <function/arity>
=> help for a functionh <operator/arity>
=> help for a operators <function/arity>
=> specification for a functions <operator/arity>
=> specification for a operatort <function/arity>
=> type for a functionc <file>
=> load and compile a.ex
file
1
=> integer1_000
=> integers can use_
to make it easy to read0x1F
=> integer0b1010
=> binary integer notation 100o777
=> octadecimal integer notation 5110x1F
=> hexadecimal integer notation 31
-1.0
=> float5.7e-2
=> float exponent notation 0.057
-
:atom
=> atom / symbol -
true
=> boolean (atom)
<<97::size(2)>>
=> bit string<<97,98>>
=> binary"elixir"
=> string
{1, 2, 3}
=> tuple
-
[1, 2, 3]
=> list -
'elixir'
=> char list -
[a: 5, b: 3]
=> keyword list short notation -
[{:a, 5}, {:b, 3}]
=> keyword list long notation
%{name: "Mary", age: 29}
=> map short notation (keys must be atoms)%{:name => "Mary", :age => 29}
=> map long notation
self() #=> #PID<0.80.0>
=> current Process id
fn -> :hello end
=> anonymous function
make_ref() #=> #Reference<0.0.8.133>
=> create a new reference
hd Port.list() #=> #Port<0.0>
=> get first port
is_nil/1
is_integer 1
is_float 4.6
is_number 7.8
is_atom :foo
is_boolean false
is_bitstring <<97:2>>
is_binary <<97, 98>>
is_list/1
is_tuple/1
is_map/1
is_pid self()
is_function(fn a, b -> a + b end)
=> functionis_function(fn a, b -> a + b end, 2)
=> function with arityis_port hd Port.list()
is_reference make_ref()
Range.range?(1..3)
to_char_list("hełło")
=> convert a string to char listto_string('hełło')
=> convert a char list to stringMap.to_list(%{:a => 1, 2 => :b})
=> convert a map to list of tuples or keyword list
10 / 2 => 5.0
=> division always return a floatdiv(10, 2) => 5
=> integer divisionrem 10, 3 => 1
=> integer remain of a divisionround(3.58) => 4
=> float roundtrunc(3.58) => 3
=> float trunc
Falsy in Elixir is false
and nil
, otherwise will be truthy.
==
=> equals!=
=> different===
=> strict equal (integer with float)!==
=> strict different (integer with float)<
=> less<=
=> less or equal>
=> greater>=
=> greater or equal&&
=> truthy and||
=> truthy or!
=> truthy notand
=> boolean andor
=> boolean ornot
=> boolean not
It's possible to compare different data types and that's the sorting order: number < atom < reference < functions < port < pid < tuple < list < bit string
.
|>
=> pipe operator
The return of a function will be passed as the first argument to the following.
1..100 |>
Stream.map(&(&1 * 3)) |>
Stream.filter(&(rem(&1, 2) != 0)) |>
Enum.sum
#=> 7500
In Elixir =
sign is not just an assign operator, but a Match Operator.
This means that you assign variables from right side to the left based on patterns defined by the left one. If a pattern does not match a MatchError
is raised.
This powerful tool is also used to decompose complex objects like tuples, lists, etc into smaller ones:
x = 1 #=> assign 1 to x
2 = x #=> ** (MatchError)
1 = x #=> match and does not assign anything
<<0, 1, x>> = <<0, 1, 2, 3>> #=> ** (MatchError)
<<0, 1, x::binary>> = <<0, 1, 2, 3>>
<<0, 1>> <> <<x::binary>> = <<0, 1, 2, 3>>
<<0, 1>> <> <<x, y>> = <<0, 1, 2, 3>>
<<0, 1>> <> <<x>> <> <<y>> = <<0, 1, 2, 3>>
"world" <> x = "hello" #=> ** (MatchError)
"he" <> x = "hello"
{x, y, z} = {1, 2} #=> ** (MatchError)
{} = {1, 2} #=> ** (MatchError)
{:a, :b} = {:b, :a} #=> ** (MatchError)
{x, y} = {1, 2}
first..last = 1..5
[x, 4] = [:a, 5] #=> ** (MatchError)
[] = [:a, 5] #=> ** (MatchError)
[:a, :b] = [:b, :a] #=> ** (MatchError)
[x, 4] = [:a, 4]
[x | y] = [] #=> ** (MatchError)
[x | y] = [1]
[x | y] = [1, 2, 3]
[a: x] = [b: 9] #=> ** (MatchError)
[a: x] = [a: 5]
[{:a, x}] = [a: 5]
%{a: x} = %{b: 5} #=> ** (MatchError)
%{} = %{a: 5} # empty map matches any map
%{a: x, b: 5} = %{b: 5, a: 7, c: 9}
defmodule User do
defstruct name: "John", age: 27
end
john = %User{age: 29}
%User{name: name} = john
name #=> "John"
So in other words:
- non variables on the left side will be used to restrict a pattern to match
- variables using the pin operator on the left side will have its value to be used to restrict a pattern to match
- variables on the left side will be assigned with right side values
So variables and non variables behave differently with the match operator.
In order to assert an empty map you have to use a guard instead of pattern match, just like:
(
fn m when map_size(m) == 0 ->
"empty map"
end
).(%{}) #=> "empty map"
The Pin Operator ^
is used to treat variables the same way non variables with the match operator. In other words, the Pin Operator will evaluate the variable and use its value to restrict a pattern, preserving its original value.
x = 1 #=> assign 1 to x
^x = 1 #=> match x value with right side 1
^x = 2 #=> ** (MatchError)
You cannot make function calls on the left side of a match.
length([1, [2], 3]) = 3 #=> ** (CompileError) illegal pattern
You can customize an Elixir Operator like the following example:
1 + 2 #=> 3
defmodule WrongMath do
def a + b do
{a, b}
end
end
import WrongMath
import Kernel, except: [+: 2]
1 + 2 #=> {1, 2}
Available delimiters for Sigil
: /
, |
, "
, '
, (
, [
, {
, <
.
~r
=> regular expression (modifiers:i
)~r/hello/i
=>i
modifies to case insensitive~w
=> list of string words (modifiers: )~w[foo bar]c
=>c
modifies to list of char lists~w[foo bar]a
=>c
modifies to list of atoms
~w(one two three) #=> ["one", "two", "three"]
~w(one two three)c #=> ['one', 'two', 'three']
~w(one two three)a #=> [:one, :two, :three]
<<97::4>>
=> short notation with 4 bits<<97::size(4)>>
=> long notation with 4 bitsbyte_size(<<5::4>>)
=> bit string byte size
byte_size(<<97::4>>)
Binaries are 8 bits multiple Bit Strings. Binaries are 8 bits by default.
<<97>>
=> short notation with 8 bits<<97::size(8)>>
=> long notation with 8 bits<>
=> concatenate binaries/strings
byte_size(<<97>>)
String is a Binary of code points where all elements are valid characters. Strings are surrounded by double-quotes and are encoded in UTF-8
by default.
"hello"
=> string<<97, 98>>
=> string "ab"<<?a, ?b>>
=> string "ab"?x
=> code points (ASCII code) for letterx
"hello #{:world}"
=> string interpolation"\n"
=> new lineString.length("hello") #=> 5
=> get the length of a stringString.upcase("hello") #=> "HELLO"
=> upcase a string"""
=> multi-line string begin/end
byte_size("hello")
String.length("Hello")
Tuple is a list that is stored contiguously in memory.
{:ok, "hello"}
tuple_size({:ok, "hello"})
=> tuple sizeelem({:ok, "hello"}, 0)
=> get tuple element by indexput_elem({:ok, "hello"}, 1, "world")
tuple_size({:ok, "hello"})
elem({:ok, "hello"}, 1)
put_elem({:ok, "hello"}, 1, "world")
Lists implements Enumerables protocol.
List is a linked list structure where each element points to the next one in memory. When subtraction just the first ocurrence will be removed.
[:ok, "hello"]
[97 | [1, 6, 9]]
=> prepend[1, 5] ++ [3, 9] # [1, 5, 3, 9]
=> concatenation[1, 5] -- [9, 5] # [1]
=> subtraction first occurrenceshd([1, 5, 7])
=> headtl([1, 5, 7])
=> tail:foo in [:foo, :bar] #=> true
=> in operator
[97 | [1, 6, 9]]
=> prependhd([1, 5, 7])
=> headtl([1, 5, 7])
=> tail
[1, 5] ++ [3, 9] # [1, 5, 3, 9]
=> concatenation[1, 5] -- [9, 5] # [1]
=> subtraction first occurrenceslength([1, 4])
:foo in [:foo, :bar] #=> true
=> in operator
A Char List is a List of code points where all elements are valid characters. Char Lists are surrounded by single-quote and are usually used as arguments to some old Erlang code.
'ab'
=> char list[97, 98]
=>'ab'
[?a, ?b]
=>'ab'
?x
=> code points (ASCII code) for letterx
'hello' ++ 'world' # 'helloworld'
=> concatenation'hello' -- 'world' # 'hel'
=> subtraction first occurrences?l in 'hello' #=> true
=> in operator
[?H | 'ello']
=> prepend
'hello' ++ 'world' # 'helloworld'
=> concatenation'hello' -- 'world' # 'hel'
=> subtraction first occurrenceslength('Hello')
?l in 'hello' #=> true
=> in operator
Keyword list is a list of tuples where first elements are atoms. When fetching by key the first match will return. If a keyword list is the last argument of a function the square brackets [
are optional.
[a: 5, b: 3]
=> keyword list short notation[{:a, 5}, {:b, 3}]
=> keyword list long notation[{:a, 6} | [b: 7]] # [a: 6, b: 7]
=> prepend[a: 5] ++ [a: 7] # [a: 5, a: 7]
=> concatenation[a: 5, b: 7] -- [a: 5] # [b: 7]
=> subtraction first ocurrences([a: 5, a: 7])[:a] # 5
=> fetch by keylength(a: 5, b: 7)
=> optional squared brackets[
[{:a, 6} | [b: 7]] # [a: 6, b: 7]
=> prepend
[a: 5] ++ [a: 7] # [a: 5, a: 7]
=> concatenation[a: 5, b: 7] -- [a: 5] # [b: 7]
=> subtraction first ocurrences([a: 5, a: 7])[:a] # 5
=> fetch by keylength(a: 5, b: 7)
=> optional squared brackets[
Maps implements Enumerables protocol.
Map holds a key value structure.
%{name: "Mary", age: 29}
=> map short notation (keys must be atoms)%{:name => "Mary", :age => 29}
=> map long notation%{name: "Mary", age: 29}[:name]
=> fetch:name
hash notation%{name: "Mary", age: 29}[:born]
=> returns nil when do not find in the hash notation%{name: "Mary", age: 29}.name
=> fetch:name
short notation%{name: "Mary", age: 29}.born # ** (KeyError)
=> blows an error when key does not exists%{%{name: "Mary", age: 29} | age: 31}
=> update value for existing key%{%{name: "Mary", age: 29} | born: 1990} # ** (KeyError)
=> blows an error when updating non existing keymap_size(%{name: "Mary"}) #=> 1
=> map size
%{name: "Mary", age: 29}[:name]
=> fetch:name
%{name: "Mary", age: 29}.name
=> fetch:name
short notation%{%{name: "Mary", age: 29} | age: 31}
=> update value for existing keymap_size(%{name: "Mary"}) #=> 1
=> map size
Structs are built in top of Map.
defstruct
=> define a struct
defmodule User do
defstruct name: "John", age: 27
end
john = %User{} #=> %User{age: 27, name: "John"}
mary = %User{name: "Mary", age: 25} #=> %User{age: 25, name: "Mary"}
meg = %{john | name: "Meg"} #=> %User{age: 27, name: "Meg"}
bill = Map.merge(john, %User{name: "Bill", age: 23})
john.name #=> John
john[:name] #=> ** (UndefinedFunctionError) undefined function: User.fetch/2
is_map john #=> true
john.__struct__ #=> User
Map.keys(john) #=> [:__struct__, :age, :name]
Ranges are Struct
.
range = 1..10
=> range definitionEnum.reduce(1..3, 0, fn i, acc -> i + acc end) #=> 6
=> range used in a reduce function to sum them upEnum.count(range) #=> 10
Enum.member?(range, 11) #=> false
defprotocol Foo
=> define protocolFoo
defimpl Foo, for Integer
=> implement that protocol forInteger
Here are all native data types that you can use: Atom
, BitString
, Float
, Function
, Integer
, List
, Map
, PID
, Port
, Reference
, Tuple
.
defprotocol Blank do
@doc "Returns true if data is considered blank/empty"
def blank?(data)
end
defimpl Blank, for: Integer do
def blank?(_), do: false
end
defimpl Blank, for: List do
def blank?([]), do: true
def blank?(_), do: false
end
defimpl Blank, for: Map do
def blank?(map), do: map_size(map) == 0
end
defimpl Blank, for: Atom do
def blank?(false), do: true
def blank?(nil), do: true
def blank?(_), do: false
end
Blank.blank?(0) #=> false
Blank.blank?([]) #=> true
Blank.blank?([1, 2, 3]) #=> false
Blank.blank?("hello") #=> ** (Protocol.UndefinedError)
Structs
do not share Protocol
implementations with Map
.
defimpl Blank, for: User do
def blank?(_), do: false
end
You can also implement a Protocol
for Any
. And in this case you can derive any Struct
.
defimpl Blank, for: Any do
def blank?(_), do: false
end
defmodule DeriveUser do
@derive Blank
defstruct name: "john", age: 27
end
Elixir built-in most common used protocols: Enumerable
, String.Chars
, Inspect
.
put_in/2
update_in/2
get_and_update_in/2
users = [
john: %{name: "John", age: 27, languages: ["Erlang", "Ruby", "Elixir"]},
mary: %{name: "Mary", age: 29, languages: ["Elixir", "F#", "Clojure"]}
]
users[:john].age #=> 27
users = put_in users[:john].age, 31
users = update_in users[:mary].languages, &List.delete(&1, "Clojure")
Lists and Maps are Enumerables.
Enum
module perform eager operations, meanwhile Stream
module perform lazy operations.
# eager Enum
1..100 |> Enum.map(&(&1 * 3)) |> Enum.sum #=> 15150
# lazy Stream
1..100 |> Stream.map(&(&1 * 3)) |> Enum.sum #=> 15150
In Elixir you can use either Keyword List syntax or do/end Block syntax:
sky = :gray
if sky == :blue do
:sunny
else
:cloudy
end
if sky == :blue, do: :sunny, else: :cloudy
if sky == :blue, do: (
:sunny
), else: (
:cloudy
)
sky = :gray
if sky == :blue, do: :sunny, else: :cloudy
sky = :gray
unless sky != :blue, do: :sunny, else: :cloudy
sky = {:gray, 75}
case sky, do: (
{:blue, _} -> :sunny
{_, t} when t > 80 -> :hot
_ -> :check_wheather_channel
)
On when guards short-circuiting operators &&
, ||
and !
are not allowed.
cond
is equivalent as if/else-if/else
statements.
sky = :gray
cond do: (
sky == :blue -> :sunny
true -> :cloudy
)
with
=> macro to combine multiple match clauses<-
=> a matching clause, on the left=
=> bare expression is allowedelse
=> if some matching clause fails
opts = %{width: 10, height: 20}
with {:ok, width} <- Map.fetch(opts, :width),
{:ok, height} <- Map.fetch(opts, :height) do
{:ok, width * height}
else
:error ->
{:error, :wrong_data}
end
#=> {:ok, 200}
There is traditional no for loop in Elixir, due to Elixir immutability There is a macro for
that it's also called as Comprehension
but it works differently from a traditional for loop. If you want a simple loop iteration you'll need to use recursion:
defmodule Logger do
def log(msg, n) when n <= 0, do: ()
def log(msg, n) do
IO.puts msg
log(msg, n - 1)
end
end
Logger.log("Hello World!", 3)
# Hello World!
# Hello World!
# Hello World!
In functional programming languages map and reduce are two major algorithm concepts. They can be implemented with recursion or using the Enum
module.
reduce will reduces the array into a single element:
defmodule Math do
def sum_list(list, sum \\ 0)
def sum_list([], sum), do: sum
def sum_list([head | tail], sum) do
sum_list(tail, head + sum)
end
end
Math.sum_list([1, 2, 3]) #=> 6
Enum.reduce([1, 2, 3], 0, &+/2) #=> 6
map modifies an existing array (new array with new modified values):
defmodule Math do
def double([]), do: []
def double([head | tail]) do
[head * 2 | double(tail)]
end
end
Math.double([1, 2, 3]) #=> [2, 4, 6]
Enum.map([1, 2, 3], &(&1 * 2)) #=> [2, 4, 6]
Comprehension
is a syntax sugar for the very powerful for special form
. You can have generators and filters in that.
for
=>Comprehension
->
=> generators:into
=> change return to anotherCollectable
type
You can iterate over Enumerable
what makes so close to a regular for
loop on other languages:
for n <- [1, 2, 3, 4], do: n * n
#=> [1, 4, 9, 16]
You can also iterate over multiple Enumerable
and then create a combination between them:
for i <- [:a, :b, :c], j <- [1, 2], do: {i, j}
#=> [a: 1, a: 2, b: 1, b: 2, c: 1, c: 2]
You can pattern match each element:
values = [good: 1, good: 2, bad: 3, good: 4]
for {:good, n} <- values, do: n * n
#=> [1, 4, 16]
Generators use ->
and they have on the right an Enumerable
and on the left a pattern matchable element variable.
You can have filters to filter truthy elements:
for dir <- [".", "/"],
file <- File.ls!(dir),
path = Path.join(dir, file),
File.regular?(path) do
"dir=#{dir}, file=#{file}, path=#{path}"
end
#=> ["dir=., file=README.md, path=./README.md", "dir=/, file=.DS_Store, path=/.DS_Store"]
You can use :into
to change the return type:
for k <- [:foo, :bar], v <- 1..5, into: %{}, do: {k, v}
#=> %{bar: 5, foo: 5}
for k <- [:foo, :bar], v <- 1..5, into: [], do: {k, v}
#=> [foo: 1, foo: 2, foo: 3, foo: 4, foo: 5, bar: 1, bar: 2, bar: 3, bar: 4, bar: 5]
fn
=> define functions->
=> one line function definition.
=> call a functionwhen
=> guards
add = fn a, b -> a + b end
add.(4, 5) #=> 9
We can have multiple clauses and guards inside functions.
calc = fn
x, y when x > 0 -> x + y
x, y -> x * y
end
calc.(-1, 6) #=> 5
calc.(4, 5) #=> 45
defmodule
=> define Modulesdef
=> define functions inside Modulesdefp
=> define private functions inside Moduleswhen
=> guards@
=> module attributes__info__(:functions)
=> list functions inside a moduledefdelegate
=> delegate functions
defmodule Math do
def sum(a, b) when is_integer(a) and is_integer(b), do: a + b
end
Math.sum(1, 2) #=> 3
Math.__info__(:functions) #=> [sum: 2]
Module attribute works as constants, evaluated at compilation time:
defmodule Math do
@foo :bar
def print, do: @foo
end
Math.print #=> :bar
Special Module attributes:
@vsn
@moduledoc
@doc
@behaviour
@before_compile
\\
=> default argument
defmodule Concat do
def join(a, b, sep \\ " ") do
a <> sep <> b
end
end
IO.puts Concat.join("Hello", "world") #=> Hello world
IO.puts Concat.join("Hello", "world", "_") #=> Hello_world
Default values are evaluated runtime.
defmodule DefaultTest do
def dowork(x \\ IO.puts "hello") do
x
end
end
DefaultTest.dowork #=> :ok
# hello
DefaultTest.dowork 123 #=> 123
DefaultTest.dowork #=> :ok
# hello
Function with multiple clauses and a default value requires a function head where default values are set:
defmodule Concat do
def join(a, b \\ nil, sep \\ " ") # head function
def join(a, b, _sep) when is_nil(b) do
a
end
def join(a, b, sep) do
a <> sep <> b
end
end
IO.puts Concat.join("Hello") #=> Hello
IO.puts Concat.join("Hello", "world") #=> Hello world
IO.puts Concat.join("Hello", "world", "_") #=> Hello_world
&
=> function capturing&1
=> 1st argument
&
could be used with a module function.
When capturing you can use the function/operator with its arity.
&(&1 * &2).(3, 4) #=> 12
(&*/2).(3, 4) #=> 12
(&Kernel.is_atom(&1)).(:foo) #=> true
(&Kernel.is_atom/1).(:foo) #=> true
(&{:ok, [&1]}).(:foo) #=> {:ok, [:foo, :bar]}
(&[&1, &2]).(:foo, :bar) #=> [:foo, :bar]
(&[&1 | [&2]]).(:foo, :bar) #=> [:foo, :bar]
Behaviour modules defines functions
@callback
=> defines a function to be implemented by other modules::
=> separates the function definition to its return type
defmodule Parser do
@callback parse(String.t) :: any
@callback extensions() :: [String.t]
end
defmodule JSONParser do
@behaviour Parser
def parse(str), do: # ... parse JSON
def extensions, do: ["json"]
end
Exceptions/Errors in Elixir are Structs
.
raise "oops" #=> ** (RuntimeError) oops
=> raises error with messageraise ArgumentError #=> ** (ArgumentError) argument error
=> raises an error by moduleraise ArgumentError, message: "oops" #=> ** (ArgumentError) oops
=> raises an error by module with messagedefexception
=> define an exceptiontry/rescue
=> catches an errorthrow/try/catch
=> can be used as circuit breaking, but should be avoidedexit("my reason")
=> exiting current processafter
=> ensures some resource is cleaned up even if an exception was raised
defmodule MyError do
defexception message: "default message"
end
is_map %MyError{} #=> true
Map.keys %MyError{} #=> [:__exception__, :__struct__, :message]
raise MyError #=> ** (MyError) default message
raise MyError, message: "custom message" #=> ** (MyError) custom message
You can rescue an error with:
try do
raise "oops"
rescue
e in RuntimeError -> e
after
IO.puts "I can do some clean up here"
end
#=> %RuntimeError{message: "oops"}
try do
raise "oops"
rescue
RuntimeError -> "Error!"
end
#=> "Error!"
throw/catch
sometime is used for circuit breaking, but you can usually use another better way:
try do
Enum.each -50..50, fn(x) ->
if rem(x, 13) == 0, do: throw(x)
end
"Got nothing"
catch
x -> "Got #{x}"
end
#=> "Got -39"
Enum.find -50..50, &(rem(&1, 13) == 0)
#=> -39
exit
can be caught but this is rare in Elixir:
try do
exit "I am exiting"
catch
:exit, _ -> "not really"
end
#=> "not really"
You can ommit try
inside functions and use rescue
, catch
, after
directly:
def without_even_trying do
raise "oops"
after
IO.puts "cleaning up!"
end
IO.puts/1 "Hello"
=> prints to stdoutIO.puts :stderr, "Hello"
=> prints to stderrIO.gets "yes/no: "
=> reads an user input
{:ok, pid} = StringIO.open("my-file.md")
=> open a fileIO.read(pid, 2) #=> "he"
=> read first 2 lines
{:ok, file} = File.open "hello", [:write]
=> open file for readingIO.binwrite file, "world"
=> write into fileFile.close file
=> close fileFile.read "my-file.md"
=> reads a fileFile.stream!("my-file.md") |> Enum.take(10)
=> read the first 10 lines
{:ok, file} = File.open "my-file.md", [:write]
IO.binwrite file, "hello world"
File.close file
File.read "my-file.md" #=> {:ok, "hello world"}
Path.join
=> joinsPath.expand("~/hello")
=> expands to full path
Process in Elixir has the same concept as threads in a lot of other languages, but extremely lightweight in terms of memory and CPU. They are isolated from each other and communicate via message passing.
spawn/1
=> fork a processself()
=> current processProcess.alive?(pid)
=> check if process is still runningsend/2
=> send message to another processreceive/1
=> receive message from another processafter
=> receive option to work with timeoutflush()
=> prints out all messages receivedspawn_link/1
=> forks a process and link them, so failures are propagatedTask.start/1
=> starts a taskTask.start_link/1
=> starts a task and link it to the current processProcess.register(pid, :foo)
=> register a name for a process
The idea is to have a supervisor that spawn_link
new processes and when they fail the supervisor will restart them. This is the basics for Fail Fast and Fault Tolerant in Elixir.
Tasks are used in supervision trees.
parent = self()
spawn_link(fn -> send(parent, {:hello, self()}) end)
receive do: ({msg, pid} -> "#{inspect pid} => #{msg}"), after: (1_000 -> "nothing after 1s")
Task.start_link(fn -> send(parent, {:hello, self()}) end)
receive do: ({msg, pid} -> "#{inspect pid} => #{msg}"), after: (1_000 -> "nothing after 1s")
flush()
State can be stored in processes or using its abstraction: Agent
.
Manual implementation of a storage using Elixir processes:
defmodule KV do
def start_link do
Task.start_link(fn -> loop(%{}) end)
end
defp loop(map) do
receive do
{:get, key, caller} ->
send caller, Map.get(map, key)
loop(map)
{:put, key, value} ->
loop(Map.put(map, key, value))
end
end
end
{:ok, pid} = KV.start_link
send pid, {:put, :hello, :world}
send pid, {:get, :hello, self()}
flush() #=> :world
Implementation of a storage using Agent
:
{:ok, pid} = Agent.start_link(fn -> %{} end)
Agent.update(pid, fn map -> Map.put(map, :hello, :world) end)
Agent.get(pid, fn map -> Map.get(map, :hello) end)
In order to facilitate code reuse Elixir has: alias
, require
, import
(directives) and use
(macro).
alias Foo.Bar, as: Bar
=> alias module, so Bar can be called instead of Foo.Baralias Foo.Bar
=>as
is optional on aliasrequire Foo
=> ensure the module is compiled and available (usually for macros)import Foo
=> requires and import functions from Foo so they can be called without theFoo.
prefiximport List, only: [duplicate: 2]
=> only optionimport List, expect: [duplicate: 2]
=> except optionimport List, only: :macros
=> import only macrosimport List, only: :functions
=> import only functionsuse Foo
=> invokes the custom code defined in Foo as an extension pointalias MyApp.{Foo, Bar, Baz}
=> multiple aliasrequire MyApp.{Foo, Bar, Baz}
=> multiple requireimport MyApp.{Foo, Bar, Baz}
=> multiple import
All modules are defines inside Elixir
namespace but it can be omitted for convenience.
alias
, require
and import
are lexically scoped, which means that it will be valid just inside the scope it was defined. This is not a global scope.
require
is usually used to require Elixir macro code:
Integer.is_odd(3) #=> ** (CompileError): you must require Integer before invoking the macro Integer.is_odd/1
require Integer
Integer.is_odd(3) #=> true
use
call __using__
when the module is being used:
defmodule Fruit do
defmacro __using__(option: option) do
IO.puts "options=#{inspect option}"
quote do: IO.puts "Using Fruit module"
end
end
defmodule Meal do
use Fruit, option: :hello
end
#=> "Good to see you've added Fruit to your meal"
quote
=> shows AST (Abstract Syntax Tree)
quote do: 2 * 2 == 4
#=> {
#=> :==,
#=> [context: Elixir, import: Kernel],
#=> [
#=> {
#=> :*,
#=> [context: Elixir, import: Kernel],
#=> [2, 2]
#=> },
#=> 4
#=> ]
#=> }
Elixir provider some Erlang modules as atoms.
:crypto
=> crypto functions like:crypto.hash/2
:io
=> io functions like:io.format/2
:digraph
=> deal with digraphs:ets
=> large data structure in memory:dets
=> large data structure on disk:math
=> math functions like:math.pi/0
:queue
=> first-in first-out structure:rand
=> rand functions like:rand.uniform/0
:zip
=> handle zip files:zlib
=> handle gzip files