Slide 1

Slide 1 text

Elixir Ruby syntax and Lisp macro on Erlang VM

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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/

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

Elixir Features Pattern Match Pipeline Concurrent Macro

Slide 6

Slide 6 text

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]

Slide 7

Slide 7 text

Pattern Match fib(0) = 0 fib(1) = 1 fib(n) = fib(n -1) + fib(n - 2)

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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”.

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Pipeline 1..100000 | *3 | filter odd | sum

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Concurrent parent = self child = spawn(fn -> send(parent, {:hello, self()}) end) receive do {:hello, pid} -> "Got hello from #{inspect pid}" end

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Metaprogramming quote unquote macro

Slide 20

Slide 20 text

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]}

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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)

Slide 23

Slide 23 text

Elixir frameworks Phoenix http://www.phoenixframework.org/