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.

Phoenix demysitify, with fun

191 views

Published on

The talk of Elixir language and Phoenix framework at Rubyconf Taiwan 2016

Published in: Technology
  • Be the first to comment

Phoenix demysitify, with fun

  1. 1. Phoenix demysitify, with fun
  2. 2. Who am I @taiansu |> |> Ruby |> React.js, Redux, Rx.js |> RailsGirls.tw & Elixir.tw http://taian.su facebook.com/sutaian
  3. 3. Elixir Taiwan » Facebook: Elixir Taiwan » Slack: elixirtw.slack.com » KKTIX: elixirtw.kktix.cc
  4. 4. Phoenix?
  5. 5. Web MVC Elixir
  6. 6. , web ... Elixir
  7. 7. Elixir / Phoenix
  8. 8. Dave Thomas droped by Taiwan at 2014
  9. 9. Elixir =~ Ruby syntax + Erlang speciality and some more
  10. 10. Erlang from 1986 open-sourced in 1998
  11. 11. Build for Telecom switch system at beginning » Functional » Soft real time » Concurrent » 99.999% up-time, including upgrade and hot-fix » Hot update
  12. 12. On the fly update, literally*. Erlang AR drone in-flight upgrade
  13. 13. Actor model
  14. 14. Compare with system process » C# process: 300µs per process 50µs per message » Erlang VM process: 1µs up to 2,500 processes 3µs up to 30,000 processes 0.8µs per message
  15. 15. We do not have ONE web server handling 2 millions sessions. We have 2 million web servers handling one session each. » Joe Armstrong
  16. 16. process in Erlang/Elixir =~ object instance in OO application
  17. 17. with :observer.start
  18. 18. github.com/taiansu/game_of_life_elixir
  19. 19. Weakness » Hard real time » Processing big chunk of data (Video, Audio) » Uncommon syntax » String
  20. 20. Syntax -module(mym). -export([max/2]). max(X, Y) -> if X > Y -> X; X < Y -> Y; true -> nil end.
  21. 21. Erlang string: a list of integer [1, 2, 3] % [1, 2, 3] [119, 97, 116, 63, 33] % "wat?!" <<119, 97, 116, 63, 33>> % Bit-syntax from R16
  22. 22. wat.
  23. 23. Elixir by José Valim at 2013 Rails core team member
  24. 24. Leverage the Erlang VM
  25. 25. Pattern matching What's the "=" means, seriously?
  26. 26. Pattern matching basic # Ruby x, x, y = [1, 2, 3] # => x = 2, y = 3 # Elixir [x, x, y] = [1, 2, 3] # => (MatchError) no match of right hand side value [x, x, y] = [1, 1, 3] # => x = 1, y = 3 [head | tail] = [1, 2, 3, 4] # => head = 1, tail = [2, 3, 4] {:ok, contents} = File.read("my_app.config")
  27. 27. Pattern matching: recursive defmodule Factorial do def calc(0), do: 1 def calc(val), do: val * calc(val - 1) end Factorial.calc(100)
  28. 28. Pattern matching with function, contd. parse_response = fn {:err, msg} -> show_error(msg) {:ok, article = %{title: title, content: content}} -> # article = %{title: "abc", content: "123"} # title = "abc" # content = "123" render(title, content) _ -> IO.puts "Huston, we have a problem." end parse_response.(response)
  29. 29. OTP compliant GenServer, Supervisor, GenEvent, Application
  30. 30. Elixir only
  31. 31. Case: useless variable request = generate_request response = get_response(request) body = parse_body(response, :html) html = render(body) # otherwise html = render(parse_body(get_response(generate_request), :html))
  32. 32. Pipe operator: |> html = generate_request |> get_response |> parse_body(:html) |> render
  33. 33. Meta-programming macro, quote and unquote, to AST live demo ( if enough time ) quote do: 1 + 2 * 3
  34. 34. github.com/taiansu/pipe_to 2 |> Enum.drop(1..10, _) |> Enum.at(1)
  35. 35. Code with 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
  36. 36. doc
  37. 37. test
  38. 38. Trinity
  39. 39. Phoenix by Chris McCord
  40. 40. Disclaim I ❤ Ruby, still.
  41. 41. Phoenix is Rails with less magic
  42. 42. Demysitify: Both are MVC framework with » Routes » Controller » Model » View » assets
  43. 43. Core idea of phoenix: Your entire web application is a function
  44. 44. url is your params, and return value is that html path = "/order/search?start_date=2017-12-02&end_date=2017-12-03" html = path |> router |> controller |> model |> controller |> view
  45. 45. The conn variable pipe through the whole system
  46. 46. Routes & Controller Basically the same
  47. 47. Model and ORM SQL wrapper named Ecto
  48. 48. Ecto defmodule MyApp.Order schema "orders" do belongs_to :user, MyApp.User has_many :order_items, MyApp.OrderItem field :code, :string field :payment_method, :string end # query functions here end
  49. 49. Ecto query functions def query_cart_of_user(query __MODULE__, user_id) do from o in query, where: o.user_id == ^user_id end def order_by_created_at(query __MODULE__) do from o in query, order_by: o.created_at end ## in Controller orders = %Order{} |> query_cart_of_user(20) |> order_by_created_at |> Repo.all
  50. 50. View and template
  51. 51. File: template/order/show.html.eex <h2> Hi! this is the show page!<%= order_name(@conn, @order) %> </h2>
  52. 52. File: /view/order_view.ex defmodule MyApp.OrderView do use MyApp.Web, :view def render("show.html", params []) do ~E{ <h2>Hi! this is the show page!<%= order_name(@conn, @order) %></h2> } end def order_name(conn, order) do user = conn.assigns[:current_user] "#{user.name}: #{order.name} x #{order.quantity}" end end
  53. 53. Channel just like ActionCable
  54. 54. Conclusion
  55. 55. What I've learned » Different paradiam expand your horizon » Which is a knowledge you can bring with you anywhere » Composability over REUSE » Think in process way » And...
  56. 56. Code with Fun Just like Ruby tought us, thanks Matz.
  57. 57. Code with Functional
  58. 58. Thanks for your time! Any Questions? more plans on going, join Elixir.tw!

×