Elixir: where to start?

Oleksandr Rozumii, Toptal

Elixir: where to start?

Oleksandr Rozumii
github.com/brain-geek
First Kyiv Elixir meetup, 2016

Elixir: where to start? gotchas and cool stuff I stumbled upon

Oleksandr Rozumii
github.com/brain-geek
First Kyiv Elixir meetup, 2016

Elixir: where to start? gotchas and cool stuff I stumbled upon brief overview

Oleksandr Rozumii
github.com/brain-geek
First Kyiv Elixir meetup, 2016

Who am I?

  1. Elixir School contributor/translator
  2. Ruby (lead) developer for >5 years
  3. Doing something with Elixir for almost a year now

Quiz

The task?

Alternatives?

Phoenix main page

But there is no such thing as a Phoenix project

Mix

      $ mix new kv --module KV

      * creating mix.exs
      * creating config/config.exs
      * creating lib/kv.ex
      * creating test/test_helper.exs
      * creating test/kv_test.exs
    

Mix

      defmodule KV.Mixfile do
        use Mix.Project
        def application do
          [applications: [:logger]]
        end
        defp deps do
          [{:postgrex, ">= 0.0.0"}]
        end
      end
    

Plug

      defmodule MyPlug do
        def init([]), do: false
        def call(conn, _opts), do: conn
      end
    

Plug

      defmodule App.Router do
      use App.Web, :router
      use Plug.ErrorHandler

      pipeline :browser do
        plug :accepts, ["html"]
        plug :fetch_session
        plug :fetch_flash
        plug :protect_from_forgery
        plug :put_secure_browser_headers
      end
    

Did I mention it's all immutable?

Uh... I was going to give an overview

Programming elixir
Metaprogramming elixir
Programming phoenix

Pattern matching

Pattern matching

      iex> x = 1
      1
    
      iex> 1 = x
      1
    
      iex> 2 = x
      ** (MatchError) no match of right hand side value: 1
    

Pattern matching

      # Lists
      iex> list = [1, 2, 3]
      iex> [1, 2, 3] = list
      [1, 2, 3]
      iex> [] = list
      ** (MatchError) no match of right hand side value: [1, 2, 3]
    

Pattern matching

      iex> [1|tail] = list
      [1, 2, 3]
      iex> tail
      [2, 3]
      iex> [2|_] = list
      ** (MatchError) no match of right hand side value: [1, 2, 3]
    

Pattern matching - Fibonacci

      defmodule Fib do 
        def fib(0) do 0 end
        def fib(1) do 1 end
        def fib(n) do fib(n-1) + fib(n-2) end
      end
    

Pattern matching in real world

      def tick(state, []) do
        state
      end

      def tick(state, state) do
        state
      end

      def tick(prev_state, curr_state) do
    

Pattern matching in real world

      def event_text(%{"event" => "end", "label" => match_label}) do
        I18n.t!("en", "bot.unavailable", label: label)
      end

      def event_text(%{"event" => "start", "label" => match_label}) do
        I18n.t!("en", "bot.started", label: label)
      end

      def event_text(details = %{"match_id" => id, "event" => event}) do
    
Elixir School

And there are supervisors!

Wrong supervisor
Wrong supervisor
Supervisor

Supervisors

      children = [
        supervisor(App.Endpoint, []),
        worker(App.Repo, []),
        supervisor(App.EventStore.Supervisor, []),
      ]
      opts = [strategy: :one_for_one, name: App.Supervisor]
      Supervisor.start_link(children, opts)
    

Thank you!

Fork me on GitHub