外傷的Elixir

 外傷的Elixir

#railsdm

4b071f90c5d9c0a58e2d9076460b7be4?s=128

さっちゃん

December 09, 2017
Tweet

Transcript

  1. 外傷的Elixir

  2. 外傷的Elixir

  3. .。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

  4. .。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆) 仕事で使ってゐる⾔語 : Ruby, Elixir, Crystal Privateで使ってゐる⾔語 :

    Clojure (Android, SuperCollider, Processing), Haskell (Everything except JVM), ユーラル語 (yUraru yUsin) ⼀番仕事で使ひたい⾔語 : J, Lojban
  5. .。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆) 7 libraries I made by Elixir ヾ(〃l

    _ l)ノ゙ http://c4se.hatenablog.com/entry/2017/12/05/232150
  6. Elixir is _?_

  7. defmodule Demo do def ok?(likes) do if likes do :ok

    else :died end end end IO.inspect Demo.ok? true
  8. Elixir looks like Ruby.

  9. for i <- 1..4, do: IO.puts :erlang.term_to_binary i

  10. Elixir & Erlang are very close.

  11. Erlang -module(momonga). -behaviour(gen_server). -export([start_link/0, init/1, handle_call/3]). start_link() -> gen_server:start_link(momonga, 0,

    []). init(Number) -> {ok, Number}. handle_call(bear, _From, Number) -> Number2 = Number + 0, {reply, Number2, Number2}. Elixir defmodule Momonga do use GenServer def start_link, do: GenServer.start_link __MODULE__, 0 def init(number), do: {:ok, number} def handle_call(:bear, _from, number) do number = number + 0 {:reply, number, number} end end
  12. Elixir looks familiar than Erlang.

  13. 1..4 |> Enum.map(fn i when rem(i, 2) == 0 ->

    true _ -> false end) |> Enum.map(&Task.async fn -> if &1, do: Process.sleep(4000), else: IO.puts("No wait") end) |> Enum.each(&Task.await/1)
  14. Elixir has some nice language feature. * Strong pattern match

    that originated from Prologʼs unification. * Chain functions |> fluently |> by “pipe operator”. * Easy parallel computing (Task, GenStage, …).
  15. Elixir has nice & fast Rails like WAF called “Phoenix”.

  16. We are Rubyist. So we can be Alchemist!

  17. ?

  18. Elixir is _?_

  19. Elixir was Clojure

  20. Elixir was Clojure on Erlang VM

  21. Elixir was Clojure on Erlang VM that looks like Ruby.

  22. Elixirʼs syntax & standard library are like Ruby. Elixir is

    a functional programming language with immutable data. Elixir has supervisor architecture same as Erlang.
  23. Elixirʼs syntax & standard library are like Ruby. Elixir is

    a functional programming language with immutable data. Elixir has supervisor architecture same as Erlang.
  24. x = [1] y = x y = [2 |

    y] IO.inspect x # => [1] IO.inspect y # => [2, 1]
  25. Variables can be reassigned. Data is immutable. Immutable data makes

    app thread safe.
  26. result = state.f value {result, state} = f value, state

  27. p y x.f # x has pointer to y. p

    y IO.inspect y y = f x, y IO.inspect y
  28. # x1, …, x10000 have pointers to large_data. f x1

    f x1, large_data
  29. Immutable data has no hidden state (unlike OOP). Function has

    no implicit arguments because thereʼs no shared data.
  30. The best way is to learn functional programming. (I recommend

    Clojure & Haskell.) But most apps should have hidden state & shared state. How can I do?
  31. defmodule Momonga do def start, do: loop 0 defp loop(number)

    do receive do {:bear, from} -> number = number + 0 send from, {self(), number} loop number _ -> loop number end end end momonga = spawn Momonga, :start, [] send momonga, {:bear, self()} IO.inspect receive do: ({^momonga, number} -> number)
  32. defmodule Momonga do use GenServer def start, do: GenServer.start __MODULE__,

    0 def init(number), do: {:ok, number} def handle_call(:bear, _from, number) do number = number + 0 {:reply, number, number} end end {:ok, momonga} = Momonga.start IO.inspect GenServer.call(momonga, :bear)
  33. {:ok, momonga} = Agent.start fn -> 0 end IO.inspect Agent.get_and_update(momonga,

    fn number -> number = number + 0 {number, number} end)
  34. Process has a hidden state. Agent & GenServer is useful.

  35. defmodule Momonga do use GenServer def start, do: GenServer.start __MODULE__,

    0, name: __MODULE__ def init(number), do: {:ok, number} def handle_call(:bear, _from, number) do number = number + 0 {:reply, number, number} end end Momonga.start IO.inspect GenServer.call(Momonga, :bear)
  36. :ets.new :momonga, [:public, :named_table] :ets.insert :momonga, {:number, 0} :ets.safe_fixtable :momonga,

    true number = :ets.lookup(:momonga, :number)[:number] + 0 :ets.insert :momonga, {:number, number} :ets.safe_fixtable :momonga, false IO.inspect number
  37. Registered process has shared state. ETS, DETS, Mnesia can read/write

    parallel. We can use external DB of course.
  38. Elixirʼs syntax & standard library are like Ruby. Elixir is

    a functional programming language with immutable data. Elixir has supervisor architecture same as Erlang.
  39. None
  40. Your app should be parallel & fault tolerant. Elixir is

    better but itʼs not a silver bullet.
  41. Elixir has some mechanism. * Monitor * Link * Supervisor

    * Registry Elixir has some good restriction. * Immutable data * Sending messages canʼt fail * No shared state (Thereʼs some exception) * Process doesn't block other processes (Thereʼs some exception) But testing parallel fault is too-oo-oo hard for human.
  42. Elixir is better but itʼs not a silver bullet. We

    are searching the best…