elixir
DESCRIPTION
An introduction into Elixir, a Ruby-like language built on Erlang. Demos at https://github.com/rob-brown/Elixir-Demos.TRANSCRIPT
![Page 1: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/1.jpg)
ElixirRobert Brown
@robby_brown@robert_brown@rob-brown
![Page 2: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/2.jpg)
What is Elixir?
A Ruby-inspired language built on the Erlang Virtual Machine
Extends Erlang with macros, pipelines, and sigils
Your next programming language
![Page 3: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/3.jpg)
What is Erlang?
Created in 1986 by Ericsson
Open sourced in 1998
Functional, concurrent language
Based on Prolog, Smalltalk, CSP, and functional programming
![Page 4: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/4.jpg)
Advantages of Erlang
Fault tolerant
Lightweight processes
Hot code swapping
“Let it crash” philosophy
![Page 5: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/5.jpg)
Advantages of Erlang
Battle-tested libraries
Soft real time
Trivial parallel processing
Trivial network protocol processing
![Page 6: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/6.jpg)
Advantages of Erlang
Pattern matching
Tail recursion
Garbage Collected
![Page 7: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/7.jpg)
Advantages of Erlang
http://www.slideshare.net/JanHenryNystrom/productivity-gains-in-erlang
![Page 8: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/8.jpg)
Who Uses Erlang?
Amazon
Yahoo!
T-Mobile
Motorola
Ericsson
Huffington Post
CouchDB
GitHub
Basho
RabbitMQ
Call of Duty
League of Legends
Goldman Sachs
http://en.wikipedia.org/wiki/Erlang_(programming_language)
![Page 9: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/9.jpg)
Why Learn Functional Programming?
The future is in parallel processing
Easier to debug
Many languages are adopting FP techniques
![Page 10: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/10.jpg)
Actor Model
Actors can be created/destroyed and send/receive messages
All state is encapsulated
In Elixir, each actor is its own process
![Page 11: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/11.jpg)
Elixir Syntax: Numbers
!
42
123.456
1_000_000
!
0b101010 (binary)
0xdeadc0de (hex)
034 (octal)
![Page 12: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/12.jpg)
Elixir Syntax: Tuples
{ 1, 2, 3 }
{ 3.14, :hello, “world” }
![Page 13: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/13.jpg)
Elixir Syntax: List
[ ]
[ 1, 2, 3 ]
[ head | tail ]
[ first, second | tail ]
![Page 14: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/14.jpg)
Elixir Syntax: Atom
:atom
:“with spaces”
![Page 15: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/15.jpg)
Elixir Syntax: Binary
“Elixir”
<<“Elixir”>>
<< 69, 108, 105, 120, 105, 114 >>
![Page 16: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/16.jpg)
Elixir Syntax: Character List
‘Elixir’
[ ?E, ?l, ?i, ?x, ?i, ?r ]
[ 69, 108, 105, 120, 105, 114 ]
![Page 17: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/17.jpg)
Elixir Syntax: Range
1..100
10..0
-10..10
![Page 18: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/18.jpg)
Elixir Syntax: Pipeline
IO.puts(“Hello world!”)
“Hello world!” |> IO.puts()
!
IO.puts(String.upcase(“Elixir”))
“Elixir” |> String.upcase() |> IO.puts()
![Page 19: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/19.jpg)
Elixir Syntax: Regex
~r“^[A-Z]$”
“101010” =~ ~r“^[01]+$”
![Page 20: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/20.jpg)
Elixir Syntax: Operators+ - * / ! = == === != !== > >= < <=
and or xor not
&& ||
[ 1, 2, 3 ] ++ [ 4, 5, 6 ]
[ 1, 2, 3 ] -- [ 2 ]
“Hello ” <> “World!”
![Page 21: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/21.jpg)
Elixir Syntax: Fn
fn (x) -> x * x end
&(&1 * &1)
!
fn (x, y) -> x + y * 2 end
&(&1 + &2 * 2)
![Page 22: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/22.jpg)
Elixir Syntax: Modules and Functionsdefmodule Demo do
def say_hello() do
IO.puts(“Hello”)
end
def say_goodbye(), do: IO.puts(“Goodbye”)
defp private_function(), do: “Top Secret”
end
![Page 23: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/23.jpg)
Pattern Matching
“=” operator does not mean “assign”
It’s the matching operator
Think of “=” in terms of math
![Page 24: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/24.jpg)
Pattern Matching
x = 42
[ a, b, c ] = [ 1, 2, 3 ]
[ d, d, e ] = [ 4, 4, 5 ]
{ ^x, y } = { 42, 99 }
![Page 25: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/25.jpg)
Pattern Matching
{ :ok, data } = File.read(“Demo.txt”)
{ :error, reason } = File.read(“Bogus.txt”)
{ a, b, _ } = Demo.do_something()
![Page 26: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/26.jpg)
Pattern Matching
def sum(list), do: _sum(list, 0)
defp _sum([], total), do: total
defp _sum([ head | tail ], total) do
_sum(tail, head + total)
end
![Page 27: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/27.jpg)
Pattern Matching
fn (x) when rem(x, 15) == 0 -> “FizzBuzz”
(x) when rem(x, 3) == 0 -> “Fizz”
(x) when rem(x, 5) == 0 -> “Buzz”
(x) -> x
end
![Page 28: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/28.jpg)
Pattern Matching
<< number::[ bitstring, size(16) ],
“ ”,
word::[ bitstring, size(48) ] >> =
“42 Elixir”
![Page 29: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/29.jpg)
PID
Process ID
Returned from spawn and spawn_link
Transfer messages with send and receive
![Page 30: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/30.jpg)
PID: spawn
pid = spawn(fn -> do_something() end)
pid = spawn(Demo, :do_something, [])
pid = spawn(&Demo.do_something/0)
pid = spawn_link(fn -> 1 / 0 end)
![Page 31: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/31.jpg)
PID: send
send(pid, 42)
send(pid, { self, :something })
send(pid, { self, fn (x) -> x * x end })
![Page 32: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/32.jpg)
PID: receivereceive do
{ from, :something } ->
send(from, { self, do_something() }
{ :EXIT, from, reason } -> IO.puts(“#{from} died by #{reason}”)
after 60 * 1000 ->
:timeout
end
![Page 33: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/33.jpg)
Questions?
![Page 34: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/34.jpg)
Demo
![Page 35: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/35.jpg)
Want to Learn More?
Elixir Lang
Elixir Cheat Sheet
Programming Elixir
Programming Erlang
![Page 36: Elixir](https://reader033.vdocument.in/reader033/viewer/2022051609/546cfd1caf7959ec228b76a1/html5/thumbnails/36.jpg)
Want to Learn More?
!
Productivity Gains In Erlang
Joe Armstrong
Evan Miller
Russel Dillin