Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elixir - The New Kid on the Block

Teemu Harju
November 19, 2015

Elixir - The New Kid on the Block

Introduction to Elixir programming. My presentation at the "Not only object-oriented" track of TopConf Tallinn 2015.

Teemu Harju

November 19, 2015
Tweet

More Decks by Teemu Harju

Other Decks in Programming

Transcript

  1. Elixir - The New Kid on the Block TopConf Tallinn

    2015 Teemu Harju Senior Software Engineer Futurefly Ltd
  2. Elixir Language • v1.0.0 released on Sep 2014 • v1.1.0

    released on Sep 2015 • > 300 contributors on GitHub • Created by José Valim
  3. Elixir is Functional • No side-effects. • No shared state.

    • Immutable data. • Functions transform data.
  4. Basic Types • Arbitrary-sized integers: integer = 123456789 • Floating-point

    numbers: float = 1.23456789 • Booleans: is_ok = true • Atoms: atom = :this_is_atom • Strings: string = “this is a string” • Ranges: range = 0..100
  5. Basic Types • Tuples: tuple = {1, “string”, :atom} •

    Lists: list = [1, “string”, :atom] • Maps: map = %{:key => “value”} • Binaries: binary = <<0, 1, 2, 3, 4, 5>>
  6. Lists vs Tuples • Lists in Elixir are implemented as

    linked lists. Accessing other elements than the head of the list is linear operation (O(N)). Updating is fast as long as we are prepending to the list. • Tuples are stored contiguously in memory. Accessing elements is fast (O(1)), but updating is slow because the entire tuple needs to be copied.
  7. Basic Operators • Arithmetic: +, -, *, / • List

    manipulation: ++ and -- • String concatenation: <> • Boolean: and, or, not, &&, ||, ! • Comparison: ==, <, >, !=, ===, !==, <=, >=
  8. Pattern Matching • The match operator can be used for

    destructing more complex data structures.
  9. Pattern Matching • You can also pattern match on a

    head an tail of a list using | operator. You can also use the same operator for prepending items on a list.
  10. Modules • Elixir named functions are grouped into modules and

    are identified by their name and arity (the number of arguments).
  11. Protocols • Elixir supports polymorphism via protocols. • By far

    the most used one is the Enumerable protocol. • Other built-in protocols String.Chars, Inspect and Collectable.
  12. Immutable Data • All Elixir data types are immutable. Functions

    are used to transform the data. Here we just rebind the data to variable called numbers.
  13. Pipe-operator |> • The Elixir pipe operator passes the result

    of the previous function call as the first argument of the next.
  14. Immutable Data (Nested Data Structures) • The Elixir provides some

    handy macros for handling nested data types. E.g. put_in and get_in.
  15. Elixir is Concurrent • Designed from the core for building

    soft-realtime systems. • Message passing concurrency. • Lightweight processes. • Pre-emptive scheduling. • Parallel garbage collection.
  16. Runs on Erlang VM “Few languages have the mystique of

    Erlang, the concurrency language that makes hard things easy and easy things hard.” - Bruce Tate
  17. Runs on Erlang VM • Elixir compiles to BEAM bytecode.

    • There is no conversion cost for calling Erlang code from Elixir and vice-versa. • Elixir ecosystem benefits greatly from existing Erlang ecosystem.
  18. OTP - Open Telecom Platform • Scary sounding name, not

    much to do with telecom actually. • Framework for building distributed, fault tolerant applications. • This is really good stuff and Elixir gets this for free and builds on top of it.
  19. Applications • Elixir “libraries” are OTP Applications. If you follow

    the OTP principles and you should. • Do not build monolithic applications that do everything and then some. • Applications should be reusable/testable components. A sort of micro service architecture, if you will.
  20. Fault tolerance • We cannot catch all bugs anyways, so

    we avoid “defensive programming” where we try to catch all different errors and handle them gracefully. • It’s better to “fail fast” and “let it crash”. • Let Supervisors restart crashed processes. • Processes can also monitor other processes and be notified if they crash.
  21. Supervisors • Supervisors are setup in a “supervision tree”. •

    Tree consists of Supervisors or Workers. • Workers are always at the leaves of the tree. • The “restart strategy” of a Supervisor can be configured to handle restarts of it’s children.
  22. OTP Releases • Releases provide nicely contained environment which we

    can use to deploy our applications. • The release contains everything including the ERTS as well as Elixir and your dependencies next to your own application. • Everything is compiled and ready scripts are provided for managing your server application. • You’ll need Mix plugin “exrm” for making OTP releases from your Elixir projects.
  23. Elixir Toolchain • This is where Elixir really shines. •

    Tool called mix does all you need and more.
  24. Phoenix Framework • Phoenix is a framework for building HTML5

    apps, API backends and distributed systems. Written in Elixir, you get beautiful syntax, productive tooling and a fast runtime. • Channels provide real-time streaming within Phoenix for building rich, interactive experiences across browsers, native mobile apps, and embedded devices. • http://phoenixframework.org