5. DEFINITION
MODULE
defmodule ElixirPatternMatching do
// staff
end
FUNCTION
def sum(x,y) do
IO.inspect x
IO.inspect y
x + y # returned value
end
In Elixir, a function must be defined inside a module.
PRIVATEFUNCTION
defp get_gps_coordinates(country) do
%{
France: {46.71109, 1.7191036},
Spain: {40.2085, -3.713},
Italy: {41.29246, 12.5736108}
}[country]
end
10. STREAM
Enum problem : each enum should complete before piping
result to next enum.
Solution : use stream !
stream =(
1..3
|> Stream.map(fn x -> IO.inspect(x) end)
|> Stream.map(fn x -> x * 2 end)
|> Stream.map(fn x -> IO.inspect(x) end)
)
Enum.to_list(stream)
1
2
2
4
3
6
Each number is completely evaluated before moving to the
next number in the enumeration !
Streams are lazy !
Streams are useful when working with large, possibly
infinite, collections.
11. TUPLE
Tuple are like a statically-sized arrays : they hold a fixed
number of elements. For dynamic length use List.
list = [1, 2, true, 3]
tuple = {1, 2, true, 3}
gps_coordinate = {46.71109, 1.7191036}
{latitude, longitude} = {46.71109, 1.7191036} # pattern matching
https://hexdocs.pm/elixir/Tuple.html#functions
MAP(KEY/VALUE)
def get_gps_coordinates(country) do
%{
France: {46.71109, 1.7191036},
Spain: {40.2085, -3.713},
Italy: {41.29246, 12.5736108}
}[country]
end
When keys are dynamics :
map = %{"foo" => "bar", "hello" => "world"}
When keys are constants :
%{foo: "bar", hello: "world"} or %{:foo => "bar", :hello => "world"}
https://devhints.io/elixir#map
https://hexdocs.pm/elixir/Map.html#functions
12. STRUCT
defmodule ElixirPatternMatching.Country do
defstruct name: "", code: ""
end
defmodule ElixirPatternMatching.Examples do
alias ElixirPatternMatching.Country
def get_all_countries do
[
%Country{name: "France", code: "FR"},
%Country{name: "Spain", code: "ES"},
%Country{name: "Italy", code: "IT"}
]
end
end
https://elixir-lang.org/getting-started/structs.html
PATTERNMATCHING
TUPLE
{latitude, longitude} = {46.71109, 1.7191036} # tuple
{_latitude, longitude} = {46.71109, 1.7191036} # unused latitude
{_, longitude} = {46.71109, 1.7191036} # skip latitude
14. MULTICLAUSEFUNCTIONS
MULTICLAUSE
def get_cars(%{type: "bmw"} = params) do
IO.puts("I want only #{params.type} !")
end
def get_cars(%{type: "volkswagen"} = params) do
IO.puts("I want only #{params.type} !")
end
# the default clause
def get_cars(_) do
IO.puts("I want all cars !")
end
Instead of having classical branching (if/else), we can do
multiple clauses for our function get_cars.
MULTICLAUSEWITHGUARD
def get_cars(category, %Car{type: "bmw"} = car) when
is_binary(category) do
IO.puts("I want only #{String.upcase(car.type)}
#{String.upcase(car.category)} !")
end
def get_cars(%Car{} = car) do
IO.puts("I want all cars !")
end
https://medium.com/@helabenkhalfallah/elixir-pattern-matching-
bd4a1eb4d59f
15. IF,CASE,COND
IF
Multiple lignes :
if condition do
...
else
...
end
Example :
if String.contains?(name, " ") do
split_name = name |> String.split(" ")
first_letter = split_name |> List.first() |> String.slice(0, 1)
last_letter = split_name |> List.last() |> String.slice(0, 1)
else
name |> String.slice(0, 1)
end
Single ligne :
if condition, do: something, else: another_thing
Example :
def max(a, b) do
if a >= b, do: a, else: b
end
16. COND
Definition :
cond do
expression_1 ->
...
expression_2 ->
...
end
Example :
def greet(lang) do
cond do
lang == "en" ->
"Hello"
lang == "fr" ->
"Bonjour"
lang == "es" ->
"Hola"
true ->
"We don't have a greeting for that."
end
end
cond will raise an error if there is no match. To handle this,
we should define a condition set to true.
17. CASE
Definition :
case expression do
pattern_1 ->
...
pattern_2 ->
...
end
Example :
def greet(lang) do
case lang do
"eng" -> "Hello"
"fr" -> "Bonjour"
"es" -> "Hola"
_ -> "We don't have a greeting for that."
end
end
Default clause ‘_’ is mandatory.
18. ERRORHANDLING
TRY,RESCUE,AFTER
try do
opts
|> Keyword.fetch!(:source_file)
|> File.read!()
rescue
e in KeyError -> IO.puts("missing :source_file option")
e in File.Error -> IO.puts("unable to read source file")
end
TRY,CATCH
try do
...
catch
type_pattern_1, error_value_1 -> …
type_pattern_2, error_value_2 -> ...
end
19. THROW
try do
for x <- 0..10 do
if x == 5, do: throw(x)
IO.puts(x)
end
catch
x -> IO.puts("Caught: #{x}")
end
The throw function gives us the ability to exit execution
with a specific value we can catch and use.
CREATENEWEXCEPTION
defmodule ExampleError do
defexception message: "an example error has occurred"
end
try do
raise ExampleError
rescue
e in ExampleError -> e
end
20. IMPORT,ALIAS&USE
defmodule Stats do
alias Math.List, as: List
# In the remaining module definition List expands to
Math.List.
end
defmodule ElixirPatternMatching.Country do
defstruct name: "", code: ""
end
defmodule ElixirPatternMatching.Examples do
alias ElixirPatternMatching.Country
# we can call Country directly without
ElixirPatternMatching.Country
def get_all_countries do
[
%Country{name: "France", code: "FR"},
%Country{name: "Spain", code: "ES"},
%Country{name: "Italy", code: "IT"}
]
end
end
22. 3.
RUNTIME
BEAM&PROCESS
Erlang VM is called Beam.
Beam is a garbage collector memory management.
- inside Beam VM, process are lightweights and
independents (isolated).
- process are also independents from the Host'OS
(deterministic system, the same behavior everywhere).
- process communicate only by exchanging messages.
- each process has its own memory (a mailbox, a heap and a
stack) and a process control block (PCB) with information
about the process.
- each process has its own heap means that each process's
heap is garbage collected independently. Only one
process will be paused for GC and not the whole runtime,
this is unlike Java JVM which stop the world for GC.
- supervisor to supervise process healthiness (restart
process if needed).
- process can supervise each other.
- schedulers to balance execution.