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

Intro to Elixir

Intro to Elixir

Elixir is a dynamic, functional language designed for building scalable and maintainable applications.

Angus Miller

June 01, 2016
Tweet

More Decks by Angus Miller

Other Decks in Technology

Transcript

  1. What is Elixir…? 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. Create by José Valim.
  2. Functional what...?! A style of building the structure and elements

    of computer programs, that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. Calling a function f twice with the same value for an argument x will produce the same result f(x) each time.
  3. Elixir Installation Very easier, packages and pre-built binaries for most

    platforms. See http://elixir-lang.org/install.html e.g. OSX brew install elixir
  4. Elixir REPL Once installed you have iex executable to run

    code ➜ iex Interactive Elixir (1.2.1) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> 1 + 1 2 iex(2)> IO.puts "Hello world from Elixir" Hello world from Elixir :ok
  5. Scripts Create a file with an .exs extension and run

    elixir ➜ cat sample.exs IO.puts "Hello world from Elixir" ➜ elixir sample.exs Hello world from Elixir
  6. Compiled Code Create a file with an .ex extension and

    run elixirc ➜ cat math.ex defmodule Math do def sum(a, b) do a + b end end ➜ elixirc math.ex ➜ ll -rw-r--r-- 1 angusmiller staff 1.3K Feb 23 14:34 Elixir.Math.beam -rw-r--r-- 1 angusmiller staff 57B Feb 23 14:31 math.ex -rw-r--r-- 1 angusmiller staff 34B Feb 23 14:20 sample.exs ➜ iex iex(1)> Math.sum(1,2) 3
  7. Elixir Types iex> 1 # integer iex> 0x1F # integer

    iex> 1.0 # float iex> true # boolean iex> :atom # atom / symbol iex> "elixir" # string iex> [1, 2, 3] # list iex> {1, 2, 3} # tuple
  8. Atoms Atoms are constants where their name is their own

    value. Some other languages call these symbols. iex> :hello :hello iex> :hello == :world false
  9. Lists Elixir uses square brackets to specify a list of

    values. Values can be of any type. Head and tail of a list are important and represent the first / remainder of a list. iex> [1,2,3] [1,2,3] iex> [1,2,3] ++ [4,5] [1,2,3,4,5] iex> [1,2,3,4,5] -- [4,5] [1,2,3] iex> hd [1,2,3] 1 iex> tl [1,2,3] [2,3]
  10. Tuples Elixir uses curly brackets to define tuples. Like lists,

    tuples can hold any value. Tuples store elements contiguously in memory. Accessing a tuple element per index or getting the tuple size is a fast operation (indexes start from zero) iex> tuple = {'one', 'two'} {'one', 'two'} iex> elem(tuple, 0) 'one' iex> elem(tuple, 1) 'two'
  11. Lists vs. Tuples Lists are stored in memory as linked

    lists, each element in a list holds its value and points to the following element until the end of the list is reached. This means accessing the length of a list is a linear operation Tuples are stored contiguously in memory. Getting the tuple size or accessing an element by index is fast. Updating or adding elements to tuples is expensive because it requires copying the whole tuple in memory.
  12. Modules defmodule Math do def sum(a, b) do a +

    b end def join(a, b, sep \\ “ “) do a + b end end Modules are a way to group functions They are defined using the defmodule macro Functions are defined with def Default arguments are defined with \\
  13. Recursion defmodule Math do def sum_list([head|tail], accumulator \\ 0) do

    sum_list(tail, head + accumulator) end def sum_list([] \\ [0], accumulator) do accumulator end end IO.puts Math.sum_list([1, 2, 3], 0) #=> 6 Due to immutability, loops in Elixir are written differently.
  14. Mix Mix is a build tool that ships with Elixir

    that provides tasks for creating, compiling, testing your application, managing its dependencies. Let’s build a sample… yaca (yet another chat app) ➜ mix new yaca --module Yaca * creating README.md * creating .gitignore * creating mix.exs * creating config * creating config/config.exs * creating lib * creating lib/yaca.ex * creating test * creating test/test_helper.exs * creating test/yaca_test.exs Your Mix project was created successfully. You can use "mix" to compile it, test it, and more: cd yaca mix test Run "mix help" for more commands.
  15. Running Tests Mix generates a skeleton test for us defmodule

    YacaTest do use ExUnit.Case doctest Yaca test "the truth" do assert 1 + 1 == 2 end end ➜ yaca mix test Compiled lib/yaca.ex Generated yaca app Consolidated List.Chars Consolidated String.Chars Consolidated Collectable Consolidated IEx.Info Consolidated Enumerable Consolidated Inspect . Finished in 0.06 seconds (0.06s on load, 0.00s on tests) 1 test, 0 failures Randomized with seed 943520
  16. Compiling Config is in mix.exs ➜ mix compile Compiled lib/yaca.ex

    Generated yaca app Consolidated List.Chars Consolidated Collectable Consolidated String.Chars Consolidated Enumerable Consolidated IEx.Info Consolidated Inspect
  17. Environments :dev - the one in which Mix tasks (like

    compile) run by default :test - used by mix test :prod - the one you will use to run your project in production Default environment is :dev ➜ MIX_ENV=prod mix compile