elixir for aspiring erlang developers

66
ELIXIR FOR ASPIRING ERLANG DEVS Torben Dohrn @nexusger http:// nexusger.de

Upload: torben-dohrn

Post on 09-Feb-2017

1.858 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Elixir for aspiring Erlang developers

ELIXIR FOR ASPIRING ERLANG DEVSTorben Dohrn

@nexusger

http://nexusger.de

Page 2: Elixir for aspiring Erlang developers

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

Page 3: Elixir for aspiring Erlang developers

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

Page 4: Elixir for aspiring Erlang developers

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

Page 5: Elixir for aspiring Erlang developers

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

Page 6: Elixir for aspiring Erlang developers

WHAT IS ELIXIR?

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 6

Page 7: Elixir for aspiring Erlang developers

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

Page 8: Elixir for aspiring Erlang developers

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

Page 9: Elixir for aspiring Erlang developers

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

Page 10: Elixir for aspiring Erlang developers

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

Page 11: Elixir for aspiring Erlang developers

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

Page 12: Elixir for aspiring Erlang developers

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

Page 13: Elixir for aspiring Erlang developers

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

Page 14: Elixir for aspiring Erlang developers

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

Page 15: Elixir for aspiring Erlang developers

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

Page 16: Elixir for aspiring Erlang developers

ERLANG COMPARED TO ELIXIR

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 16

Page 17: Elixir for aspiring Erlang developers

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

Page 18: Elixir for aspiring Erlang developers

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

Page 19: Elixir for aspiring Erlang developers

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

Page 20: Elixir for aspiring Erlang developers

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

Page 21: Elixir for aspiring Erlang developers

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

Page 22: Elixir for aspiring Erlang developers

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

Page 23: Elixir for aspiring Erlang developers

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

Page 24: Elixir for aspiring Erlang developers

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

Page 25: Elixir for aspiring Erlang developers

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

Page 26: Elixir for aspiring Erlang developers

ELIXIR EXTRAS

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 26

Page 27: Elixir for aspiring Erlang developers

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

Page 28: Elixir for aspiring Erlang developers

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

Page 29: Elixir for aspiring Erlang developers

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

Page 30: Elixir for aspiring Erlang developers

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

Page 31: Elixir for aspiring Erlang developers

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

Page 32: Elixir for aspiring Erlang developers

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

Page 33: Elixir for aspiring Erlang developers

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

Page 34: Elixir for aspiring Erlang developers

CREATING A HELLO WORLD APPShowing some of the features

Not necessary the most useful

app…

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 34

Page 35: Elixir for aspiring Erlang developers

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_world\lib\hello_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

Page 36: Elixir for aspiring Erlang developers

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

Page 37: Elixir for aspiring Erlang developers

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 filesor what ever).

IO.gets gets text from the console.

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 37

Page 38: Elixir for aspiring Erlang developers

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 callthe same function again.

No need for fully qualified name, aslong as we are in the same scope.

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 38

Page 39: Elixir for aspiring Erlang developers

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_listbefore usage.

All Erlang built-in functions

are available.

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 39

Page 40: Elixir for aspiring Erlang developers

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

Page 41: Elixir for aspiring Erlang developers

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 theintermediate variable here.

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 41

Page 42: Elixir for aspiring Erlang developers

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 iseasier to maintain if you useseverall small functions instead of one big one.

(And I need the function for thenext slides…)

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 42

Page 43: Elixir for aspiring Erlang developers

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)) whichgets 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

Page 44: Elixir for aspiring Erlang developers

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

Page 45: Elixir for aspiring Erlang developers

PHOENIX FRAMEWORK

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 45

Page 46: Elixir for aspiring Erlang developers

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

Page 47: Elixir for aspiring Erlang developers

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

Page 48: Elixir for aspiring Erlang developers

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

Page 49: Elixir for aspiring Erlang developers

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

Page 50: Elixir for aspiring Erlang developers

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

Page 51: Elixir for aspiring Erlang developers

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

Page 52: Elixir for aspiring Erlang developers

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

Page 53: Elixir for aspiring Erlang developers

CREATING A HELLO WORLD SITE

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 53

Page 54: Elixir for aspiring Erlang developers

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

Page 55: Elixir for aspiring Erlang developers

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

Page 56: Elixir for aspiring Erlang developers

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

Page 57: Elixir for aspiring Erlang developers

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

Page 58: Elixir for aspiring Erlang developers

ELIXIR OR NOT ELIXIR? When to use Elixir. And when

not.

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 58

Page 59: Elixir for aspiring Erlang developers

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

Page 60: Elixir for aspiring Erlang developers

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

Page 61: Elixir for aspiring Erlang developers

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

Page 62: Elixir for aspiring Erlang developers

SUMMARY

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 62

Page 63: Elixir for aspiring Erlang developers

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

Page 64: Elixir for aspiring Erlang developers

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

Page 65: Elixir for aspiring Erlang developers

ELIXIR FOR ASPIRING ERLANG DEVSTorben Dohrn

@nexusger

http://nexusger.de

COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 65

Page 66: Elixir for aspiring Erlang developers

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