Elixir introd

of 17 /17
Elixir Crash Intro dennis 2015.10.23

Embed Size (px)

Transcript of Elixir introd

Page 1: Elixir introd

Elixir Crash Introdennis 2015.10.23

Page 2: Elixir introd

What is Elixir?• http://elixir-lang.org/

• Erlang VM + Ruby + Lisp(or clojure?)

• Sexy syntax: ruby style, functional

• low-latency, distributed and fault-tolerant systems on Erlang VM

• Actor Model

• Macros ,Protocols and Sigils

• More tools such as iex, mix, hex etc.

Page 3: Elixir introd

https://github.com/elixir-lang/elixir https://github.com/josevalim

Page 4: Elixir introd

Functionalsquare = fn x -> x*x end

square.(3) => 9

Enum.map(1..5, &(&1 * &1)) => [1, 4, 9, 16, 25]

defmodule MyList do

def map(list, f), do: _map(list, f, [])

defp _map([], _f, ret) do

:lists.reverse(ret)

end

defp _map([head | tail], f, ret) do

_map(tail, f, [f.(head) | ret])

end

end

Page 5: Elixir introd

Pattern Matching

[head | tail] = [1, 2, 3]

{a, b, c} = {:hello, "world", 42}

[a, [b,_,_], c] = [1, [2,3,4], 3]

x = 1; ^x=2

Page 6: Elixir introd

Actor

pid = spawn fn -> 1 + 2 end

send self(), {:hello, “world"}

receive do

{:hello, msg} -> msg

{:world, msg} -> "won't match”

end

Page 7: Elixir introd

GenServer

Page 8: Elixir introd

GenServer

• https://gist.github.com/killme2008/7d358688a6a922c1dbab

• GenServers are the go-to abstraction for building generic servers in both Elixir and OTP.

Page 9: Elixir introd

Supervisor

• https://gist.github.com/killme2008/a1ddf2b0fee80f6e137b

• A process which supervises other processes called child processes

Page 10: Elixir introd

Supervisor Tree• :observer.start()

Page 11: Elixir introd
Page 12: Elixir introd

Macros

• https://gist.github.com/killme2008/54381ae5f1bb2642d0cf

• Custom syntax

• DSL

Page 13: Elixir introd

Protocols

• Protocols are a mechanism to achieve polymorphism in Elixir

• https://gist.github.com/killme2008/f192804eee700511642c

Page 14: Elixir introd

Sigils

• "HELLO" =~ ~r/hello/i => true

• ~s(this is a string with "double" quotes, not 'single' ones)

• ~w(foo bar bat)

• https://gist.github.com/killme2008/d8345301536980d1b64a

Page 15: Elixir introd

Tools

• iex : REPL

• mix: Build tool

• hex: Package manager

• Elixir.ExUnit: Unit Test Framework

Page 16: Elixir introd

Receipt

• K/V Store

• http://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html

• Phoenix Hello World

• http://blog.fnil.net/blog/hello/

Page 17: Elixir introd