Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Elixir & Phoenix 推坑

233 views

Published on

簡介 Elixir & Phoenix

Published in: Software
  • Be the first to comment

  • Be the first to like this

Elixir & Phoenix 推坑

  1. 1. Elixir A dynamic, functional language designed for building scalable and maintainable applications. 1
  2. 2. Elixir ‑ taiansu elixir or ‑ Hype Driven Development ‑ Marek Kirejczyk React, TDD is dead, Microservice, NoSQL, Elixir/Phoenix 2
  3. 3. Intro José Valim (@josevalim) Rails Core Team since early 2010 v1.0.0 ‑ Sep 10, 2014 Erlang Virtual Machine (BEAM) 3
  4. 4. Erlang (1987‑) Erlang Virtual Machine (BEAM) 1. lightweight process 2. supervisor tree ‑ fail fast, let it crash 3. Hot swapping IM/ 4
  5. 5. Toolchain nodejs Elixir package manager npm hex.pm CLI npm mix REPL node iex test jest, mocha... ExUnit (doctest) docs JSDoc ExDoc 5
  6. 6. Types Value types Collection types Arbittrary‑sized integers Tuples Floating‑point numbers Lists Atoms Maps Ranges Binaries Regular expressions number < atom < tuple < map < list < binary 6
  7. 7. Immutability ‑ All values are Immutable ‑ Copying Data iex> list1 = [3, 2, 1] [3, 2, 1] iex> list2 = [4 | list1] [4, 3, 2, 1] 7
  8. 8. Pattern Matching (1/2) Not an assignment iex> a = 1 1 iex> 1 = a 1 iex> 2 = a ** (MatchError) no match of right hand side value: 1 More Complex Matches iex> list = [1, 2, 3] [1, 2, 3] iex> [a, 2, b] = list [1, 2, 3] iex> a 1 iex> b 3 8
  9. 9. Pattern Matching (2/2) Variable Bind Once (per Match) iex> [a, a] = [1, 1] [1, 1] iex> a 1 iex> [b, b] = [1, 2] ** (MatchError) no match of right hand side value: [1, 2] pin operator: ^ iex> a = 8 8 iex> [^a, 2] = [1, 2] ** (MatchError) no match of right hand side value: [1, 2] 9
  10. 10. Anonymous Functions iex> sum = fn (a, b) ‐> a + b end iex> sum.(1, 2) 3 iex> swap = fn {a, b} ‐> {b, a} end iex> swap.({6, 8}) {8, 6} 10
  11. 11. Modules and Named Functions (1/2) Function Calls and Pattern Matching defmodule Factorial do   def of(0), do: 1   def of(n), do: n * of(n ‐ 1) end Guard Clauses defmodule Factorial do   def of(0), do: 1   def of(n) when n > 0 and is_integer(n) do     n * of(n ‐ 1)   end end 11
  12. 12. Modules and Named Functions (2/2) Default Parameters defmodule Example do   def func(p1, p2  2) do     IO.inspect [p1, p2]   end end Private Functions defmodule Factorial do   defp foo(a): do: false end 12
  13. 13. Pipe Operator |> people = DB.find_customers orders = Orders.for_customers(people) tax = sales_tax(orders, 2017) tax = DB.find_customers   |> Orders.for_customers   |> sales_tax(2017) tax = sales_tax(Orders.for_customers(DB.find_customers), 2017) 13
  14. 14. Heads & Tails iex> [head|tail] = [1, 2, 3] [1, 2, 3] iex> head 1 iex> tail [2, 3] defmodule MyList do   def len([]), do: 0   def len([head|tail]), do: 1 + len(tail) end 14
  15. 15. Concurrency current_process = self() # Spawn an Elixir process (not an operating system one!) spawn_link(fn ‐>   send current_process, {:msg, "hello world"} end) # Block until the message is received receive do   {:msg, contents} ‐> IO.puts contents end 15
  16. 16. Take Away Erlang 16
  17. 17. Reference Tutorial Try Elixir ‑ Code School elixir‑koans Site elixir‑lang hex.pm Elixir Taiwan (FB group) NewsLetter Elixir Radar 17
  18. 18. Part 2 18
  19. 19. Phoenix A productive web framework that does not compromise speed and maintainability. 19
  20. 20. Intro Chris McCord (@chris_mccord) v1.0.0 ‑ Aug 28, 2015 server‑side MVC pattern, like Rails / Django high developer productivity and high application performance channels for implementing realtime features Final results from Phoenix channel benchmarks on 40core/128gb box. 2 million clients, limited by ulimit ‑ Tweet from @chris_mccord 20
  21. 21. Analogies nodejs Rails Phoenix minimal express Rack Plug ORM/ODM Mongoose ActiveRecord, Mongoid Ecto Template .ejs, .jade .erb .eex 21
  22. 22. Phoenix The Endpoint The Router Controllers Views Templates Channels PubSub http://www.phoenixframework.org/docs/overview 22
  23. 23. Plug defmodule HelloPhoenix.MessageController do   use HelloPhoenix.Web, :controller   plug :authenticate   plug :find_message   plug :authorize_message   def show(conn, params) do     render conn, :show, page: find_message(params["id"])   end   defp authenticate(conn), do: ...   defp find_message(id), do: ...   defp authorize_message(conn, _), do: ... end 23
  24. 24. Ecto supports PostgreSQL, MySQL, MSSQL, SQLite3 and MongoDB Repo Schema Query Changeset def index(conn, _params) do   users = Repo.all(User)   render(conn, "index.html", users: users) end 24
  25. 25. Take Away Rails Elixir syntax ( ) 25
  26. 26. Reference Site Phoenix Framework Brunch.io Course The Complete Elixir and Phoenix Bootcamp ‑ udemy Book Programming Phoenix 26
  27. 27. Q & A 27

×