Hemslo
November 26, 2016
320

# Introduction to Elixir

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

#### Hemslo

November 26, 2016

## Transcript

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.

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

Enum.sum

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

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

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)