0% found this document useful (0 votes)
112 views

Functional Programming With Elixir

This document provides an overview of the Elixir programming language. It begins with definitions of Elixir and its origins as a language that runs on the Erlang VM. Key aspects of Elixir discussed include its functional, concurrent nature and syntax similarities to Ruby. The document outlines Elixir's types, pipe operator, pattern matching, and looping functionality. Reasons why developers use Elixir are its built-in support for processes, fault tolerance, and speed. The ecosystem surrounding Elixir is also summarized, including tools like Hex and Mix. Learning resources for Elixir are listed before the document concludes with a Q&A section.

Uploaded by

elkinlon0930
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Functional Programming With Elixir

This document provides an overview of the Elixir programming language. It begins with definitions of Elixir and its origins as a language that runs on the Erlang VM. Key aspects of Elixir discussed include its functional, concurrent nature and syntax similarities to Ruby. The document outlines Elixir's types, pipe operator, pattern matching, and looping functionality. Reasons why developers use Elixir are its built-in support for processes, fault tolerance, and speed. The ecosystem surrounding Elixir is also summarized, including tools like Hex and Mix. Learning resources for Elixir are listed before the document concludes with a Q&A section.

Uploaded by

elkinlon0930
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Functional Programming With Elixir

Nguyen Anh Tien


Table of Contents
● What is Elixir ?
● Functional Programming
● Why Elixir ?
● Syntax of Elixir
● Elixir’s Ecosystem
● Q&A

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

Float 1.0 3.1415 6.02e23

Atom :foo :me@home :"with spaces"

Tuple { 1, 2, :ok, "xy" }

List [ 1, 2, 3 ] or [head | tail]

Keyword List [ a: "Foo", b: 123 ]

Map %{ key => value, key => value }

Truth true, false, nil

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

Data Function Function Data

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

● Connect to other node in another computer


● -> Distributed
14
Why Elixir ? Fast (1)
● Data copying
○ -> inefficient ?
● Garbage Collector
○ Processes have separated
heap
○ No synchronisation
● Robert Virding - Hitchhiker's
Tour of the BEAM
○ https://www.youtube.com
/watch?v=_Pwlvy3zz9M

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

● Productive - Reliable - Fast


● MVC model, Plug based
● Live reload coding
● Websocket integrated
○ http://www.phoenixframework.org/blog/the-road-to-2-million-websocket-c
onnections
○ https://dockyard.com/blog/2016/08/09/phoenix-channels-vs-rails-action-
cable

● More and more app migration from


Rails to Phoenix
○ https://speakerdeck.com/bcardarella/fro
m-rails-to-phoenix

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

You might also like