Functional Programming With Elixir
Functional Programming With Elixir
2
What is Elixir ?
● Functional, Concurrent, General Purpose
● Created by José Valim @ Plataformatec
○ Former Rails Core Team member
○ Author of Crafting Rails 4 Application
● Run on Erlang VM
● Stable release: 1.3.1
● Used by: Pinterest, Dockyard, Bleacher Report, ...
3
What is Elixir ? - Erlang
● Developed by Ericsson, ~1986
● Same characteristic as Elixir
● Used in Telephone Applications
● BEAM VM
○ A virtual machine to run Erlang
○ Interfaces to the “outside” world
● Backed of WhatApp, Amazon’s SimpleDB, ...
○ http://highscalability.com/blog/2014/2/26/the-whatsapp-architecture-facebook-bought-for-19
-billion.html
○ https://blog.whatsapp.com/196/1-million-is-so-2011
● Battle-tested !
4
Functional vs Imperative Programming
● Modules ● Objects
● Immutable ● Mutable
● Pure functions ● Methods
○ No side-effect ○ Can have side-effect
● Stateless ? ● Stateful
● Declarative ● Imperative
○ Expressions ○ Statements
● Erlang, Haskell, Clojure, ... ● Python, Ruby, Java
5
Elixir’s Syntax - Types
Integer 1234 0xcafe 0177 0b100 10_000
Range a..b
6
Elixir’s Syntax - Ruby like
7
Elixir’s Syntax
● First class functions - High order functions
○ Functions that can either take other functions as arguments or return them as results
○ Macro
○ Homoiconicity
● Anonymous functions
transformation
transformation
8
Elixir’s Syntax - Pipe operator (1)
● |>
○ passes the result of an expression as the first parameter of another expression.
● First argument: Data to transform
● Function composing
● Normal (bad) code
9
Elixir’s Syntax - Pipe operator (2)
● Nested code
● Pipe code
10
Elixir’s Syntax - Pattern Matching (1)
● the = operator is actually a match operator -> binding + rebinding
11
Elixir’s Syntax - Pattern Matching (2)
● Function signature matching
12
Elixir’s Syntax - Looping
● Looping through recursion, tail-call
● Iteration
○ Enum.reduce
○ Enum.map
○ Enum.filter
○ ...
13
Why Elixir ? Processes
● Processes
○ Lightweight, low cost (~1KB)
○ Isolated, concurrent
○ Thousand of processes
● Communication by message-passing
○ Inbox
15
Why Elixir ? Fast (2)
● Let’s see some
benchmark
○ https://github.com/mroth/
phoenix-showdown
16
Why Elixir ? Fault-tolerant (1)
● Todo Server Example
○ Everyone have a Todo
○ Todo: List of todo items
● Use cache when DB
isn’t reachable
● One user’s todo list do
not affect other’s todo
list
17
Why Elixir ? Fault-tolerant (2)
● “Let it crash”
● Link and Monitor
● OTP
● Supervisor Trees -
GenServer
● Restart Strategy
○ one_for_one
○ simple_one_for_one
○ rest_for_one
18
Elixir’s Ecosystem
● Hex - The package manager for the Erlang ecosystem
● Mix - Elixir’s build tool
○ = Rake + Bundler + RubyGems
● Call outside program via Ports or NIF
19
Elixir’s Ecosystem
20
Learning Resources
● Elixir Getting Started
● Elixir Schools
● ElixirConf @ youtube
● #elixir @ confreaks.tv
● exercism.io
● vietnamrb.slack.com
● Elixir-lang.slack.com
● Erlang stuffs !
● ...
21
Q&A
22
Thank you !
23