Upgrade to Pro — share decks privately, control downloads, hide ads and more …

外傷的Elixir

 外傷的Elixir

#railsdm

さっちゃん

December 09, 2017
Tweet

More Decks by さっちゃん

Other Decks in Programming

Transcript

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

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

    _ l)ノ゙ http://c4se.hatenablog.com/entry/2017/12/05/232150
  3. defmodule Demo do def ok?(likes) do if likes do :ok

    else :died end end end IO.inspect Demo.ok? true
  4. 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
  5. 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)
  6. 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, …).
  7. ?

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

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

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

    y] IO.inspect x # => [1] IO.inspect y # => [2, 1]
  11. p y x.f # x has pointer to y. p

    y IO.inspect y y = f x, y IO.inspect y
  12. Immutable data has no hidden state (unlike OOP). Function has

    no implicit arguments because thereʼs no shared data.
  13. 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?
  14. 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)
  15. 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)
  16. {:ok, momonga} = Agent.start fn -> 0 end IO.inspect Agent.get_and_update(momonga,

    fn number -> number = number + 0 {number, number} end)
  17. 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)
  18. :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
  19. Registered process has shared state. ETS, DETS, Mnesia can read/write

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

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

    better but itʼs not a silver bullet.
  22. 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.