elixir for aspiring erlang developers

Download Elixir for aspiring Erlang developers

Post on 09-Feb-2017

1.836 views

Category:

Engineering

1 download

Embed Size (px)

TRANSCRIPT

  • ELIXIR FOR ASPIRING ERLANG DEVSTorben Dohrn

    @nexusger

    http://nexusger.de

    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 Grtner

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

    http://www.htw-berlin.de/http://people.f4.htw-berlin.de/lehrende/gaertner.html

  • 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

    =<

  • 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

  • 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 its 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 isnt 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