Elixir

36
Elixir Robert Brown @robby_brown @robert_brown @rob-brown

description

An introduction into Elixir, a Ruby-like language built on Erlang. Demos at https://github.com/rob-brown/Elixir-Demos.

Transcript of Elixir

Page 2: Elixir

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

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

Advantages of Erlang

Fault tolerant

Lightweight processes

Hot code swapping

“Let it crash” philosophy

Page 5: Elixir

Advantages of Erlang

Battle-tested libraries

Soft real time

Trivial parallel processing

Trivial network protocol processing

Page 6: Elixir

Advantages of Erlang

Pattern matching

Tail recursion

Garbage Collected

Page 7: Elixir

Advantages of Erlang

http://www.slideshare.net/JanHenryNystrom/productivity-gains-in-erlang

Page 8: Elixir

Who Uses Erlang?

Amazon

Yahoo!

Facebook

T-Mobile

Motorola

Ericsson

WhatsApp

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

Why Learn Functional Programming?

The future is in parallel processing

Easier to debug

Many languages are adopting FP techniques

Page 10: Elixir

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

Elixir Syntax: Numbers

!

42

123.456

1_000_000

!

0b101010 (binary)

0xdeadc0de (hex)

034 (octal)

Page 12: Elixir

Elixir Syntax: Tuples

{ 1, 2, 3 }

{ 3.14, :hello, “world” }

Page 13: Elixir

Elixir Syntax: List

[ ]

[ 1, 2, 3 ]

[ head | tail ]

[ first, second | tail ]

Page 14: Elixir

Elixir Syntax: Atom

:atom

:“with spaces”

Page 15: Elixir

Elixir Syntax: Binary

“Elixir”

<<“Elixir”>>

<< 69, 108, 105, 120, 105, 114 >>

Page 16: Elixir

Elixir Syntax: Character List

‘Elixir’

[ ?E, ?l, ?i, ?x, ?i, ?r ]

[ 69, 108, 105, 120, 105, 114 ]

Page 17: Elixir

Elixir Syntax: Range

1..100

10..0

-10..10

Page 18: Elixir

Elixir Syntax: Pipeline

IO.puts(“Hello world!”)

“Hello world!” |> IO.puts()

!

IO.puts(String.upcase(“Elixir”))

“Elixir” |> String.upcase() |> IO.puts()

Page 19: Elixir

Elixir Syntax: Regex

~r“^[A-Z]$”

“101010” =~ ~r“^[01]+$”

Page 20: Elixir

Elixir Syntax: Operators+ - * / ! = == === != !== > >= < <=

and or xor not

&& ||

[ 1, 2, 3 ] ++ [ 4, 5, 6 ]

[ 1, 2, 3 ] -- [ 2 ]

“Hello ” <> “World!”

Page 21: Elixir

Elixir Syntax: Fn

fn (x) -> x * x end

&(&1 * &1)

!

fn (x, y) -> x + y * 2 end

&(&1 + &2 * 2)

Page 22: Elixir

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

Pattern Matching

“=” operator does not mean “assign”

It’s the matching operator

Think of “=” in terms of math

Page 24: Elixir

Pattern Matching

x = 42

[ a, b, c ] = [ 1, 2, 3 ]

[ d, d, e ] = [ 4, 4, 5 ]

{ ^x, y } = { 42, 99 }

Page 25: Elixir

Pattern Matching

{ :ok, data } = File.read(“Demo.txt”)

{ :error, reason } = File.read(“Bogus.txt”)

{ a, b, _ } = Demo.do_something()

Page 26: Elixir

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

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

Pattern Matching

<< number::[ bitstring, size(16) ],

“ ”,

word::[ bitstring, size(48) ] >> =

“42 Elixir”

Page 29: Elixir

PID

Process ID

Returned from spawn and spawn_link

Transfer messages with send and receive

Page 30: Elixir

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

PID: send

send(pid, 42)

send(pid, { self, :something })

send(pid, { self, fn (x) -> x * x end })

Page 32: Elixir

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

Questions?

Page 34: Elixir

Demo