Thinking Differently
With Elixir
by Kiran Danduprolu
Elixir
- Dynamically typed
- Functional programming language
- Immutable Data
- Compiles to Erlang Bytecode
- Created by Jose Valim in 2012
- Hit 1.0 in Sept 2014
- Current version: 1.1.0
- Ruby inspired syntax
- Inspired by Ruby, Clojure
Erlang
- Dynamically typed
- Functional programming language
- Compiles to Erlang Bytecode
- Runs on Erlang VM
- Current implementation is BEAM
- Created in Ericsson in 80s even more Java
Erlang Design Philosophy
- Scalability
- Low latency
- Fault tolerant
- High availability
- Concurrent
Companies using Erlang
- Amazon
- Yahoo!
- Heroku
- Github
- and more
Problems with Erlang
- Retro syntax inspired by Scheme, Prolog
- Inconsistencies in Standard API
- Difficult learning curve
- Slow string operations until Erlang 17.0
Why use Elixir?
- All the good stuff of Erlang
- Interoperability with Erlang and Erlang libraries
- Better tooling
- Better syntax
- Polymorphism, inspired by Clojure
- Metaprogramming abilities
Elixir types
- Integers
- Floating point numbers
- Atoms
- Tuples
- Lists
- Binaries
- Ranges
- Maps
- HashDict
- PID
- References
- Ports
Integers
- Arbitrary sized integers
a = 100000000000000000
b = 1_000_000_000
hex = 0xcadbef
bin = 0b010101010
oct = 0o146723Floating point numbers
- Bullet One
- Bullet Two
- Bullet Three
Functions
Module, name, arity
Anonymous functions
- Can live outside modules
- Are not named
- Arity is defined
iex> sum = fn (a, b) -> a + b end
iex> sum.(5,6)
11
iex> sum.(1.0, 5)
6.0
iex> mul = fn
(a, b) -> a * b
end
iex> square = fn a -> a * a endGuard Clauses
iex> sum = fn (a,b) when is_integer(a) and is_integer(b) -> a + b end
iex> sum.(5,6)
11
iex> sum.(1.0, 5)
** (FunctionClauseError) no function clause matching in :erl_eval."-inside-an-interpreted-fun-"/2
iex> mul = fn (a,b) when is_number(a) and is_number(b) -> a * b end
iex> mul.(5,6)
30
iex> mul.(1.0, 5)
5.0when
Pattern matching
iex> func = fn (:sum, {a,b}) -> a + b end
iex> func.(:sum, {5, 6})
11
iex> func.(:mul, {5, 6})
iex> head = fn [h | _t] = list -> {list, h} end
iex> head.([1,2,3])
{[1, 2, 3], 1}iex> sum = &(&1 + &2)
iex> sum.(5, 6)
11
iex> sum.(1.0, 5)
6.0
iex> mul = &(&1 * &2)
iex> mul.(5, 6)
30
iex> ## Try defining cube using & operator& operator
Function clauses
- Every function (named or anonymous) can have multiple clauses
- Each function clause can take advantage of pattern matching and guard clauses
iex> math = fn
(:sum, {a, b}) -> a + b
(:mul, {a, b}) -> a * b
(:sub, {a, b}) -> a - b
(:abs, a) when a < 0 -> -a
(:abs, a) when a > 0 -> a
(:div, {a, b}) when b != 0 -> a / b
end
iex> math.(:sum, {5,6})
11
iex> math.(:abs, -5)
5
iex> math.(:sub, {5,6})
-1
iex> math.(:abs, 0)
???Named functions
- Live inside modules
- Are named
- Arity is defined
- Can have multiple function clauses
defmodule MyMath do
# Simple function
def sum(a, b) do
a + b
end
# Function with guard clauses
def div(a, b) when is_number(a) and is_number(b) and b != 0 do
a / b
end
# Short form
def mul(a, b), do: a * b
# Function with multiple clauses
def abs(a) when a < 0, do: -a
def abs(a) when a >= 0, do: a
endThinking Differently in Elixir
By kirandanduprolu
Thinking Differently in Elixir
- 101