Advertisement
Guest User

Untitled

a guest
Sep 25th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 4.18 KB | None | 0 0
  1. defmodule Anon.GeneralStorage do
  2.   use GenServer
  3.  
  4.   ### API ###
  5.  
  6.   def start_link(state \\ []),
  7.     do: GenServer.start_link(__MODULE__, state)
  8.  
  9.   def store(pid, data) when is_list(data),
  10.     do: GenServer.call(pid, {:store, data})
  11.  
  12.   def stored?(pid),
  13.     do: GenServer.call(pid, :is_stored)
  14.    
  15.   def get_all(pid),
  16.     do: GenServer.call(pid, :get_all)
  17.  
  18.  
  19.   ### Callbacks ###
  20.  
  21.   @impl true
  22.   def init(state) do
  23.     IO.puts("General storage started")
  24.     {:ok, state}
  25.   end
  26.  
  27.   @impl true
  28.   def handle_call({:store, data}, _from, _state) do
  29.     IO.puts("Data stored in General storage")
  30.     {:reply, :ok, data}
  31.   end
  32.  
  33.   def handle_call(:is_stored, _from, data) do
  34.     {:reply, data != [], data}
  35.   end
  36.  
  37.   @impl true
  38.   def handle_call(:get_all, _from, state) do
  39.     IO.puts("Selecting all data from General storage")
  40.     {:reply, state, state}
  41.   end
  42. end
  43.  
  44. defmodule Anon.AdultStorage do
  45.   use GenServer
  46.  
  47.   ### API ###
  48.  
  49.   def start_link(state \\ []),
  50.     do: GenServer.start_link(__MODULE__, state)
  51.  
  52.   def store(pid, data) when is_list(data),
  53.     do: GenServer.call(pid, {:store, data})
  54.  
  55.   def stored?(pid),
  56.     do: GenServer.call(pid, :is_stored)
  57.  
  58.   def get_all(pid),
  59.     do: GenServer.call(pid, :get_all)
  60.  
  61.   ### Callbacks ###
  62.  
  63.   @impl true
  64.   def init(state) do
  65.     IO.puts("Adult storage started")
  66.     {:ok, state}
  67.   end
  68.  
  69.   @impl true
  70.   def handle_call({:store, data}, _from, _state) do
  71.     IO.puts("Data stored in Adult storage")
  72.     adult_data = Enum.filter(data, & &1["age"] >= 18)
  73.     {:reply, :ok, adult_data}
  74.   end
  75.  
  76.   def handle_call(:is_stored, _from, data) do
  77.     {:reply, data != [], data}
  78.   end
  79.  
  80.   @impl true
  81.   def handle_call(:get_all, _from, state) do
  82.     IO.puts("Selecting all data from Adult storage")
  83.     {:reply, state, state}
  84.   end
  85. end
  86.  
  87. defmodule Anon.MinorStorage do
  88.   use GenServer
  89.  
  90.   ### API ###
  91.  
  92.   def start_link(state \\ []),
  93.     do: GenServer.start_link(__MODULE__, state)
  94.  
  95.   def store(pid, data) when is_list(data),
  96.     do: GenServer.call(pid, {:store, data})
  97.  
  98.   def stored?(pid),
  99.     do: GenServer.call(pid, :is_stored)
  100.  
  101.   def get_all(pid),
  102.     do: GenServer.call(pid, :get_all)
  103.  
  104.   ### Callbacks ###
  105.  
  106.   @impl true
  107.   def init(state) do
  108.     IO.puts("Minor storage started")
  109.     {:ok, state}
  110.   end
  111.  
  112.   @impl true
  113.   def handle_call({:store, data}, _from, _state) do
  114.     IO.puts("Data stored in Minor storage")
  115.     adult_data = Enum.filter(data, & &1["age"] < 18)
  116.     {:reply, :ok, adult_data}
  117.   end
  118.  
  119.   def handle_call(:is_stored, _from, data) do
  120.     {:reply, data != [], data}
  121.   end
  122.  
  123.   @impl true
  124.   def handle_call(:get_all, _from, state) do
  125.     IO.puts("Selecting all data from Minor storage")
  126.     {:reply, state, state}
  127.   end
  128. end
  129.  
  130. defmodule Anon.AgeSort do
  131.   alias Anon.{GeneralStorage, AdultStorage, MinorStorage}
  132.  
  133.   def call(data) when is_list(data) do
  134.     {:ok, general_pid} = GeneralStorage.start_link()
  135.     {:ok, adult_pid} = AdultStorage.start_link()
  136.     {:ok, minor_pid} = MinorStorage.start_link()
  137.    
  138.     result =
  139.       with :ok <- GeneralStorage.store(general_pid, data),
  140.            true <- GeneralStorage.stored?(general_pid),
  141.            general_data <- GeneralStorage.get_all(general_pid),
  142.            :ok <- AdultStorage.store(adult_pid, general_data),
  143.            true <- AdultStorage.stored?(adult_pid),
  144.            :ok <- MinorStorage.store(minor_pid, general_data),
  145.            true <- MinorStorage.stored?(minor_pid) do
  146.      
  147.         adults = AdultStorage.get_all(adult_pid)
  148.         minors = MinorStorage.get_all(minor_pid)
  149.      
  150.         %{
  151.           "adults" => Enum.map(adults, & &1["id"]),
  152.           "minors" => Enum.map(minors, & &1["id"])
  153.         }
  154.     else
  155.       _error -> :error
  156.     end
  157.    
  158.     stop_storages([general_pid, adult_pid, minor_pid])
  159.     result
  160.   end
  161.  
  162.   defp stop_storages(storages) when is_list(storages) do
  163.     Enum.each(storages, &GenServer.stop/1)
  164.   end
  165. end
  166.  
  167. # Have no time for implementing JSON parser
  168. data = [
  169.   %{"id" => 1, "age" => 10},
  170.   %{"id" => 2, "age" => 18},
  171.   %{"id" => 3, "age" => 25},
  172.   %{"id" => 4, "age" => 5}
  173. ]
  174.  
  175. data
  176. |> Anon.AgeSort.call()
  177. |> IO.inspect(label: "Result")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement