Skip to content

Latest commit

 

History

History
331 lines (217 loc) · 4.31 KB

elixir_installation.md

File metadata and controls

331 lines (217 loc) · 4.31 KB

theme: Merriweather2, 1

fit


Elixir.tw

inline

https://www.facebook.com/groups/elixir.tw/ https://www.meetup.com/Taipei-Elixir-Erlang-Meetup-Group/


[fit] Elixir installation party


Levarage Erlang

inline


Distribute

Light weight process


Java, PHP, Python, JavaScript, Ruby

with multi core CPU

inline


C#1

300µs per process 50µs per message

Erlang

1µs up to 2,500 processe 3µs up to 30,00 processes 0.8µs per message


process in Erlang/Elixir =~ object instance in OO application


We do not have ONE web-server handling 2 millions sessions. We have 2 million webservers handling one session each.2

  • Joe Armstrong

Fault-tolerant

* Let it crash

inline


Update on the fly, literally.

inline


[fit] 2. Elixir Syntax Highlight


Intuitive syntax like Ruby

defmodule Math do
  def sum(a, b) do
    a + b
  end
end

Math.sum(1, 2)  #=> 3

Pattern matching

What's the "=" means, seriously?


Pattern matching basic

x = 1

# Ruby x, x, y = [1, 2, 3] # => x = 2, y = 3

[x, x, y] = [1, 2, 3] # => Error

[x, x, y] = [1, 1, 3] # => x = 1, y = 3

{m, "bar", n} = {"foo", "bar", "baz"} => m = "foo", n = "baz"

Mathmatical function

inline


Pattern matching

defmodule Factorial do
  def calc(0), do: 1
  def calc(val), do: val * calc(val - 1)
end

Factorial.calc(100)

Pattern matching, contd.

def parse(html =
            %{head: head = %{title: title, meta: _},
            body: body})
    when is_bitstring(title) do
    # body => "Hello world"
    # title => "foo"
    # head => %{title: "foo", meta: "bar"}
    # html => %{head: %{title: "foo", meta: "bar"}, body: "Hello world"}
end

parse(%{head: %{title: "foo", meta: "bar"}, body: "Hello world"})

Pipe operator: |>


Problem: useless temp variable

response = get_response(request)
body = parse_body(response, :html)
html = render(body)

html = render(parse_body(get_response(request), :html))

Pipe operator: |>

html =
  request
  |> get_response()
  |> parse_body(:html)
  |> render()

Functional


Map, Filter and Reduce

inline


Functional modules:

  • Enum: map, reduce, filter, zip, scan
  • Stream: lazy evaluation

OTP modules:

  • GenServer, Supervisor
  • Task: Partially GenServer
  • Agent: Another side of GenServer
  • Flow: Back-preasure concurrent base on GenState

Meta-programming:

marco, quote, unquote

The ability to manipulate AST

iex> quote do: 1 * 2 + 3

{:+, [context: Elixir, import: Kernel],
[{:*, [context: Elixir, import: Kernel], [1, 2]}, 3]}

doctest

defmodule Num do
  @doc """
  Demonstrate doctest feature
  ## Example
    iex> Num.is_even?(1)
    true
  """
  def is_even?(num) do
    rem(num, 2) == 0
  end
end

doc part

inline


test part

inline


Trinity

inline


3. Phoenix

TODO: mickey


4. Nerves

The IOT framework of Elixir

https://github.com/nerves-project


installation



Poncho project

.
├── firmware
│   ├── _build
│   ├── config
│   ├── deps
│   ├── lib
│   ├── mix.exs
│   └── mix.lock.rpi3
└── ui
    ├── assets
    ├── config
    ├── deps
    ├── lib
    ├── mix.exs
    └── priv

Barebone: Firmware

export MIX_TARGET=rpi3
mix deps.get
mix firmware
mix firmware.burn

Wireless update

{:nerves_firmware_http, "~> 0.4"}
mix firmware.push 192.168.1.100 --target rpi3

學習資源


Thanks

Any Questions?

Footnotes

  1. 2003's data

  2. http://goo.gl/IlcNTF