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) fin...
ABOUT ME
Torben Dohrn
Master student at the University of
Applied Sciences Berlin
.NET Developer at a small company
I did ...
GOALS OF THIS PRESENTATION
After this presentation you …
Know about the Elixir programming language and some differences t...
AGENDA
What is Elixir?
Erlang compared to Elixir
Elixir extras
Creating a Hello World app
Phoenix
Phoenix creating a Hello...
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...
SELF-DISPLAY ON ELIXIR-LANG.COM
„Elixir is a dynamic, functional language designed for building scalable and
maintainable ...
DYNAMIC
Similar to Erlang, Elixir is dynamically typed.
Also similar to Erlang there are function and type specifications....
FUNCTIONAL
Elixir is a functional language.
You have all the good parts of Erlang
(pattern matching, guards, immutable
dat...
SCALABLE
As Erlang code, Elixir code runs in lightweight execution threads (called processes)
that are isolated (“share no...
MAINTAINABLE
The combination of several aspects makes Elixir code maintainable:
- functional programming paradigm: Shorter...
ERLANG VM
Elixir gets compiled to Erlang bytecode (BEAM)
No overhead in call to Erlang functions
Using of Erlang tools (e....
WEB DEVELOPMENT
Several Erlang webserver available (e.g. Cowboy, httpd)
Several web frameworks available (e.g. phoenix, re...
EMBEDDED SOFTWARE DOMAIN
As Erlang runs on ARM platforms so does Elixir
Raspberry Pi, Beagle Board Black, AR Parrot Drone ...
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 ...
A MORE “USED TO” APPROACH
In general the syntax was brought closer to “modern“ languages.
Expression terminators are gone ...
OPERATORS
In comparison to Erlang, Elixir omits the two operators AND and OR.
The AND and OR operators in Elixir behave li...
VARIABLES AND ATOMS
Erlang
Variables starts with uppercase letter.
Atom starts with lowercase letter.
Elixir
Variables sta...
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...
MODULES AND FUNCTIONS
In Erlang you have to explicitly export a function to make it public.
In Elixir you have to define a...
IMMUTABLE DATA, YET REASSIGN IS POSSIBLE
All data is immutable, yet this is allowed:
The data is still immutable. You just...
BINARIES
Erlang Elixir Usage
erl iex REPL
erlc elixirc Compiler
erl elixir Executable
(rebar) mix Build tool
COPYRIGHT TOR...
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 def...
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_awesom...
PIPE OPERATOR |>
The pipe operator |> is syntactical
sugar to create more comprehensive
pipelines by reversing the “flow“ ...
PIPE OPERATOR |>
The argument on the left side of |> is
introduced as the first parameter of the
function call on the righ...
DOCTESTS
Did you ever encounter a well documented function where the documentation did not
correspond to the function it s...
DOCTESTS
Doctests can help you with this situation.
On including examples, Elixir will create a unit test which will point...
DOCTESTS
The command mix test will fail because the not matching documentation.
To include Doctests, write in the document...
PROTOCOLS
Constructs which allow Elixir to have polymorphism.
defprotocol Nice do
@doc "Returns true if Torben likes the t...
CREATING A HELLO WORLD APP
Showing some of the features
Not necessary the most useful
app…
COPYRIGHT TORBEN DOHRN. ALL CON...
SING ALONG
Install Elixir - http://elixir-
lang.org/install.html
Testing installation: iex / iex.bat (on
Windows)
 IO.put...
SING ALONG – HELLO WORLD
defmodule HelloWorld do
def sayhello do
IO.puts("Hello World")
end
end
We create a module HelloWo...
SING ALONG – GETTING TEXT
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
IO.puts(message...
SING ALONG – LOOP
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
IO.puts(message)
sayhel...
SING ALONG – CALL ERLANG METHODS
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
message1...
SING ALONG – REASSIGN VARIABLE (HERESY)
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
m...
SING ALONG – TIDY UP
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
IO.puts(:string.to_u...
SING ALONG – EXTRACT FUNCTION
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
IO.puts(upp...
SING ALONG – PIPE OPERATOR
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
message
|>uppe...
SING ALONG – DOCTESTS
defmodule HelloWorld do
def sayhello do
message = IO.gets("State your business“)
message
|>upper
|>I...
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...
FAST?
Request are measured in microseconds.
For a reason.
The same site (dynamically rendered by
Phoenix and as static HTM...
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 / HT...
COMPONENTS I
Endpoint
 Handles all aspects of the request up to the
router
 The glue between OTP and Phoenix
Router
 Di...
COMPONENTS II
Channels
 Manage sockets for soft-real time features.
 Uses WebSockets or LongPolling
 Similar to control...
FLOW OF DATA
An Endpoint receives a request which gets dispatched to the router
The router decides which controller (and a...
ASSET BUILDING
Phoenix has a optional dependency to node.js because it uses brunch.io
Brunch.io is a asset build tool for ...
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
...
GREETINGS
Edit the template hello_world_site/web/templates/page/index.html.eex
Create markup with calling function in the ...
TEST OUR GREETINGS
Before implementing the greeting, lets
create a test.
Edit the ~/test/views/
page_view_test.exs and wri...
GREETINGS IN ACTION
After starting the server with
mix phoenix.server we can observe
our change in the default page:
http:...
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 a...
WHO IS USING ELIXIR?
Pinterest (Image curation)
Puppet Labs (Automation)
22cans (Games)
Undead labs (Games)
+ 42 additiona...
COMMUNITY
The Elixir community is as far as I can tell great.
Core contributors answer on the mailing list (José Valim ans...
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 tra...
WHAT I DIDN'T COVER
Sigils – handle textual representations (regex, string interpolation…)
Meta Programing with Macros – W...
ELIXIR FOR ASPIRING ERLANG DEVS
Torben Dohrn
@nexusger
http://nexusger.de
COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-...
SOURCES
Elixir self-display http://elixir-lang.com
Drawbacks on Ruby/Rails https://medium.com/@kenmazaika/why-im-betting-o...
Nächste SlideShare
Wird geladen in …5
×

Elixir for aspiring Erlang developers

2.436 Aufrufe

Veröffentlicht am

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

Veröffentlicht in: Ingenieurwesen
  • Hope you are doing good. We have an urgent requirement for Elixir/Erlang Developer with our client in California with Long term Full time only. If you are Interested please let me know asap. nancy@rootshellinc.com
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

Elixir for aspiring Erlang developers

  1. 1. ELIXIR FOR ASPIRING ERLANG DEVS Torben Dohrn @nexusger http://nexusger.de
  2. 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. 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. 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. 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. 6. WHAT IS ELIXIR? COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 6
  7. 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. 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. 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. 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. 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. 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. 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. 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. 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. 16. ERLANG COMPARED TO ELIXIR COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 16
  17. 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. 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. 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. 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. 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. 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. 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. 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. 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. 26. ELIXIR EXTRAS COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 26
  27. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 45. PHOENIX FRAMEWORK COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 45
  46. 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. 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. 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. 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. 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. 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. 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. 53. CREATING A HELLO WORLD SITE COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 53
  54. 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. 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. 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. 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. 58. ELIXIR OR NOT ELIXIR? When to use Elixir. And when not. COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 58
  59. 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. 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. 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. 62. SUMMARY COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 62
  63. 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. 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. 65. ELIXIR FOR ASPIRING ERLANG DEVS Torben Dohrn @nexusger http://nexusger.de COPYRIGHT TORBEN DOHRN. ALL CONTENT UNDER CC-BY-SA 65
  66. 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

×