SlideShare a Scribd company logo
1 of 66
Download to read offline
ELIXIR FOR ASPIRING ERLANG DEVS
Torben Dohrn
@nexusger
http://nexusger.de
BACKGROUND
This presentation is aimed at students of the bachelors degree course Applied
Computer Science who (almost) finished the course “Advanced Programming Concepts
- Functional Programming with Erlang”
This presentation was created for the course “Independent Coursework” at the
University of Applied Sciences Berlin.
Supervisor was Professor Dr.-Ing. Hendrik Gärtner
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 2
ABOUT ME
Torben Dohrn
Master student at the University of
Applied Sciences Berlin
.NET Developer at a small company
I did the “Advanced Programming
Concepts” course around two years ago
Interested in different computer science
topics
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 3
GOALS OF THIS PRESENTATION
After this presentation you …
Know about the Elixir programming language and some differences to Erlang
Can read simple Elixir code
Are able to write a simple “Hello World” program in Elixir
Are able to write a simple “Hello World“ web site with Elixir and Phoenix
Can decide when to use Elixir and when not
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 4
AGENDA
What is Elixir?
Erlang compared to Elixir
Elixir extras
Creating a Hello World app
Phoenix
Phoenix creating a Hello World site
Elixir or not Elixir
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 5
WHAT IS ELIXIR?
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 6
SHORT HISTORY
Started in January 2011 by José Valim, a Ruby on Rails core committer
As influential Ruby/Rails is to modern web development, it has architectural
drawbacks
 Memory consumption can be quite high (unicorn/unicorn-worker-killer)
 Multi threading is generally avoided
 Can be slow
To overcome the limitations José Valim tried to bring the joy of Rails to the Erlang VM
 The idea of Elixir was born
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 7
SELF-DISPLAY ON ELIXIR-LANG.COM
„Elixir is a dynamic, functional language designed for building scalable and
maintainable applications.
Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-
tolerant systems, while also being successfully used in web development and the
embedded software domain.“
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 8
DYNAMIC
Similar to Erlang, Elixir is dynamically typed.
Also similar to Erlang there are function and type specifications.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 9
FUNCTIONAL
Elixir is a functional language.
You have all the good parts of Erlang
(pattern matching, guards, immutable
data) but some “more modern” syntax
and good tooling.
No side effects
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 10
SCALABLE
As Erlang code, Elixir code runs in lightweight execution threads (called processes)
that are isolated (“share nothing“) and exchange information via messages.
Because of their lightweight nature one can have hundreds of thousands of processes
on one machine.
Because of the isolation all processes can be garbage collected independently  no
system wide pauses.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 11
MAINTAINABLE
The combination of several aspects makes Elixir code maintainable:
- functional programming paradigm: Shorter, cleaner code
- tooling (mix, linter, Dialyzer)
- Testing (ExUnit, DocTests)
- Umbrella projects (Split your project in several smaller projects)
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 12
ERLANG VM
Elixir gets compiled to Erlang bytecode (BEAM)
No overhead in call to Erlang functions
Using of Erlang tools (e.g. rebar integration)
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 13
WEB DEVELOPMENT
Several Erlang webserver available (e.g. Cowboy, httpd)
Several web frameworks available (e.g. phoenix, relax)
Plug – Specification for modules between web applications
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 14
EMBEDDED SOFTWARE DOMAIN
As Erlang runs on ARM platforms so does Elixir
Raspberry Pi, Beagle Board Black, AR Parrot Drone 2.0
Nerves-Project, bakeware: Cross compiling for different platforms
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 15
ERLANG COMPARED TO ELIXIR
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 16
ERLANG COMPARED TO ELIXIR
Erlang and Elixir share some similarities. Think of Erlang with some differences.
Some of these differences are shown in the following slides.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 17
A MORE “USED TO” APPROACH
In general the syntax was brought closer to “modern“ languages.
Expression terminators are gone – line break (or optional semicolon) instead of dot.
Functions from modules are called with dot syntax: String.reverse() instead colon
string:reverse().
Functions parameters can have default values.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 18
OPERATORS
In comparison to Erlang, Elixir omits the two operators AND and OR.
The AND and OR operators in Elixir behave like the Erlang ANDALSO and ORELSE.
Other operators: Erlang Elixir Usage
=:= === Match operator
=/= !== Negative match operator
/= != Not equals
=< <= Less than or equals
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 19
VARIABLES AND ATOMS
Erlang
Variables starts with uppercase letter.
Atom starts with lowercase letter.
Elixir
Variables starts with lowercase letter.
Atom starts with a colon.
Literals with uppercase letter are called
atom aliases (and are atoms with the
value “Elixir.XXX”).
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 20
COMPREHENSIONS
Erlang
pyth(N) ->
[ {A,B,C} ||
A <- lists:seq(1,N),
B <- lists:seq(1,N),
C <- lists:seq(1,N),
A+B+C =< N,
A*A+B*B == C*C
].
Elixir
def pyth (n) do
for a <- 1..n,
b <- 1..n,
c <- 1..n,
a + b + c <= n,
a*a + b*b == c*c,
do: {a, b, c}
end
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 21
MODULES AND FUNCTIONS
In Erlang you have to explicitly export a function to make it public.
In Elixir you have to define a function as private, otherwise it‘s public by default.
Anonymous functions are defined with fn() -> … end instead fun() -> …end
If you assign a anonymous function to a variable you have to call it with a .prefix
hello = fn() -> "world" end
hello.()
"world"
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 22
IMMUTABLE DATA, YET REASSIGN IS POSSIBLE
All data is immutable, yet this is allowed:
The data is still immutable. You just
change the meaning of the label “a”
“In Elixir, once a variable references a
list such as [1,2,3], you know it will
always reference those same values
(until you rebind the variable).”
Dave Thomas – Programming Elixir
Implicit changing of the value isn’t
possible.
a = "hello"
a = "world"
iex> a = "hello"
iex> SomeThing.magic(a)
iex> a
"hello"
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 23
BINARIES
Erlang Elixir Usage
erl iex REPL
erlc elixirc Compiler
erl elixir Executable
(rebar) mix Build tool
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 24
REPL
In Erlang if you want to define a module you need to create a file first and compile
that file. In Elixir you can define modules directly in the REPL.
Short codes for working in iex
 h(ModuleName) – HELP. Prints the documentation of the module (instead of the history).
 c(FileName) – COMPILE. Compiles and loads file
 r(ModuleName) – RELOAD. Compiles and loads file of ModuleName
 i(variable, function, Module…) – INSPECT. Show the type and some info.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 25
ELIXIR EXTRAS
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 26
MIX
Command line build and dependency tool
 Various tasks are executed with mix
 Tasks can be extended
mix new my_awesome_app – Create a new Elixir program
mix compile – Compiles the program in the current folder
mix release – An extended Task. Only available if exrm is included
mix phoenix.server – Starts a phoenix site in development mode
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 27
PIPE OPERATOR |>
The pipe operator |> is syntactical
sugar to create more comprehensive
pipelines by reversing the “flow“ of
data.
Pseudocode + execution order:
Trim(Reverse(To_upper(" Hallo")))
--3-(----2-(-----1---(---0---)))
The pseudocode on the left can be
expressed with the pipe operator as
following:
Expression Execution order
" Hallo" 0
|>To_upper 1
|>Reverse 2
|>Trim 3
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 28
PIPE OPERATOR |>
The argument on the left side of |> is
introduced as the first parameter of the
function call on the right side.
The usefulness gets even more obvious if
we have multiple parameter:
A(B(C(D("E"),"F"),"G","H"),"I")
Which parameter belongs to which function?
"E"
|>D
|>C("F")
|>B("G","H")
|>A("I")
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 29
DOCTESTS
Did you ever encounter a well documented function where the documentation did not
correspond to the function it self? Maybe a refactoring not taking the docs into
account? Something like this?
@doc """
This function multiplies three
numbers
"""
def multiplyWithSeven(oneNumber)
do
oneNumber*7
end
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 30
DOCTESTS
Doctests can help you with this situation.
On including examples, Elixir will create a unit test which will point the developer to
inconsistencies between documentation and code.
@doc """
This function multiplies three numbers
iex> multipyThreeNumbers(2,2,2)
8
"""
def multiplyWithSeven(oneNumber) do
oneNumber*7
end
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 31
DOCTESTS
The command mix test will fail because the not matching documentation.
To include Doctests, write in the documentation of the function
iex> functionName prefixed by four spaces.
In the next line you write the result.
Multiple examples are considered as one test if there is no empty line between them.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 32
PROTOCOLS
Constructs which allow Elixir to have polymorphism.
defprotocol Nice do
@doc "Returns true if Torben likes the type"
def nice?(data)
end
defimpl Nice, for: List do
def nice?([]), do: true
def nice?(_), do: false
end
iex> Nice.nice?([])
true
defimpl Nice, for: Map do
def nice?({}), do: true
def nice?(_), do: false
end
defimpl Nice, for: Integer do
def nice?(_), do: false
end
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 33
CREATING A HELLO WORLD APP
Showing some of the features
Not necessary the most useful
app…
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 34
SING ALONG
Install Elixir - http://elixir-
lang.org/install.html
Testing installation: iex / iex.bat (on
Windows)
 IO.puts "Hello world"
Creating a new project:
 mix new hello_world
edit .hello_worldlibhello_world.ex
Compile
 mix compile
 mix test
To execute, run
 iex –S mix
 HelloWorld.sayhello()
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 35
SING ALONG – HELLO WORLD
defmodule HelloWorld do
def sayhello do
IO.puts("Hello World")
end
end
We create a module HelloWorld
(actually, mix new did that for us
already).
In this module we create a function
sayhello which sends "Hello World"
to the console.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 36
SING ALONG – GETTING TEXT
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
IO.puts(message)
end
end
IO.puts put text to the console (or files
or what ever).
IO.gets gets text from the console.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 37
SING ALONG – LOOP
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
IO.puts(message)
sayhello
end
end
After outputting the text we simply call
the same function again.
No need for fully qualified name, as
long as we are in the same scope.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 38
SING ALONG – CALL ERLANG METHODS
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
message1 = :string.to_upper(to_char_list(message))
IO.puts(message1)
sayhello
end
end Here the Erlang built-in
string:to_upper function is called.
(Elixir built-in function is also available).
Because Erlang expects char_lists we
have to convert our string to char_list
before usage.
All Erlang built-in functions
are available.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 39
SING ALONG – REASSIGN VARIABLE (HERESY)
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
message = :string.to_upper(to_char_list(message))
IO.puts(message)
sayhello
end
end In Elixir you can.
The data is still immutable. You just
change the label to a different value.
In Erlang we can‘t label
different values with the
same variable name.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 40
SING ALONG – TIDY UP
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
IO.puts(:string.to_upper(to_char_list(message)))
sayhello
end
end
Obviously we don‘t need the
intermediate variable here.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 41
SING ALONG – EXTRACT FUNCTION
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
IO.puts(upper(message))
sayhello
end
def upper(message) do
:string.to_upper(to_char_list(message))
end
end
Even if our function is not long, it is
easier to maintain if you use
severall small functions instead of
one big one.
(And I need the function for the
next slides…)
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 42
SING ALONG – PIPE OPERATOR
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
message
|>upper
|>IO.puts
sayhello
end
def upper(message) do
:string.to_upper(to_char_list(message))
end
end
Instead of IO.puts(upper(message)) which
gets applied from inner to outer:
3(2(1))
We can use the pipe operator. The intent of
of the function gets more clear this way.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 43
SING ALONG – DOCTESTS
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
message
|>upper
|>IO.puts
sayhello
end
@doc """
Upcases text. What did you expect?
iex> HelloWorld.upper("Hallo")
‘HALLO’
"""
def upper(message) do
:string.to_upper(to_char_list(message))
end
end
Now we add documentation to the function
upper.
With the addition of an example not only a
developer knows how this use this function, the
compiler does as well.
mix test now executes this doctest as well
See mix test --trace for a more detailed
view
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 44
PHOENIX FRAMEWORK
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 45
WHAT IS THE PHOENIX FRAMEWORK?
Phoenix is a web framework written in
Elixir and runs in the Erlang VM.
What is it good for?
 HTML5 apps
 API back ends
 Distributed systems
Features:
 Server side MVC
 Real time support via channels
 Precompiled templates
 Fast
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 46
FAST?
Request are measured in microseconds.
For a reason.
The same site (dynamically rendered by
Phoenix and as static HTML copy served
by Apache) had notable speed
differences:
Average response time:
 Apache 900 microseconds
 Phoenix 300 microseconds
My mini pseudo benchmark is not
scientific significant! Improvements
possible!
Virtual Machine: Ubuntu 14.04
Case A) Phoenix 1.1.1 + Elixir 1.2,
Production release
Case B) Static copy of the Phoenix site
served by Apache2
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 47
FAST?
Apache2 Phoenix
665 05/Jan/2016 "GET / HTTP/1.1" 200
499 05/Jan/2016 "GET / HTTP/1.1" 200
1657 05/Jan/2016 "GET / HTTP/1.1" 200
998 05/Jan/2016 "GET / HTTP/1.1" 200
942 05/Jan/2016 "GET / HTTP/1.1" 200
1381 05/Jan/2016 "GET / HTTP/1.1" 200
23:18:34.611 [info] Sent 200 in 225µs
23:18:35.000 [info] Sent 200 in 206µs
23:18:35.406 [info] Sent 200 in 336µs
23:18:35.798 [info] Sent 200 in 221µs
23:18:36.167 [info] Sent 200 in 449µs
23:18:36.562 [info] Sent 200 in 174µs
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 48
COMPONENTS I
Endpoint
 Handles all aspects of the request up to the
router
 The glue between OTP and Phoenix
Router
 Dispatches incoming requests to the correct
controller
 Applies pipelines (groups of plugs)
Controllers
 Prepare data and send it to views
 Invoke rendering via Views
Views
 Render templates
 Provide helper function for use in template
Templates
 Markup with aforementioned helper functions
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 49
COMPONENTS II
Channels
 Manage sockets for soft-real time features.
 Uses WebSockets or LongPolling
 Similar to controllers but with bi-directional
communication on persistent connection.
PubSub
 Allows channel clients (ios, android, javascript,
c#) to subscribe to topics
Plug
 “Plug is a specification for constructing
composable modules to build web applications”
 Examples: Authentication, logging,
preprocessing
 Multiple plugs create a pipeline
Ecto
 Database wrapper
 Query composition
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 50
FLOW OF DATA
An Endpoint receives a request which gets dispatched to the router
The router decides which controller (and action) handles this request (+ pipeline)
The corresponding controller action invoke the view
The view renders the templates
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 51
ASSET BUILDING
Phoenix has a optional dependency to node.js because it uses brunch.io
Brunch.io is a asset build tool for web assets
It can execute various plugins but in the default configuration “only” joins all *.css and
*.js files to app.css and app.js correspondingly to reduce the amount of request to the
server.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 52
CREATING A HELLO WORLD SITE
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 53
GETTING STARTED
Install phoenix (and node.js and Erlang
and Elixir…)
 http://www.phoenixframework.org/docs/install
ation
Create a new Phoenix app in the current
folder
 mix phoenix.new hello_world_site --no-ecto
Only at the first time usage
 Brunch.io gets downloaded
Test the installation
 mix test – executes the default tests
 mix phoenix.server – starts the server
On http://localhost:4000 the default site
is shown
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 54
GREETINGS
Edit the template hello_world_site/web/templates/page/index.html.eex
Create markup with calling function in the div block with the class “jumbotron”
 <p><%= sayhello %></p>
The sayhello function will be defined in the page_view. Because all the templates in
…/page/ get rendered from the “page_view” view, we don’t have to qualify the
module name
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 55
TEST OUR GREETINGS
Before implementing the greeting, lets
create a test.
Edit the ~/test/views/
page_view_test.exs and write a test
for the function:
Execute the test in the console with
mix test
Obviously our test fails. Lets change that:
Edit the corresponding view:
hello_world_site/web/views/page_v
iew.ex
Execute the tests again. It should run.
def sayhello do
"Hello World"
end
test "says hello" do
assert HelloWorldSite.PageView.sayhello
== "Hello World"
end
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 56
GREETINGS IN ACTION
After starting the server with
mix phoenix.server we can observe
our change in the default page:
http://localhost:4000
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 57
ELIXIR OR NOT ELIXIR? When to use Elixir. And when
not.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 58
SO WHEN SHOULD I USE ELIXIR?
You should consider Elixir when…
… you are curious
… you want to leverage the Erlang VM
but are intimidated of Erlang
… you come from a Ruby background
… you program fast APIs
… you value a great community
You shouldn’t consider Elixir when…
… you program desktop apps
… you program mobile apps
… your working application is in Erlang.
Elixir is not faster than Erlang.
… you need lots of libraries. Library
support is not great yet.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 59
WHO IS USING ELIXIR?
Pinterest (Image curation)
Puppet Labs (Automation)
22cans (Games)
Undead labs (Games)
+ 42 additional companies regarding to this list:
https://github.com/doomspork/elixir-companies/blob/master/README.md
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 60
COMMUNITY
The Elixir community is as far as I can tell great.
Core contributors answer on the mailing list (José Valim answered me in ~30 min)
Many Ruby developer consider Elixir the next big thing and therefore there is a lot of
posts and actions on the web right now.
There are Meetups and Conferences in Berlin  ElixirConf.eu 10.05 – 12.05.2016
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 61
SUMMARY
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 62
SUMMARY
In this presentation you saw an overview in Elixir, a programming language based on
Erlang.
It shares a lot of traits with Erlang, yet feels more “used to” because of syntax an
powerful tooling.
You saw the differences both languages have and two “Hello World” applications,
one on the console and one as a Web project.
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 63
WHAT I DIDN'T COVER
Sigils – handle textual representations (regex, string interpolation…)
Meta Programing with Macros – Write Elixir in Elixir
Umbrella Apps – Split big apps in smaller, independent tasks
Supervisor trees – Similar to Erlang, Elixir uses Supervisors to restart processes
Distributed tasks – You can send messages to processes on different nodes
Elixir on embedded devices – You can deploy your OTP app on embedded devices
Production deployment – exrm creates OTP releases. Phoenix can also be hosted
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 64
ELIXIR FOR ASPIRING ERLANG DEVS
Torben Dohrn
@nexusger
http://nexusger.de
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 65
SOURCES
Elixir self-display http://elixir-lang.com
Drawbacks on Ruby/Rails https://medium.com/@kenmazaika/why-im-betting-on-
elixir-7c8f847b58#.tnz88vbv8
Erlang on the Parrot Drone: https://www.youtube.com/watch?v=96UzSHyp0F8
Erlang code in Elixir and vice Versa: http://blakewilliams.me/posts/playing-with-
elixir-and-erlang/
Elixir variables ARE immutable: http://stackoverflow.com/questions/29967086/are-
elixir-variables-really-immutable
Companies using Elixir: https://github.com/doomspork/elixir-companies
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 66

More Related Content

What's hot

Assembler design option
Assembler design optionAssembler design option
Assembler design option
Mohd Arif
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loaders
Tech_MX
 
High Performance Erlang
High Performance ErlangHigh Performance Erlang
High Performance Erlang
didip
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
Sami Said
 

What's hot (20)

Assembler
AssemblerAssembler
Assembler
 
Assembler design option
Assembler design optionAssembler design option
Assembler design option
 
Assembler (2)
Assembler (2)Assembler (2)
Assembler (2)
 
Assembler
AssemblerAssembler
Assembler
 
C introduction
C introductionC introduction
C introduction
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loaders
 
Assembler
AssemblerAssembler
Assembler
 
Assembler1
Assembler1Assembler1
Assembler1
 
High Performance Erlang
High Performance ErlangHigh Performance Erlang
High Performance Erlang
 
The Ruby On Rails I18n Core Api
The Ruby On Rails I18n Core ApiThe Ruby On Rails I18n Core Api
The Ruby On Rails I18n Core Api
 
Erlang OTP
Erlang OTPErlang OTP
Erlang OTP
 
Sp chap2
Sp chap2Sp chap2
Sp chap2
 
Erlang
ErlangErlang
Erlang
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
 
1 hour dive into Erlang/OTP
1 hour dive into Erlang/OTP1 hour dive into Erlang/OTP
1 hour dive into Erlang/OTP
 
C++ basics
C++ basicsC++ basics
C++ basics
 
Linker and loader upload
Linker and loader   uploadLinker and loader   upload
Linker and loader upload
 
Pearl
PearlPearl
Pearl
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 

Viewers also liked

Messaging With Erlang And Jabber
Messaging With  Erlang And  JabberMessaging With  Erlang And  Jabber
Messaging With Erlang And Jabber
l xf
 
VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012
Eonblast
 

Viewers also liked (20)

Codemotion Berlin 2015 recap
Codemotion Berlin 2015   recapCodemotion Berlin 2015   recap
Codemotion Berlin 2015 recap
 
Badge Hacking with Nerves Workshop - ElixirConf 2016 - Justin Schneck and Fra...
Badge Hacking with Nerves Workshop - ElixirConf 2016 - Justin Schneck and Fra...Badge Hacking with Nerves Workshop - ElixirConf 2016 - Justin Schneck and Fra...
Badge Hacking with Nerves Workshop - ElixirConf 2016 - Justin Schneck and Fra...
 
Clojure class
Clojure classClojure class
Clojure class
 
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-SubramanyaErlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
 
Messaging With Erlang And Jabber
Messaging With  Erlang And  JabberMessaging With  Erlang And  Jabber
Messaging With Erlang And Jabber
 
Winning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test CycleWinning the Erlang Edit•Build•Test Cycle
Winning the Erlang Edit•Build•Test Cycle
 
High Performance Erlang
High  Performance  ErlangHigh  Performance  Erlang
High Performance Erlang
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Clojure values
Clojure valuesClojure values
Clojure values
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
 
20 reasons why we don't need architects (@pavlobaron)
20 reasons why we don't need architects (@pavlobaron)20 reasons why we don't need architects (@pavlobaron)
20 reasons why we don't need architects (@pavlobaron)
 
Elixir talk
Elixir talkElixir talk
Elixir talk
 
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
Clojure: Functional Concurrency for the JVM (presented at Open Source Bridge)
 
From Perl To Elixir
From Perl To ElixirFrom Perl To Elixir
From Perl To Elixir
 
NDC London 2014: Erlang Patterns Matching Business Needs
NDC London 2014: Erlang Patterns Matching Business NeedsNDC London 2014: Erlang Patterns Matching Business Needs
NDC London 2014: Erlang Patterns Matching Business Needs
 
VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Introduction to Erlang for Python Programmers
Introduction to Erlang for Python ProgrammersIntroduction to Erlang for Python Programmers
Introduction to Erlang for Python Programmers
 
Clojure, Plain and Simple
Clojure, Plain and SimpleClojure, Plain and Simple
Clojure, Plain and Simple
 
Erlang - Because S**t Happens
Erlang - Because S**t HappensErlang - Because S**t Happens
Erlang - Because S**t Happens
 

Similar to Elixir for aspiring Erlang developers

BIT204 1 Software Fundamentals
BIT204 1 Software FundamentalsBIT204 1 Software Fundamentals
BIT204 1 Software Fundamentals
James Uren
 

Similar to Elixir for aspiring Erlang developers (20)

Intro to Elixir talk
Intro to Elixir talkIntro to Elixir talk
Intro to Elixir talk
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
 
Anton Mishchuk - Multi-language FBP with Flowex
Anton Mishchuk - Multi-language FBP with FlowexAnton Mishchuk - Multi-language FBP with Flowex
Anton Mishchuk - Multi-language FBP with Flowex
 
ElixirでFPGAを設計する
ElixirでFPGAを設計するElixirでFPGAを設計する
ElixirでFPGAを設計する
 
Erlang, an overview
Erlang, an overviewErlang, an overview
Erlang, an overview
 
Advanced c programming in Linux
Advanced c programming in Linux Advanced c programming in Linux
Advanced c programming in Linux
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
Elixir introduction
Elixir introductionElixir introduction
Elixir introduction
 
Multi language FBP with Flowex by Anton Mishchuk
Multi language FBP with Flowex by Anton Mishchuk Multi language FBP with Flowex by Anton Mishchuk
Multi language FBP with Flowex by Anton Mishchuk
 
Multi-language FBP with flowex
Multi-language FBP with flowexMulti-language FBP with flowex
Multi-language FBP with flowex
 
BIT204 1 Software Fundamentals
BIT204 1 Software FundamentalsBIT204 1 Software Fundamentals
BIT204 1 Software Fundamentals
 
Elixir
ElixirElixir
Elixir
 
Lab4 scripts
Lab4 scriptsLab4 scripts
Lab4 scripts
 
Hot deployments with distillery
Hot deployments with distilleryHot deployments with distillery
Hot deployments with distillery
 
Linux
LinuxLinux
Linux
 
Beam me up, scotty (PUG Roma)
Beam me up, scotty (PUG Roma)Beam me up, scotty (PUG Roma)
Beam me up, scotty (PUG Roma)
 
OpenCL Overview Japan Virtual Open House Feb 2021
OpenCL Overview Japan Virtual Open House Feb 2021OpenCL Overview Japan Virtual Open House Feb 2021
OpenCL Overview Japan Virtual Open House Feb 2021
 
20240412 QFM010 Elixir Reading List March 2024
20240412 QFM010 Elixir Reading List March 202420240412 QFM010 Elixir Reading List March 2024
20240412 QFM010 Elixir Reading List March 2024
 

Recently uploaded

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 

Recently uploaded (20)

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 

Elixir for aspiring Erlang developers

  • 1. ELIXIR FOR ASPIRING ERLANG DEVS Torben Dohrn @nexusger http://nexusger.de
  • 2. BACKGROUND This presentation is aimed at students of the bachelors degree course Applied Computer Science who (almost) finished the course “Advanced Programming Concepts - Functional Programming with Erlang” This presentation was created for the course “Independent Coursework” at the University of Applied Sciences Berlin. Supervisor was Professor Dr.-Ing. Hendrik Gärtner COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 2
  • 3. ABOUT ME Torben Dohrn Master student at the University of Applied Sciences Berlin .NET Developer at a small company I did the “Advanced Programming Concepts” course around two years ago Interested in different computer science topics COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 3
  • 4. GOALS OF THIS PRESENTATION After this presentation you … Know about the Elixir programming language and some differences to Erlang Can read simple Elixir code Are able to write a simple “Hello World” program in Elixir Are able to write a simple “Hello World“ web site with Elixir and Phoenix Can decide when to use Elixir and when not COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 4
  • 5. AGENDA What is Elixir? Erlang compared to Elixir Elixir extras Creating a Hello World app Phoenix Phoenix creating a Hello World site Elixir or not Elixir COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 5
  • 6. WHAT IS ELIXIR? COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 6
  • 7. SHORT HISTORY Started in January 2011 by José Valim, a Ruby on Rails core committer As influential Ruby/Rails is to modern web development, it has architectural drawbacks  Memory consumption can be quite high (unicorn/unicorn-worker-killer)  Multi threading is generally avoided  Can be slow To overcome the limitations José Valim tried to bring the joy of Rails to the Erlang VM  The idea of Elixir was born COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 7
  • 8. SELF-DISPLAY ON ELIXIR-LANG.COM „Elixir is a dynamic, functional language designed for building scalable and maintainable applications. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault- tolerant systems, while also being successfully used in web development and the embedded software domain.“ COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 8
  • 9. DYNAMIC Similar to Erlang, Elixir is dynamically typed. Also similar to Erlang there are function and type specifications. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 9
  • 10. FUNCTIONAL Elixir is a functional language. You have all the good parts of Erlang (pattern matching, guards, immutable data) but some “more modern” syntax and good tooling. No side effects COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 10
  • 11. SCALABLE As Erlang code, Elixir code runs in lightweight execution threads (called processes) that are isolated (“share nothing“) and exchange information via messages. Because of their lightweight nature one can have hundreds of thousands of processes on one machine. Because of the isolation all processes can be garbage collected independently  no system wide pauses. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 11
  • 12. MAINTAINABLE The combination of several aspects makes Elixir code maintainable: - functional programming paradigm: Shorter, cleaner code - tooling (mix, linter, Dialyzer) - Testing (ExUnit, DocTests) - Umbrella projects (Split your project in several smaller projects) COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 12
  • 13. ERLANG VM Elixir gets compiled to Erlang bytecode (BEAM) No overhead in call to Erlang functions Using of Erlang tools (e.g. rebar integration) COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 13
  • 14. WEB DEVELOPMENT Several Erlang webserver available (e.g. Cowboy, httpd) Several web frameworks available (e.g. phoenix, relax) Plug – Specification for modules between web applications COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 14
  • 15. EMBEDDED SOFTWARE DOMAIN As Erlang runs on ARM platforms so does Elixir Raspberry Pi, Beagle Board Black, AR Parrot Drone 2.0 Nerves-Project, bakeware: Cross compiling for different platforms COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 15
  • 16. ERLANG COMPARED TO ELIXIR COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 16
  • 17. ERLANG COMPARED TO ELIXIR Erlang and Elixir share some similarities. Think of Erlang with some differences. Some of these differences are shown in the following slides. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 17
  • 18. A MORE “USED TO” APPROACH In general the syntax was brought closer to “modern“ languages. Expression terminators are gone – line break (or optional semicolon) instead of dot. Functions from modules are called with dot syntax: String.reverse() instead colon string:reverse(). Functions parameters can have default values. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 18
  • 19. OPERATORS In comparison to Erlang, Elixir omits the two operators AND and OR. The AND and OR operators in Elixir behave like the Erlang ANDALSO and ORELSE. Other operators: Erlang Elixir Usage =:= === Match operator =/= !== Negative match operator /= != Not equals =< <= Less than or equals COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 19
  • 20. VARIABLES AND ATOMS Erlang Variables starts with uppercase letter. Atom starts with lowercase letter. Elixir Variables starts with lowercase letter. Atom starts with a colon. Literals with uppercase letter are called atom aliases (and are atoms with the value “Elixir.XXX”). COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 20
  • 21. COMPREHENSIONS Erlang pyth(N) -> [ {A,B,C} || A <- lists:seq(1,N), B <- lists:seq(1,N), C <- lists:seq(1,N), A+B+C =< N, A*A+B*B == C*C ]. Elixir def pyth (n) do for a <- 1..n, b <- 1..n, c <- 1..n, a + b + c <= n, a*a + b*b == c*c, do: {a, b, c} end COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 21
  • 22. MODULES AND FUNCTIONS In Erlang you have to explicitly export a function to make it public. In Elixir you have to define a function as private, otherwise it‘s public by default. Anonymous functions are defined with fn() -> … end instead fun() -> …end If you assign a anonymous function to a variable you have to call it with a .prefix hello = fn() -> "world" end hello.() "world" COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 22
  • 23. IMMUTABLE DATA, YET REASSIGN IS POSSIBLE All data is immutable, yet this is allowed: The data is still immutable. You just change the meaning of the label “a” “In Elixir, once a variable references a list such as [1,2,3], you know it will always reference those same values (until you rebind the variable).” Dave Thomas – Programming Elixir Implicit changing of the value isn’t possible. a = "hello" a = "world" iex> a = "hello" iex> SomeThing.magic(a) iex> a "hello" COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 23
  • 24. BINARIES Erlang Elixir Usage erl iex REPL erlc elixirc Compiler erl elixir Executable (rebar) mix Build tool COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 24
  • 25. REPL In Erlang if you want to define a module you need to create a file first and compile that file. In Elixir you can define modules directly in the REPL. Short codes for working in iex  h(ModuleName) – HELP. Prints the documentation of the module (instead of the history).  c(FileName) – COMPILE. Compiles and loads file  r(ModuleName) – RELOAD. Compiles and loads file of ModuleName  i(variable, function, Module…) – INSPECT. Show the type and some info. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 25
  • 26. ELIXIR EXTRAS COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 26
  • 27. MIX Command line build and dependency tool  Various tasks are executed with mix  Tasks can be extended mix new my_awesome_app – Create a new Elixir program mix compile – Compiles the program in the current folder mix release – An extended Task. Only available if exrm is included mix phoenix.server – Starts a phoenix site in development mode COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 27
  • 28. PIPE OPERATOR |> The pipe operator |> is syntactical sugar to create more comprehensive pipelines by reversing the “flow“ of data. Pseudocode + execution order: Trim(Reverse(To_upper(" Hallo"))) --3-(----2-(-----1---(---0---))) The pseudocode on the left can be expressed with the pipe operator as following: Expression Execution order " Hallo" 0 |>To_upper 1 |>Reverse 2 |>Trim 3 COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 28
  • 29. PIPE OPERATOR |> The argument on the left side of |> is introduced as the first parameter of the function call on the right side. The usefulness gets even more obvious if we have multiple parameter: A(B(C(D("E"),"F"),"G","H"),"I") Which parameter belongs to which function? "E" |>D |>C("F") |>B("G","H") |>A("I") COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 29
  • 30. DOCTESTS Did you ever encounter a well documented function where the documentation did not correspond to the function it self? Maybe a refactoring not taking the docs into account? Something like this? @doc """ This function multiplies three numbers """ def multiplyWithSeven(oneNumber) do oneNumber*7 end COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 30
  • 31. DOCTESTS Doctests can help you with this situation. On including examples, Elixir will create a unit test which will point the developer to inconsistencies between documentation and code. @doc """ This function multiplies three numbers iex> multipyThreeNumbers(2,2,2) 8 """ def multiplyWithSeven(oneNumber) do oneNumber*7 end COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 31
  • 32. DOCTESTS The command mix test will fail because the not matching documentation. To include Doctests, write in the documentation of the function iex> functionName prefixed by four spaces. In the next line you write the result. Multiple examples are considered as one test if there is no empty line between them. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 32
  • 33. PROTOCOLS Constructs which allow Elixir to have polymorphism. defprotocol Nice do @doc "Returns true if Torben likes the type" def nice?(data) end defimpl Nice, for: List do def nice?([]), do: true def nice?(_), do: false end iex> Nice.nice?([]) true defimpl Nice, for: Map do def nice?({}), do: true def nice?(_), do: false end defimpl Nice, for: Integer do def nice?(_), do: false end COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 33
  • 34. CREATING A HELLO WORLD APP Showing some of the features Not necessary the most useful app… COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 34
  • 35. SING ALONG Install Elixir - http://elixir- lang.org/install.html Testing installation: iex / iex.bat (on Windows)  IO.puts "Hello world" Creating a new project:  mix new hello_world edit .hello_worldlibhello_world.ex Compile  mix compile  mix test To execute, run  iex –S mix  HelloWorld.sayhello() COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 35
  • 36. SING ALONG – HELLO WORLD defmodule HelloWorld do def sayhello do IO.puts("Hello World") end end We create a module HelloWorld (actually, mix new did that for us already). In this module we create a function sayhello which sends "Hello World" to the console. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 36
  • 37. SING ALONG – GETTING TEXT defmodule HelloWorld do def sayhello do message = IO.gets("State your business“) IO.puts(message) end end IO.puts put text to the console (or files or what ever). IO.gets gets text from the console. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 37
  • 38. SING ALONG – LOOP defmodule HelloWorld do def sayhello do message = IO.gets("State your business“) IO.puts(message) sayhello end end After outputting the text we simply call the same function again. No need for fully qualified name, as long as we are in the same scope. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 38
  • 39. SING ALONG – CALL ERLANG METHODS defmodule HelloWorld do def sayhello do message = IO.gets("State your business“) message1 = :string.to_upper(to_char_list(message)) IO.puts(message1) sayhello end end Here the Erlang built-in string:to_upper function is called. (Elixir built-in function is also available). Because Erlang expects char_lists we have to convert our string to char_list before usage. All Erlang built-in functions are available. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 39
  • 40. SING ALONG – REASSIGN VARIABLE (HERESY) defmodule HelloWorld do def sayhello do message = IO.gets("State your business“) message = :string.to_upper(to_char_list(message)) IO.puts(message) sayhello end end In Elixir you can. The data is still immutable. You just change the label to a different value. In Erlang we can‘t label different values with the same variable name. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 40
  • 41. SING ALONG – TIDY UP defmodule HelloWorld do def sayhello do message = IO.gets("State your business“) IO.puts(:string.to_upper(to_char_list(message))) sayhello end end Obviously we don‘t need the intermediate variable here. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 41
  • 42. SING ALONG – EXTRACT FUNCTION defmodule HelloWorld do def sayhello do message = IO.gets("State your business“) IO.puts(upper(message)) sayhello end def upper(message) do :string.to_upper(to_char_list(message)) end end Even if our function is not long, it is easier to maintain if you use severall small functions instead of one big one. (And I need the function for the next slides…) COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 42
  • 43. SING ALONG – PIPE OPERATOR defmodule HelloWorld do def sayhello do message = IO.gets("State your business“) message |>upper |>IO.puts sayhello end def upper(message) do :string.to_upper(to_char_list(message)) end end Instead of IO.puts(upper(message)) which gets applied from inner to outer: 3(2(1)) We can use the pipe operator. The intent of of the function gets more clear this way. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 43
  • 44. SING ALONG – DOCTESTS defmodule HelloWorld do def sayhello do message = IO.gets("State your business“) message |>upper |>IO.puts sayhello end @doc """ Upcases text. What did you expect? iex> HelloWorld.upper("Hallo") ‘HALLO’ """ def upper(message) do :string.to_upper(to_char_list(message)) end end Now we add documentation to the function upper. With the addition of an example not only a developer knows how this use this function, the compiler does as well. mix test now executes this doctest as well See mix test --trace for a more detailed view COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 44
  • 45. PHOENIX FRAMEWORK COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 45
  • 46. WHAT IS THE PHOENIX FRAMEWORK? Phoenix is a web framework written in Elixir and runs in the Erlang VM. What is it good for?  HTML5 apps  API back ends  Distributed systems Features:  Server side MVC  Real time support via channels  Precompiled templates  Fast COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 46
  • 47. FAST? Request are measured in microseconds. For a reason. The same site (dynamically rendered by Phoenix and as static HTML copy served by Apache) had notable speed differences: Average response time:  Apache 900 microseconds  Phoenix 300 microseconds My mini pseudo benchmark is not scientific significant! Improvements possible! Virtual Machine: Ubuntu 14.04 Case A) Phoenix 1.1.1 + Elixir 1.2, Production release Case B) Static copy of the Phoenix site served by Apache2 COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 47
  • 48. FAST? Apache2 Phoenix 665 05/Jan/2016 "GET / HTTP/1.1" 200 499 05/Jan/2016 "GET / HTTP/1.1" 200 1657 05/Jan/2016 "GET / HTTP/1.1" 200 998 05/Jan/2016 "GET / HTTP/1.1" 200 942 05/Jan/2016 "GET / HTTP/1.1" 200 1381 05/Jan/2016 "GET / HTTP/1.1" 200 23:18:34.611 [info] Sent 200 in 225µs 23:18:35.000 [info] Sent 200 in 206µs 23:18:35.406 [info] Sent 200 in 336µs 23:18:35.798 [info] Sent 200 in 221µs 23:18:36.167 [info] Sent 200 in 449µs 23:18:36.562 [info] Sent 200 in 174µs COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 48
  • 49. COMPONENTS I Endpoint  Handles all aspects of the request up to the router  The glue between OTP and Phoenix Router  Dispatches incoming requests to the correct controller  Applies pipelines (groups of plugs) Controllers  Prepare data and send it to views  Invoke rendering via Views Views  Render templates  Provide helper function for use in template Templates  Markup with aforementioned helper functions COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 49
  • 50. COMPONENTS II Channels  Manage sockets for soft-real time features.  Uses WebSockets or LongPolling  Similar to controllers but with bi-directional communication on persistent connection. PubSub  Allows channel clients (ios, android, javascript, c#) to subscribe to topics Plug  “Plug is a specification for constructing composable modules to build web applications”  Examples: Authentication, logging, preprocessing  Multiple plugs create a pipeline Ecto  Database wrapper  Query composition COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 50
  • 51. FLOW OF DATA An Endpoint receives a request which gets dispatched to the router The router decides which controller (and action) handles this request (+ pipeline) The corresponding controller action invoke the view The view renders the templates COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 51
  • 52. ASSET BUILDING Phoenix has a optional dependency to node.js because it uses brunch.io Brunch.io is a asset build tool for web assets It can execute various plugins but in the default configuration “only” joins all *.css and *.js files to app.css and app.js correspondingly to reduce the amount of request to the server. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 52
  • 53. CREATING A HELLO WORLD SITE COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 53
  • 54. GETTING STARTED Install phoenix (and node.js and Erlang and Elixir…)  http://www.phoenixframework.org/docs/install ation Create a new Phoenix app in the current folder  mix phoenix.new hello_world_site --no-ecto Only at the first time usage  Brunch.io gets downloaded Test the installation  mix test – executes the default tests  mix phoenix.server – starts the server On http://localhost:4000 the default site is shown COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 54
  • 55. GREETINGS Edit the template hello_world_site/web/templates/page/index.html.eex Create markup with calling function in the div block with the class “jumbotron”  <p><%= sayhello %></p> The sayhello function will be defined in the page_view. Because all the templates in …/page/ get rendered from the “page_view” view, we don’t have to qualify the module name COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 55
  • 56. TEST OUR GREETINGS Before implementing the greeting, lets create a test. Edit the ~/test/views/ page_view_test.exs and write a test for the function: Execute the test in the console with mix test Obviously our test fails. Lets change that: Edit the corresponding view: hello_world_site/web/views/page_v iew.ex Execute the tests again. It should run. def sayhello do "Hello World" end test "says hello" do assert HelloWorldSite.PageView.sayhello == "Hello World" end COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 56
  • 57. GREETINGS IN ACTION After starting the server with mix phoenix.server we can observe our change in the default page: http://localhost:4000 COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 57
  • 58. ELIXIR OR NOT ELIXIR? When to use Elixir. And when not. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 58
  • 59. SO WHEN SHOULD I USE ELIXIR? You should consider Elixir when… … you are curious … you want to leverage the Erlang VM but are intimidated of Erlang … you come from a Ruby background … you program fast APIs … you value a great community You shouldn’t consider Elixir when… … you program desktop apps … you program mobile apps … your working application is in Erlang. Elixir is not faster than Erlang. … you need lots of libraries. Library support is not great yet. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 59
  • 60. WHO IS USING ELIXIR? Pinterest (Image curation) Puppet Labs (Automation) 22cans (Games) Undead labs (Games) + 42 additional companies regarding to this list: https://github.com/doomspork/elixir-companies/blob/master/README.md COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 60
  • 61. COMMUNITY The Elixir community is as far as I can tell great. Core contributors answer on the mailing list (José Valim answered me in ~30 min) Many Ruby developer consider Elixir the next big thing and therefore there is a lot of posts and actions on the web right now. There are Meetups and Conferences in Berlin  ElixirConf.eu 10.05 – 12.05.2016 COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 61
  • 62. SUMMARY COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 62
  • 63. SUMMARY In this presentation you saw an overview in Elixir, a programming language based on Erlang. It shares a lot of traits with Erlang, yet feels more “used to” because of syntax an powerful tooling. You saw the differences both languages have and two “Hello World” applications, one on the console and one as a Web project. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 63
  • 64. WHAT I DIDN'T COVER Sigils – handle textual representations (regex, string interpolation…) Meta Programing with Macros – Write Elixir in Elixir Umbrella Apps – Split big apps in smaller, independent tasks Supervisor trees – Similar to Erlang, Elixir uses Supervisors to restart processes Distributed tasks – You can send messages to processes on different nodes Elixir on embedded devices – You can deploy your OTP app on embedded devices Production deployment – exrm creates OTP releases. Phoenix can also be hosted COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 64
  • 65. ELIXIR FOR ASPIRING ERLANG DEVS Torben Dohrn @nexusger http://nexusger.de COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 65
  • 66. SOURCES Elixir self-display http://elixir-lang.com Drawbacks on Ruby/Rails https://medium.com/@kenmazaika/why-im-betting-on- elixir-7c8f847b58#.tnz88vbv8 Erlang on the Parrot Drone: https://www.youtube.com/watch?v=96UzSHyp0F8 Erlang code in Elixir and vice Versa: http://blakewilliams.me/posts/playing-with- elixir-and-erlang/ Elixir variables ARE immutable: http://stackoverflow.com/questions/29967086/are- elixir-variables-really-immutable Companies using Elixir: https://github.com/doomspork/elixir-companies COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 66