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

Elixir

 Elixir

An introduction into Elixir, a Ruby-like language built on Erlang. Demos at https://github.com/rob-brown/Elixir-Demos.

Rob Brown

May 19, 2014
Tweet

More Decks by Rob Brown

Other Decks in Technology

Transcript

  1. What is Elixir? A Ruby-inspired language built on the Erlang

    Virtual Machine Extends Erlang with macros, pipelines, and sigils Your next programming language
  2. What is Erlang? Created in 1986 by Ericsson Open sourced

    in 1998 Functional, concurrent language Based on Prolog, Smalltalk, CSP, and functional programming
  3. Who Uses Erlang? Amazon Yahoo! Facebook T-Mobile Motorola Ericsson WhatsApp

    Huffington Post CouchDB GitHub Basho RabbitMQ Call of Duty League of Legends Goldman Sachs http://en.wikipedia.org/wiki/Erlang_(programming_language)
  4. Why Learn Functional Programming? The future is in parallel processing

    Easier to debug Many languages are adopting FP techniques
  5. Actor Model Actors can be created/destroyed and send/receive messages All

    state is encapsulated In Elixir, each actor is its own process
  6. Elixir Syntax: List [ ] [ 1, 2, 3 ]

    [ head | tail ] [ first, second | tail ]
  7. Elixir Syntax: Character List ‘Elixir’ [ ?E, ?l, ?i, ?x,

    ?i, ?r ] [ 69, 108, 105, 120, 105, 114 ]
  8. Elixir Syntax: Pipeline IO.puts(“Hello world!”) “Hello world!” |> IO.puts() !

    IO.puts(String.upcase(“Elixir”)) “Elixir” |> String.upcase() |> IO.puts()
  9. Elixir Syntax: Operators + - * / ! = ==

    === != !== > >= < <= and or xor not && || [ 1, 2, 3 ] ++ [ 4, 5, 6 ] [ 1, 2, 3 ] -- [ 2 ] “Hello ” <> “World!”
  10. Elixir Syntax: Fn fn (x) -> x * x end

    &(&1 * &1) ! fn (x, y) -> x + y * 2 end &(&1 + &2 * 2)
  11. Elixir Syntax: Modules and Functions defmodule Demo do def say_hello()

    do IO.puts(“Hello”) end def say_goodbye(), do: IO.puts(“Goodbye”) defp private_function(), do: “Top Secret” end
  12. Pattern Matching “=” operator does not mean “assign” It’s the

    matching operator Think of “=” in terms of math
  13. Pattern Matching x = 42 [ a, b, c ]

    = [ 1, 2, 3 ] [ d, d, e ] = [ 4, 4, 5 ] { ^x, y } = { 42, 99 }
  14. Pattern Matching { :ok, data } = File.read(“Demo.txt”) { :error,

    reason } = File.read(“Bogus.txt”) { a, b, _ } = Demo.do_something()
  15. Pattern Matching def sum(list), do: _sum(list, 0) defp _sum([], total),

    do: total defp _sum([ head | tail ], total) do _sum(tail, head + total) end
  16. Pattern Matching fn (x) when rem(x, 15) == 0 ->

    “FizzBuzz” (x) when rem(x, 3) == 0 -> “Fizz” (x) when rem(x, 5) == 0 -> “Buzz” (x) -> x end
  17. PID: spawn pid = spawn(fn -> do_something() end) pid =

    spawn(Demo, :do_something, []) pid = spawn(&Demo.do_something/0) pid = spawn_link(fn -> 1 / 0 end)
  18. PID: receive receive do { from, :something } -> send(from,

    { self, do_something() } { :EXIT, from, reason } ->
 IO.puts(“#{from} died by #{reason}”) after 60 * 1000 -> :timeout end
  19. Want to Learn More? ! Productivity Gains In Erlang Joe

    Armstrong Evan Miller Russel Dillin