Erlang Elixir|>
Elixir
José Valim - co-fondateur de Plataformatec
(http://plataformatec.com.br)
Proposer la puissance de la machine virtuelle Erlang
d’une manière différente aux développeurs.
Premier commit date de janvier 2011
Version actuelle : 0.10.2 / 0.10.3-dev
Release finale est prévue pour juin 2014
Erlang :
wget http://www.erlang.org/download/otp_src_R16B02.tar.gz
tar zxf otp_src_R16B02.tar.gz
cd otp_src_R16B02
./configure
make
sudo make install
Elixir :
git clone https://github.com/elixir-lang/elixir.git
cd elixir
make
sudo make install
Les variables
Erlang :
• /^(?:_[a-zA-Z0-9_]|[A-Z])[a-zA-Z0-9_]*$/
• single assignment
Elixir :
• /^(?:_[a-zA-Z0-9_]|[a-z])[a-zA-Z0-9_]*$/
• re assignment
Erlang :
1> A = [1, 2, 3].
[1, 2, 3]
2> A = [4, 5, 6].
** exception error: no match of right hand side value [4,5,6]
Elixir :
iex(1)> a = [1, 2, 3]
[1, 2, 3]
iex(2)> a = [4, 5, 6]
[4, 5, 6]
Les variables
iex(1)> a = [1, 2, 3]
[1, 2, 3]
iex(2)> [b, c, d] = a
[1, 2, 3]
iex(3)> b
1
iex(4)> c
2
iex(4)> d
3
iex(1)> a = [1, 2, 3]
[1, 2, 3]
iex(2)> [b | tail] = a
[1, 2, 3]
iex(3)> b
1
iex(4)> tail
[2, 3]
atomes
:an_atom
:"an atom!"
booléens
true
false
listes [104,101,108,108,111] = ‘hello’
tuples {1, :tuple, ‘de’, "données"}
fonctions
my_fun = fn(x) -> x*x end
my_fun.(3)
regex %r/erlang|elixir/
range [1..10]
hash
[key1: "value1", key2: 3, ...]
[{"Une clé", "sa valeur»}, ...]
Les binaires : séquence d’entiers (0-255 / 8 bits)
placée entre << et >>
Les binaires : séquence d’entiers (0-255 / 8 bits)
placée entre << et >>
<<104, 101, 108, 108, 111>>
Les binaires : séquence d’entiers (0-255 / 8 bits)
placée entre << et >>
<<[104, 101, 108, 108, 111]>>
<<104, 101, 108, 108, 111>>
Les binaires : séquence d’entiers (0-255 / 8 bits)
placée entre << et >>
<<[104, 101, 108, 108, 111]>>
<<'hello'>>
<<104, 101, 108, 108, 111>>
Les binaires : séquence d’entiers (0-255 / 8 bits)
placée entre << et >>
<<[104, 101, 108, 108, 111]>>
<<'hello'>>
"hello"
<<104, 101, 108, 108, 111>>
<<259>> = ?
<<259>> = <<3>>
<<259>> = <<3>>
<<259:size(16)>> = <<1, 3>>
<<259>> = <<3>>
<<259:size(16)>> = <<1, 3>>
<<x:size(16)>> = <<1, 3>>
<<2.4>>
<<2.4>>
** (ArgumentError) argument error
:erl_eval.expr/3
<<2.4>>
<<2.4:float>> =
<<64, 3, 51, 51, 51, 51, 51, 51>>
** (ArgumentError) argument error
:erl_eval.expr/3
<<2.4>>
<<2.4:float>> =
<<64, 3, 51, 51, 51, 51, 51, 51>>
** (ArgumentError) argument error
:erl_eval.expr/3
les flottants sont codés sur 64 bits ; le premier bit pour
coder le signe, les 11 suivants pour l’exposant, puis 52
bits pour la mantisse et le dernier, implicite.
binary, bits, bitstring, bytes, float,
integer, utf8, utf16, utf32
signed, unsigned
iex(1)> <<f::float, i::[size(32), signed]>> = <<64, 6, 102,
102, 102, 102, 102, 102, 255, 255, 254, 195>>
<<64, 6, 102, 102, 102, 102, 102, 102, 255, 255, 254, 195>>
iex(2)> f
2.8
iex(3)> i
-317
defmodule Test do
def circle(t) do
[x|tail] = t
tail ++ [x]
end
end
Test.circle [1, 2, 3]
# => [2, 3, 1]
defmodule Test do
def circle(t) do
[x|tail] = t
tail ++ [x]
end
end
Test.circle "123"
** (MatchError) no match of right
hand side value: "123"
defmodule Test do
def circle(t) when is_list(t) do
[x|tail] = t
tail ++ [x]
end
end
Test.circle [1, 2, 3]
# => [2, 3, 1]
defmodule Test do
def circle(t) when is_list(t) do
[x|tail] = t
tail ++ [x]
end
end
Test.circle "123"
** (FunctionClauseError) no function
clause matching in Test.circle/1
defmodule Test do
def circle([x|tail]) do
tail ++ [x]
end
end
Test.circle [1, 2, 3]
# => [2, 3, 1]
defmodule Test do
def circle([x|tail]) do
tail ++ [x]
end
end
Test.circle "123"
** (FunctionClauseError) no function
clause matching in Test.circle/1
defprotocol Test do
def circle(l)
end
defprotocol Test do
def circle(l)
end
defimpl Test, for: List do
def circle(l) do
[x|tail] = l
tail ++ [x]
end
end
defprotocol Test do
def circle(l)
end
defimpl Test, for: List do
def circle(l) do
[x|tail] = l
tail ++ [x]
end
end
Test.circle([1, 2, 3])
#=> [2, 3, 1]
Test.circle("123")
** (UndefinedFunctionError) undefined
function: Test.BitString.circle/1
Test.circle("123")
** (UndefinedFunctionError) undefined
function: Test.BitString.circle/1
defimpl Test, for: BitString do
def circle(l) do
[x|tail] = String.to_char_list!(l)
list_to_bitstring(tail ++ [x])
end
end
Test.circle("123")
** (UndefinedFunctionError) undefined
function: Test.BitString.circle/1
defimpl Test, for: BitString do
def circle(l) do
[x|tail] = String.to_char_list!(l)
list_to_bitstring(tail ++ [x])
end
end
Test.circle("123")
#=> "231"
defmodule Test do
defmacro times(n, body) do
quote do:
Enum.each(
1..unquote(n),
fn(_) -> unquote(body) end
)
end
end
require Test
Test.times(10) do
IO.puts "hello"
end
# => hello 10 times
Process
defmodule Math do
def sum do
receive do
{:sum, a, b} ->
IO.puts "#{a} + #{b} = #{a+b}"
sum()
_ -> IO.puts "not implemented!"
end
end
end
pid = spawn(fn -> Math.sum end)
pid <- {:sum, 4, 7}
# => 4 + 7 = 11
pid <- {:mult, 4, 7}
# => not implemented!
|>
x = [1, 2, 3, 4]
x = Enum.filter(x, fn(e) -> rem(e, 2) == 0 end)
x = Enum.reduce(x, 0, fn(e, r) -> e + r end)
|>
x = [1, 2, 3, 4]
x = Enum.filter(x, fn(e) -> rem(e, 2) == 0 end)
x = Enum.reduce(x, 0, fn(e, r) -> e + r end)
x = [1, 2, 3, 4]
|> Enum.filter(fn(e) -> rem(e, 2) == 0 end)
|> Enum.reduce(0, fn(e, r) -> e + r end)
|>
x = [1, 2, 3, 4]
x = Enum.filter(x, fn(e) -> rem(e, 2) == 0 end)
x = Enum.reduce(x, 0, fn(e, r) -> e + r end)
x = [1, 2, 3, 4]
|> Enum.filter(fn(e) -> rem(e, 2) == 0 end)
|> Enum.reduce(0, fn(e, r) -> e + r end)
x = [1, 2, 3, 4]
|> Enum.filter(&(rem(&1, 2) == 0))
|> Enum.reduce(0, &(&1 + &2))
Mix
mix new my_app
cd my_app
mix deps.get
mix compile
mix test
Web
• cowboy : http://ninenines.eu
• dynamo : https://github.com/elixir-lang/dynamo
• weber : http://0xax.github.io/weber/
Resources
http://elixir-lang.org
https://github.com/glejeune/ews
Elixir @ Paris.rb

Elixir @ Paris.rb

  • 1.
  • 2.
    Elixir José Valim -co-fondateur de Plataformatec (http://plataformatec.com.br) Proposer la puissance de la machine virtuelle Erlang d’une manière différente aux développeurs. Premier commit date de janvier 2011 Version actuelle : 0.10.2 / 0.10.3-dev Release finale est prévue pour juin 2014
  • 3.
    Erlang : wget http://www.erlang.org/download/otp_src_R16B02.tar.gz tarzxf otp_src_R16B02.tar.gz cd otp_src_R16B02 ./configure make sudo make install Elixir : git clone https://github.com/elixir-lang/elixir.git cd elixir make sudo make install
  • 4.
    Les variables Erlang : •/^(?:_[a-zA-Z0-9_]|[A-Z])[a-zA-Z0-9_]*$/ • single assignment Elixir : • /^(?:_[a-zA-Z0-9_]|[a-z])[a-zA-Z0-9_]*$/ • re assignment
  • 5.
    Erlang : 1> A= [1, 2, 3]. [1, 2, 3] 2> A = [4, 5, 6]. ** exception error: no match of right hand side value [4,5,6] Elixir : iex(1)> a = [1, 2, 3] [1, 2, 3] iex(2)> a = [4, 5, 6] [4, 5, 6] Les variables
  • 6.
    iex(1)> a =[1, 2, 3] [1, 2, 3] iex(2)> [b, c, d] = a [1, 2, 3] iex(3)> b 1 iex(4)> c 2 iex(4)> d 3
  • 7.
    iex(1)> a =[1, 2, 3] [1, 2, 3] iex(2)> [b | tail] = a [1, 2, 3] iex(3)> b 1 iex(4)> tail [2, 3]
  • 8.
    atomes :an_atom :"an atom!" booléens true false listes [104,101,108,108,111]= ‘hello’ tuples {1, :tuple, ‘de’, "données"}
  • 9.
    fonctions my_fun = fn(x)-> x*x end my_fun.(3) regex %r/erlang|elixir/ range [1..10] hash [key1: "value1", key2: 3, ...] [{"Une clé", "sa valeur»}, ...]
  • 10.
    Les binaires :séquence d’entiers (0-255 / 8 bits) placée entre << et >>
  • 11.
    Les binaires :séquence d’entiers (0-255 / 8 bits) placée entre << et >> <<104, 101, 108, 108, 111>>
  • 12.
    Les binaires :séquence d’entiers (0-255 / 8 bits) placée entre << et >> <<[104, 101, 108, 108, 111]>> <<104, 101, 108, 108, 111>>
  • 13.
    Les binaires :séquence d’entiers (0-255 / 8 bits) placée entre << et >> <<[104, 101, 108, 108, 111]>> <<'hello'>> <<104, 101, 108, 108, 111>>
  • 14.
    Les binaires :séquence d’entiers (0-255 / 8 bits) placée entre << et >> <<[104, 101, 108, 108, 111]>> <<'hello'>> "hello" <<104, 101, 108, 108, 111>>
  • 15.
  • 16.
  • 17.
  • 18.
    <<259>> = <<3>> <<259:size(16)>>= <<1, 3>> <<x:size(16)>> = <<1, 3>>
  • 19.
  • 20.
  • 21.
    <<2.4>> <<2.4:float>> = <<64, 3,51, 51, 51, 51, 51, 51>> ** (ArgumentError) argument error :erl_eval.expr/3
  • 22.
    <<2.4>> <<2.4:float>> = <<64, 3,51, 51, 51, 51, 51, 51>> ** (ArgumentError) argument error :erl_eval.expr/3 les flottants sont codés sur 64 bits ; le premier bit pour coder le signe, les 11 suivants pour l’exposant, puis 52 bits pour la mantisse et le dernier, implicite.
  • 23.
    binary, bits, bitstring,bytes, float, integer, utf8, utf16, utf32 signed, unsigned iex(1)> <<f::float, i::[size(32), signed]>> = <<64, 6, 102, 102, 102, 102, 102, 102, 255, 255, 254, 195>> <<64, 6, 102, 102, 102, 102, 102, 102, 255, 255, 254, 195>> iex(2)> f 2.8 iex(3)> i -317
  • 24.
    defmodule Test do defcircle(t) do [x|tail] = t tail ++ [x] end end Test.circle [1, 2, 3] # => [2, 3, 1]
  • 25.
    defmodule Test do defcircle(t) do [x|tail] = t tail ++ [x] end end Test.circle "123" ** (MatchError) no match of right hand side value: "123"
  • 26.
    defmodule Test do defcircle(t) when is_list(t) do [x|tail] = t tail ++ [x] end end Test.circle [1, 2, 3] # => [2, 3, 1]
  • 27.
    defmodule Test do defcircle(t) when is_list(t) do [x|tail] = t tail ++ [x] end end Test.circle "123" ** (FunctionClauseError) no function clause matching in Test.circle/1
  • 28.
    defmodule Test do defcircle([x|tail]) do tail ++ [x] end end Test.circle [1, 2, 3] # => [2, 3, 1]
  • 29.
    defmodule Test do defcircle([x|tail]) do tail ++ [x] end end Test.circle "123" ** (FunctionClauseError) no function clause matching in Test.circle/1
  • 30.
  • 31.
    defprotocol Test do defcircle(l) end defimpl Test, for: List do def circle(l) do [x|tail] = l tail ++ [x] end end
  • 32.
    defprotocol Test do defcircle(l) end defimpl Test, for: List do def circle(l) do [x|tail] = l tail ++ [x] end end Test.circle([1, 2, 3]) #=> [2, 3, 1]
  • 33.
  • 34.
    Test.circle("123") ** (UndefinedFunctionError) undefined function:Test.BitString.circle/1 defimpl Test, for: BitString do def circle(l) do [x|tail] = String.to_char_list!(l) list_to_bitstring(tail ++ [x]) end end
  • 35.
    Test.circle("123") ** (UndefinedFunctionError) undefined function:Test.BitString.circle/1 defimpl Test, for: BitString do def circle(l) do [x|tail] = String.to_char_list!(l) list_to_bitstring(tail ++ [x]) end end Test.circle("123") #=> "231"
  • 36.
    defmodule Test do defmacrotimes(n, body) do quote do: Enum.each( 1..unquote(n), fn(_) -> unquote(body) end ) end end require Test Test.times(10) do IO.puts "hello" end # => hello 10 times
  • 37.
    Process defmodule Math do defsum do receive do {:sum, a, b} -> IO.puts "#{a} + #{b} = #{a+b}" sum() _ -> IO.puts "not implemented!" end end end pid = spawn(fn -> Math.sum end) pid <- {:sum, 4, 7} # => 4 + 7 = 11 pid <- {:mult, 4, 7} # => not implemented!
  • 38.
    |> x = [1,2, 3, 4] x = Enum.filter(x, fn(e) -> rem(e, 2) == 0 end) x = Enum.reduce(x, 0, fn(e, r) -> e + r end)
  • 39.
    |> x = [1,2, 3, 4] x = Enum.filter(x, fn(e) -> rem(e, 2) == 0 end) x = Enum.reduce(x, 0, fn(e, r) -> e + r end) x = [1, 2, 3, 4] |> Enum.filter(fn(e) -> rem(e, 2) == 0 end) |> Enum.reduce(0, fn(e, r) -> e + r end)
  • 40.
    |> x = [1,2, 3, 4] x = Enum.filter(x, fn(e) -> rem(e, 2) == 0 end) x = Enum.reduce(x, 0, fn(e, r) -> e + r end) x = [1, 2, 3, 4] |> Enum.filter(fn(e) -> rem(e, 2) == 0 end) |> Enum.reduce(0, fn(e, r) -> e + r end) x = [1, 2, 3, 4] |> Enum.filter(&(rem(&1, 2) == 0)) |> Enum.reduce(0, &(&1 + &2))
  • 41.
    Mix mix new my_app cdmy_app mix deps.get mix compile mix test
  • 42.
    Web • cowboy :http://ninenines.eu • dynamo : https://github.com/elixir-lang/dynamo • weber : http://0xax.github.io/weber/
  • 43.