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

Introduction to Elixir

B31e7d599425a916c337953af9bbb57e?s=47 Hemslo
November 26, 2016

Introduction to Elixir

Introduction to Elixir, shared on https://ruby-china.org/topics/31616

B31e7d599425a916c337953af9bbb57e?s=128

Hemslo

November 26, 2016
Tweet

Transcript

  1. Elixir Ruby syntax and Lisp macro on Erlang VM

  2. Elixir Elixir is a dynamic, functional language designed for building

    scalable and maintainable applications. IO.puts "Hello world" http://elixir-lang.org/ https://github.com/elixir-lang/elixir https://github.com/josevalim
  3. Erlang Erlang is a programming language used to build massively

    scalable soft real-time systems with requirements on high availability. Some of its uses are in telecoms, banking, e-commerce, computer telephony and instant messaging. Erlang's runtime system has built-in support for concurrency, distribution and fault tolerance. io:format("~s~n", ["Hello world!"]). http://www.erlang.org/ http://joearms.github.io/
  4. OTP OTP is set of Erlang libraries and design principles

    providing middle-ware to develop these systems. It includes its own distributed database, applications to interface towards other languages, debugging and release handling tools.
  5. Elixir Features Pattern Match Pipeline Concurrent Macro

  6. Pattern Match You had to unlearn the algebraic meaning of

    = when you first came across assignment in imperative programming languages. Now’s the time to un-unlearn it. a = 1 [a, b, c ] = [1, 2, [3, 4, 5]] [a, a] = [1, 1] [head | tail] = [1, 2, 3, 4, 5]
  7. Pattern Match fib(0) = 0 fib(1) = 1 fib(n) =

    fib(n -1) + fib(n - 2)
  8. Pattern Match def fib(0), do: 0 def fib(1), do: 1

    def fib(n), do: fib(n-1) + fib(n-2)
  9. Pattern Match Write a program that prints the numbers from

    1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
  10. Pattern Match defmodule FizzBuzz do def fizzbuzz(n), do: _fizzbuzz n,

    rem(n, 3), rem(n, 5) defp _fizzbuzz(_, 0, 0), do: "FizzBuzz" defp _fizzbuzz(_, 0, _), do: "Fizz" defp _fizzbuzz(_, _, 0), do: "Buzz" defp _fizzbuzz(n, _, _), do: n end
  11. Pipeline 1..100000 | *3 | filter odd | sum

  12. Pipeline 1..100_000 |> Enum.map(&(&1 * 3)) |> Enum.filter(&(Integer.is_odd(&1) )) |>

    Enum.sum
  13. Pipeline 1..100 |> Enum.map(&FizzBuzz.fizzbuzz/1) |> Enum.each(&IO.puts/1)

  14. Concurrent parent = self child = spawn(fn -> send(parent, {:hello,

    self()}) end) receive do {:hello, pid} -> "Got hello from #{inspect pid}" end
  15. Concurrent defmodule Parallel do def pmap(collection, fun) do me =

    self collection |> Enum.map(fn (elem) -> spawn_link fn -> (send me, { self, fun.(elem) }) end end) |> Enum.map(fn (pid) -> receive do { ^pid, result } -> result end end) end end
  16. Concurrent defmodule Parallel2 do def pmap(collection, func) do collection |>

    Enum.map(&(Task.async(fn -> func.(&1) end))) |> Enum.map(&Task.await/1) end end
  17. Concurrent defmodule Parallel3 do def pmap(collection, fun) do me =

    self collection |> Enum.map(fn (elem) -> spawn_link fn -> (send me, { self, fun.(elem) }) end end) |> Enum.map(fn (_) -> receive do { _, result } -> result end end) end end
  18. Concurrent f1 = fn -> 1..40 |> Enum.reverse |> Enum.map(&Fib.fib/1)

    end f2 = fn -> 1..40 |> Enum.reverse |> Parallel.pmap(&Fib.fib/1) end f3 = fn -> 1..40 |> Enum.reverse |> Parallel2.pmap(&Fib.fib/1) end f4 = fn -> 1..40 |> Enum.reverse |> Parallel3.pmap(&Fib.fib/1) end :timer.tc f1 :timer.tc f2 :timer.tc f3 :timer.tc f4
  19. Metaprogramming quote unquote macro

  20. Metaprogramming quote do: 2 + 1 n = 10 Macro.to_string(quote

    do: n + 1) Macro.to_string(quote do: unquote(n) + 1) Code.eval_quoted {:+, [], [n, 1]}
  21. Metaprogramming defmodule Unless do def fun_unless(clause, do: expression) do if(!clause,

    do: expression) end defmacro macro_unless(clause, do: expression) do quote do if(!unquote(clause), do: unquote(expression)) end end end
  22. Metaprogramming expr = quote do: Unless.macro_unless(true, do: IO.puts "this should

    never be printed") res = Macro.expand_once(expr, __ENV__) IO.puts Macro.to_string(res)
  23. Elixir frameworks Phoenix http://www.phoenixframework.org/