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

Про Elixir. Станислав Мехоношин

Про Elixir. Станислав Мехоношин

Deep Refactoring

May 02, 2017
Tweet

More Decks by Deep Refactoring

Other Decks in Programming

Transcript

  1. Functions && Modules defmodule ExBees.Map.SegmentsSupervisor do use Supervisor def start_link(name)

    do Supervisor.start_link(__MODULE__, :ok, name: name) End def init(:ok) do children = [ supervisor(ExBees.Map.Segment, [ExBees.Map.Segment]) ] supervise(children, strategy: :one_for_one) end end
  2. Pattern Matching iex> {a, b, c} = {:hello, "world", 42}

    {:hello, "world", 42} iex> a :hello iex> b "world"
  3. Pattern Matching 2 iex> case {1, 2, 3} do ...>

    {4, 5, 6} -> ...> "This clause won't match" ...> {1, x, 3} -> ...> "This clause will match and bind x to 2 in this clause" ...> _ -> ...> "This clause would match any value" ...> end "This clause will match and bind x to 2 in this clause"
  4. Actors 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
  5. OTP

  6. VS

  7. Синтаксис -module(hello_module). -export([some_fun/0, some_fun/1]). % A "Hello world" function some_fun()

    -> io:format('~s~n', ['Hello world!']). % This one works only with lists some_fun(List) when is_list(List) -> io:format('~s~n', List). % Non-exported functions are private priv() -> secret_info. defmodule HelloModule do # A "Hello world" function def some_fun do IO.puts "Hello world!" end # This one works only with lists def some_fun(list) when is_list(list) do IO.inspect list end # A private function defp priv do :secret_info end end
  8. Мутабельность переменных ~ erl Eshell V8.2 (abort with ^G) 1>

    A = 1. 1 2> A = 2. ** exception error: no match of right hand side value 2 3> io:format("~p", [A]). 1ok ~ iex Interactive Elixir (1.4.1) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> a = 1 1 iex(2)> a = 2 2 iex(3)> IO.puts a 2 :ok
  9. "Разбудите меня лет через сто, и спросите, что сейчас делается

    на Гитхабе. И я отвечу — переписывают блоги с Ruby на Elixir." Михаил Салтыков- Щедрин 1826-1889