Slide 1

Slide 1 text

Elixir Сергей Нартымов Brainspec https://github.com/lest twitter: @just_lest

Slide 2

Slide 2 text

Elixir is a metaprogrammable, functional language built atop the Erlang VM.

Slide 3

Slide 3 text

Elixir

Slide 4

Slide 4 text

Metaprogramming

Slide 5

Slide 5 text

Metaprogramming iex> nil?(1) false iex> nil?(nil) true defmacro nil?(x) do quote do: unquote(x) == nil end

Slide 6

Slide 6 text

Metaprogramming iex> nil?(1) false iex> nil?(nil) true defmacro nil?(x) do quote do: unquote(x) == nil end

Slide 7

Slide 7 text

Metaprogramming if(foo, do: bar, else: baz) if foo do bar else baz end

Slide 8

Slide 8 text

Metaprogramming defmacro unless(clause, options) do do_clause = Keyword.get(options, :do, nil) else_clause = Keyword.get(options, :else, nil) quote do if(unquote(clause), do: unquote(else_clause), else: unquote(do_clause)) end end

Slide 9

Slide 9 text

Metaprogramming Lisps traditionally empowered developers because you can eliminate anything that’s tedious through macros, and that power is really what people keep going back for. — Rich Hickey

Slide 10

Slide 10 text

Functions

Slide 11

Slide 11 text

Function capture iex> list = ["foo", "bar", "baz"] ["foo","bar","baz"] iex> Enum.map list, fn(x) -> size(x) end [3,3,3] iex> Enum.map list, &size/1 [3,3,3] iex> fun = &rem(&1, 2) #Function<6.80484245 in :erl_eval.expr/5> iex> fun.(4) 0

Slide 12

Slide 12 text

Function capture iex> list = ["foo", "bar", "baz"] ["foo","bar","baz"] iex> Enum.map list, fn(x) -> size(x) end [3,3,3] iex> Enum.map list, &size/1 [3,3,3] iex> fun = &rem(&1, 2) #Function<6.80484245 in :erl_eval.expr/5> iex> fun.(4) 0

Slide 13

Slide 13 text

Partial application iex> list = ["foo", "bar", "baz"] ["foo","bar","baz"] iex> Enum.map list, fn(x) -> size(x) end [3,3,3] iex> Enum.map list, &size/1 [3,3,3] iex> fun = &rem(&1, 2) #Function<6.80484245 in :erl_eval.expr/5> iex> fun.(4) 0

Slide 14

Slide 14 text

Pipeline operator

Slide 15

Slide 15 text

f3(f2(f1(state))) state |> f1 |> f2 |> f3

Slide 16

Slide 16 text

f3(f2(f1(state))) state |> f1 |> f2 |> f3

Slide 17

Slide 17 text

list |> Enum.filter(&(&1 > 0)) # take positive numbers |> Enum.map(&(&1 * &1)) # square each one |> Enum.reduce(0, &(&1 + &2)) # calculate sum

Slide 18

Slide 18 text

Polymorphism

Slide 19

Slide 19 text

Protocols

Slide 20

Slide 20 text

Protocols defprotocol Inspect do def inspect(thing, opts) end defimpl Inspect, for: Atom do def inspect(false), do: "false" def inspect(true), do: "true" def inspect(nil), do: "nil" def inspect(atom) do # ... end end

Slide 21

Slide 21 text

Protocols defimpl Inspect, for: List do # ... end defimpl Inspect, for: Integer do # ... end

Slide 22

Slide 22 text

ExUnit A unit test framework

Slide 23

Slide 23 text

ExUnit ExUnit.start defmodule MyTest do use ExUnit.Case test "the truth" do assert true end end $ elixir assertion_test.exs

Slide 24

Slide 24 text

ExUnit ExUnit.start defmodule MyTest do use ExUnit.Case test "the truth" do assert true end end $ elixir assertion_test.exs

Slide 25

Slide 25 text

ExUnit assert 1 + 1 == 2 refute 1 + 3 == 3 assert 1 + 1 == 3 # Expected 2 to be equal to (==) 3

Slide 26

Slide 26 text

Mix A build tool

Slide 27

Slide 27 text

Mix defmodule MyProject.Mixfile do use Mix.Project def project do [ app: :my_project, version: "0.0.1", deps: deps ] end # Configuration for the OTP application def application do [] end defp deps do [] end end

Slide 28

Slide 28 text

Mix defp deps do [ { :some_project, github: "some_project/other", tag: "0.3.0" }, { :another_project, ">= 1.0.2", git: "https://example.com/another/repo.git" } ] end

Slide 29

Slide 29 text

Other

Slide 30

Slide 30 text

Other • Unicode strings

Slide 31

Slide 31 text

Other • Unicode strings • String sigils, heredocs

Slide 32

Slide 32 text

Other • Unicode strings • String sigils, heredocs • Documentation

Slide 33

Slide 33 text

Other • Unicode strings • String sigils, heredocs • Documentation • List comprehentions

Slide 34

Slide 34 text

Resources • http://elixir-lang.org/ • http://joearms.github.io/2013/05/31/a-week- with-elixir.html • http://www.theerlangelist.com/2014/01/ why-elixir.html • http://devintorr.es/blog/2013/06/11/elixir- its-not-about-syntax/

Slide 35

Slide 35 text

Спасибо https://github.com/lest twitter: @just_lest Сергей Нартымов Brainspec