KEMBAR78
Introducing Elixir | PPTX
Elixir
Abdulsattar - http://bimorphic.com
Features
Dynamic
Functional
Pattern Matching
Ruby inspired syntax
Extensible: macros
Concurrency, Distributed and Fault tolerant
Data Types/Structures
1 # integer
2.0 # float
true # boolean
:atom # atom / symbol
'elixir' # character list
"elixir" # string
[1, 2, 3] # list
{1,2} # tuple
[{:a, 1}, {:b, 2}] # keyword list
[a: 1, b: 2] # ^ same keyword list
%{a: 1, b: 2, c: 3} # hash
Pattern Matching
a = 1
{x, y} = {0, 0}
[head | tail] = [1, 2, 3]
result = case {1, 2, 3} do
{4, 5, 6} -> "Doesn't match"
{1, x, 3} when x > 3 -> "doesn't match because 2 < 3"
{x, 2, 3} -> "matches and x is #{x}"
_ -> "matches if nothing above matches"
end
def zero?(0), do: true
def zero?(x) when is_number(x), do: false
Modules
defmodule Math do
def sum(a, b) do
a + b
end
def sub(a, b) do
a - b
end
end
IO.puts (Math.sum(3,4))
Structs
defmodule Person do
defstruct name: "Sattar", age: 40
end
me = %Person{name: "AbdulSattar"}
IO.puts me.name # Abdulsattar
%Person{name: name, age: age} = me
IO.puts(age) # 40
youngMe = %{me | age: 10}
Protocols
defprotocol Blank do
@doc "Returns true if data is considered blank/empty"
def blank?(data)
end
defimpl Blank, for: Integer do
def blank?(0), do: true
def blank?(_), do: false
end
defimpl Blank, for: List do
def blank?([]), do: true
def blank?(_), do: false
end
Blank.blank?(3) # false
Blank.blank?([]) # true
Enumerables
iex(2)> Enum.map([1, 2, 3], fn x -> x * x end)
[1, 4, 9]
iex(3)> Enum.map([1, 2, 3], &(&1 * &1))
[1, 4, 9]
iex(4)> Enum.reduce(1..100, 0, &+/2)
5050
Pipe Operator
sumOfSquares = Enum.reduce(Enum.map(1..100, fn x -> x * x end), 0, &+/2)
sumOfSquares1 = 1..100
|> Enum.map(fn x -> x * x end)
|> Enum.reduce(0, &+/2)
square = fn x -> x * x end
sumOfSquares2 = 1..100
|> Enum.map(square)
|> Enum.sum
Macros
defmodule Unless do
defmacro macro_unless(clause, expression) do
quote do
if(!unquote(clause), do: unquote(expression))
end
end
end
Unless.macro_unless false, do: IO.puts "prints"
Unless.macro_unless true, do: IO.puts "doesn't print"
Processes
pid = spawn fn ->
receive do
msg -> IO.puts "Received: #{msg}"
end
end
send pid, "Hello!"
send pid, "3 times"
Supervisors
DEMO

Introducing Elixir

  • 1.
  • 2.
    Features Dynamic Functional Pattern Matching Ruby inspiredsyntax Extensible: macros Concurrency, Distributed and Fault tolerant
  • 3.
    Data Types/Structures 1 #integer 2.0 # float true # boolean :atom # atom / symbol 'elixir' # character list "elixir" # string [1, 2, 3] # list {1,2} # tuple [{:a, 1}, {:b, 2}] # keyword list [a: 1, b: 2] # ^ same keyword list %{a: 1, b: 2, c: 3} # hash
  • 4.
    Pattern Matching a =1 {x, y} = {0, 0} [head | tail] = [1, 2, 3] result = case {1, 2, 3} do {4, 5, 6} -> "Doesn't match" {1, x, 3} when x > 3 -> "doesn't match because 2 < 3" {x, 2, 3} -> "matches and x is #{x}" _ -> "matches if nothing above matches" end def zero?(0), do: true def zero?(x) when is_number(x), do: false
  • 5.
    Modules defmodule Math do defsum(a, b) do a + b end def sub(a, b) do a - b end end IO.puts (Math.sum(3,4))
  • 6.
    Structs defmodule Person do defstructname: "Sattar", age: 40 end me = %Person{name: "AbdulSattar"} IO.puts me.name # Abdulsattar %Person{name: name, age: age} = me IO.puts(age) # 40 youngMe = %{me | age: 10}
  • 7.
    Protocols defprotocol Blank do @doc"Returns true if data is considered blank/empty" def blank?(data) end defimpl Blank, for: Integer do def blank?(0), do: true def blank?(_), do: false end defimpl Blank, for: List do def blank?([]), do: true def blank?(_), do: false end Blank.blank?(3) # false Blank.blank?([]) # true
  • 8.
    Enumerables iex(2)> Enum.map([1, 2,3], fn x -> x * x end) [1, 4, 9] iex(3)> Enum.map([1, 2, 3], &(&1 * &1)) [1, 4, 9] iex(4)> Enum.reduce(1..100, 0, &+/2) 5050
  • 9.
    Pipe Operator sumOfSquares =Enum.reduce(Enum.map(1..100, fn x -> x * x end), 0, &+/2) sumOfSquares1 = 1..100 |> Enum.map(fn x -> x * x end) |> Enum.reduce(0, &+/2) square = fn x -> x * x end sumOfSquares2 = 1..100 |> Enum.map(square) |> Enum.sum
  • 10.
    Macros defmodule Unless do defmacromacro_unless(clause, expression) do quote do if(!unquote(clause), do: unquote(expression)) end end end Unless.macro_unless false, do: IO.puts "prints" Unless.macro_unless true, do: IO.puts "doesn't print"
  • 11.
    Processes pid = spawnfn -> receive do msg -> IO.puts "Received: #{msg}" end end send pid, "Hello!" send pid, "3 times"
  • 12.