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

Metagrokking Elixir

Metagrokking Elixir

Webcamp Zagreb, 2017

Saša Jurić

October 04, 2017
Tweet

More Decks by Saša Jurić

Other Decks in Programming

Transcript

  1. def fun(pattern_1, pattern_A), do: # ... def fun(pattern_2, pattern_B), do:

    # ... def fun(pattern_3, pattern_C), do: # ... # ...
  2. map = %{} map1 = Map.put(map, some_key, some_value) map2 =

    Map.put(map1, another_key, another_value)
  3. map = %{} map = Map.put(map, some_key, some_value) map =

    Map.put(map, another_key, another_value)
  4. test.txt 4 6 4 44 65 7 8 34 6

    17 23 33 34 76 3 9 10
  5. defmodule Producer do ... defp produce_number() do :timer.sleep(:rand.uniform(1000)) number =

    :rand.uniform(11) - 6 IO.puts "produced #{number}" number end end
  6. defmodule Producer do ... defp loop(consumer_pid) do number = produce_number()

    send(consumer_pid, {:number, number}) loop(consumer_pid) end end
  7. defmodule Consumer do ... defp loop(sum) do new_sum = receive

    do {:number, number} -> consume(sum, number) end loop(new_sum) end end
  8. produced 3 sum = 3 produced -2 produced -3 sum

    = 1 produced 4 sum = -2 sum = 2
  9. defmodule Consumer do ... def handle_cast({:number, number}, sum) do new_sum

    = consume(sum, number) {:noreply, new_sum} end end
  10. defmodule Consumer do def start_link(), do: GenServer.start_link(Consumer, nil) def notify(consumer_pid,

    number), do: GenServer.cast( consumer_pid, {:number, number} ) ... end
  11. client list service process registry who is in charge of

    {:shopping_list_service, 123} ? register me as {:shopping_list_service, 123}
  12. entry added list_id 1 entry_id: 1 name: eggs quantity: 12

    entry updated list_id 2 entry_id: 1 quantity: 6 entry deleted list_id 3 entry_id: 1 ...
  13. GenServer server process (service) Supervisor starting/stopping/restarting registered name static service

    discovery Registry dynamic service discovery Application auto start
  14. It’s small, it’s elegant, it’s thoughtful, it’s fun, and it’s

    very, very, very readable once you flip over into functional programming mentality, which took my intern a week. Steve Cohen, Pinterest One week. That is how long it took for our engineers, who had all (but one) worked with Rails for a few years, to be productive on a new Phoenix client project. Brian Cardarella, Dockyard