elixir introd

17
Elixir Crash Intro dennis 2015.10.23

Upload: dennis-zhuang

Post on 10-Apr-2017

634 views

Category:

Technology


0 download

TRANSCRIPT

Elixir Crash Introdennis 2015.10.23

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.

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

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

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

Actor

pid = spawn fn -> 1 + 2 end

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

receive do

{:hello, msg} -> msg

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

end

GenServer

GenServer

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

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

Supervisor

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

• A process which supervises other processes called child processes

Supervisor Tree• :observer.start()

Macros

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

• Custom syntax

• DSL

Protocols

• Protocols are a mechanism to achieve polymorphism in Elixir

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

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

Tools

• iex : REPL

• mix: Build tool

• hex: Package manager

• Elixir.ExUnit: Unit Test Framework

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/