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

Why Elixir? (Intro to Elixir)

Why Elixir? (Intro to Elixir)

A brief introduction of Elixir. Level: beginners.

Ventsislav Nikolov

May 22, 2017
Tweet

More Decks by Ventsislav Nikolov

Other Decks in Technology

Transcript

  1. • functional programming language • compiles to erlang bytecode •

    immutable data • dynamically typed • concurrency, distribution, fault tolerance • hot code swapping
  2. DATA TYPES (NOT ALL) 1_000_000 # Integer 314159.0e-5 # Float

    :apple # Atom "Jon Snow" # String {:error, 404} # Tuple [1, 2, 3] # (Linked) List %{name: "Jon", age: 21} # Map
  3. MODULES defmodule Math do def add(a, b) do a +

    b end defp multiply(a, b) do a * b end end # warning: function multiply/2 is unused Math.add(2, 5) # => 7 Math.multiply(2, 5) # ** (UndefinedFunctionError) function Math.multiply/2 is undefined or private
  4. = x = 1 # => 1 1 = x

    # => 1 2 = x # ** (MatchError) no match of right hand side value: 1 x = 2 # => 2
  5. DECOMPOSITION [x, y] = [1, 2] # x => 1,

    y => 2 [1, x] = [1, 2] # x => 2 [_, x] = [1, 2] # x => 2 [head | tail] = [1, 2, 3] # head => 1, tail => [2, 3] %{name: name} = %{name: "Jon Snow", age: 21} # name => "Jon Snow"
  6. LIGHTWEIGHT • created for couple of microseconds • 1-2 KB

    initial memory footprint • up to hundreds of millions processes per VM
  7. MESSAGES pid = spawn fn -> receive do msg ->

    IO.puts "received a message: #{msg}" end end send pid, "You know nothing, Jon Snow."
  8. $ iex --name one@host $ iex --name two@host $ iex

    --name three@host # in node one iex> Node.connect :two@host iex> Node.connect :three@host iex> Node.list # => [:two@host, :three@host] # in node two iex> Node.list # => [:one@host, :three@host] iex> Node.spawn :three@host, fn -> IO.puts("hello") end