Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Anon.GeneralStorage do
- use GenServer
- ### API ###
- def start_link(state \\ []),
- do: GenServer.start_link(__MODULE__, state)
- def store(pid, data) when is_list(data),
- do: GenServer.call(pid, {:store, data})
- def stored?(pid),
- do: GenServer.call(pid, :is_stored)
- def get_all(pid),
- do: GenServer.call(pid, :get_all)
- ### Callbacks ###
- @impl true
- def init(state) do
- IO.puts("General storage started")
- {:ok, state}
- end
- @impl true
- def handle_call({:store, data}, _from, _state) do
- IO.puts("Data stored in General storage")
- {:reply, :ok, data}
- end
- def handle_call(:is_stored, _from, data) do
- {:reply, data != [], data}
- end
- @impl true
- def handle_call(:get_all, _from, state) do
- IO.puts("Selecting all data from General storage")
- {:reply, state, state}
- end
- end
- defmodule Anon.AdultStorage do
- use GenServer
- ### API ###
- def start_link(state \\ []),
- do: GenServer.start_link(__MODULE__, state)
- def store(pid, data) when is_list(data),
- do: GenServer.call(pid, {:store, data})
- def stored?(pid),
- do: GenServer.call(pid, :is_stored)
- def get_all(pid),
- do: GenServer.call(pid, :get_all)
- ### Callbacks ###
- @impl true
- def init(state) do
- IO.puts("Adult storage started")
- {:ok, state}
- end
- @impl true
- def handle_call({:store, data}, _from, _state) do
- IO.puts("Data stored in Adult storage")
- adult_data = Enum.filter(data, & &1["age"] >= 18)
- {:reply, :ok, adult_data}
- end
- def handle_call(:is_stored, _from, data) do
- {:reply, data != [], data}
- end
- @impl true
- def handle_call(:get_all, _from, state) do
- IO.puts("Selecting all data from Adult storage")
- {:reply, state, state}
- end
- end
- defmodule Anon.MinorStorage do
- use GenServer
- ### API ###
- def start_link(state \\ []),
- do: GenServer.start_link(__MODULE__, state)
- def store(pid, data) when is_list(data),
- do: GenServer.call(pid, {:store, data})
- def stored?(pid),
- do: GenServer.call(pid, :is_stored)
- def get_all(pid),
- do: GenServer.call(pid, :get_all)
- ### Callbacks ###
- @impl true
- def init(state) do
- IO.puts("Minor storage started")
- {:ok, state}
- end
- @impl true
- def handle_call({:store, data}, _from, _state) do
- IO.puts("Data stored in Minor storage")
- adult_data = Enum.filter(data, & &1["age"] < 18)
- {:reply, :ok, adult_data}
- end
- def handle_call(:is_stored, _from, data) do
- {:reply, data != [], data}
- end
- @impl true
- def handle_call(:get_all, _from, state) do
- IO.puts("Selecting all data from Minor storage")
- {:reply, state, state}
- end
- end
- defmodule Anon.AgeSort do
- alias Anon.{GeneralStorage, AdultStorage, MinorStorage}
- def call(data) when is_list(data) do
- {:ok, general_pid} = GeneralStorage.start_link()
- {:ok, adult_pid} = AdultStorage.start_link()
- {:ok, minor_pid} = MinorStorage.start_link()
- result =
- with :ok <- GeneralStorage.store(general_pid, data),
- true <- GeneralStorage.stored?(general_pid),
- general_data <- GeneralStorage.get_all(general_pid),
- :ok <- AdultStorage.store(adult_pid, general_data),
- true <- AdultStorage.stored?(adult_pid),
- :ok <- MinorStorage.store(minor_pid, general_data),
- true <- MinorStorage.stored?(minor_pid) do
- adults = AdultStorage.get_all(adult_pid)
- minors = MinorStorage.get_all(minor_pid)
- %{
- "adults" => Enum.map(adults, & &1["id"]),
- "minors" => Enum.map(minors, & &1["id"])
- }
- else
- _error -> :error
- end
- stop_storages([general_pid, adult_pid, minor_pid])
- result
- end
- defp stop_storages(storages) when is_list(storages) do
- Enum.each(storages, &GenServer.stop/1)
- end
- end
- # Have no time for implementing JSON parser
- data = [
- %{"id" => 1, "age" => 10},
- %{"id" => 2, "age" => 18},
- %{"id" => 3, "age" => 25},
- %{"id" => 4, "age" => 5}
- ]
- data
- |> Anon.AgeSort.call()
- |> IO.inspect(label: "Result")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement