Elixir
-
Upload
robert-brown -
Category
Technology
-
view
552 -
download
5
description
Transcript of Elixir
ElixirRobert Brown
@robby_brown@robert_brown@rob-brown
What is Elixir?
A Ruby-inspired language built on the Erlang Virtual Machine
Extends Erlang with macros, pipelines, and sigils
Your next programming language
What is Erlang?
Created in 1986 by Ericsson
Open sourced in 1998
Functional, concurrent language
Based on Prolog, Smalltalk, CSP, and functional programming
Advantages of Erlang
Fault tolerant
Lightweight processes
Hot code swapping
“Let it crash” philosophy
Advantages of Erlang
Battle-tested libraries
Soft real time
Trivial parallel processing
Trivial network protocol processing
Advantages of Erlang
Pattern matching
Tail recursion
Garbage Collected
Advantages of Erlang
http://www.slideshare.net/JanHenryNystrom/productivity-gains-in-erlang
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)
Why Learn Functional Programming?
The future is in parallel processing
Easier to debug
Many languages are adopting FP techniques
Actor Model
Actors can be created/destroyed and send/receive messages
All state is encapsulated
In Elixir, each actor is its own process
Elixir Syntax: Numbers
!
42
123.456
1_000_000
!
0b101010 (binary)
0xdeadc0de (hex)
034 (octal)
Elixir Syntax: Tuples
{ 1, 2, 3 }
{ 3.14, :hello, “world” }
Elixir Syntax: List
[ ]
[ 1, 2, 3 ]
[ head | tail ]
[ first, second | tail ]
Elixir Syntax: Atom
:atom
:“with spaces”
Elixir Syntax: Binary
“Elixir”
<<“Elixir”>>
<< 69, 108, 105, 120, 105, 114 >>
Elixir Syntax: Character List
‘Elixir’
[ ?E, ?l, ?i, ?x, ?i, ?r ]
[ 69, 108, 105, 120, 105, 114 ]
Elixir Syntax: Range
1..100
10..0
-10..10
Elixir Syntax: Pipeline
IO.puts(“Hello world!”)
“Hello world!” |> IO.puts()
!
IO.puts(String.upcase(“Elixir”))
“Elixir” |> String.upcase() |> IO.puts()
Elixir Syntax: Regex
~r“^[A-Z]$”
“101010” =~ ~r“^[01]+$”
Elixir Syntax: Operators+ - * / ! = == === != !== > >= < <=
and or xor not
&& ||
[ 1, 2, 3 ] ++ [ 4, 5, 6 ]
[ 1, 2, 3 ] -- [ 2 ]
“Hello ” <> “World!”
Elixir Syntax: Fn
fn (x) -> x * x end
&(&1 * &1)
!
fn (x, y) -> x + y * 2 end
&(&1 + &2 * 2)
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
Pattern Matching
“=” operator does not mean “assign”
It’s the matching operator
Think of “=” in terms of math
Pattern Matching
x = 42
[ a, b, c ] = [ 1, 2, 3 ]
[ d, d, e ] = [ 4, 4, 5 ]
{ ^x, y } = { 42, 99 }
Pattern Matching
{ :ok, data } = File.read(“Demo.txt”)
{ :error, reason } = File.read(“Bogus.txt”)
{ a, b, _ } = Demo.do_something()
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
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
Pattern Matching
<< number::[ bitstring, size(16) ],
“ ”,
word::[ bitstring, size(48) ] >> =
“42 Elixir”
PID
Process ID
Returned from spawn and spawn_link
Transfer messages with send and receive
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)
PID: send
send(pid, 42)
send(pid, { self, :something })
send(pid, { self, fn (x) -> x * x end })
PID: receivereceive do
{ from, :something } ->
send(from, { self, do_something() }
{ :EXIT, from, reason } -> IO.puts(“#{from} died by #{reason}”)
after 60 * 1000 ->
:timeout
end
Questions?
Demo
Want to Learn More?
Elixir Lang
Elixir Cheat Sheet
Programming Elixir
Programming Erlang
Want to Learn More?
!
Productivity Gains In Erlang
Joe Armstrong
Evan Miller
Russel Dillin