diff --git a/labs/03/arithmetic.ex b/labs/03/arithmetic.ex new file mode 100644 index 0000000..70ea9e5 --- /dev/null +++ b/labs/03/arithmetic.ex @@ -0,0 +1,77 @@ +defmodule Arithmetic.Worker do + use GenServer + + def start() do + GenServer.start(__MODULE__, nil) + end + + ### Server Impl ### + @impl true + def init(_arg) do + {:ok, nil} + end + + @impl true + def handle_cast({:square, x, from}, state) do + GenServer.reply(from, {self(), x * x}) + {:noreply, state} + end + + @impl true + def handle_cast({:sqrt, x, from}, state) do + Process.sleep(2000) + GenServer.reply(from, {self(), if(x < 0, do: :error, else: :math.sqrt(x))}) + {:noreply, state} + end +end + +defmodule Arithmetic.Server do + use GenServer + + ### Client API ### + def start(n) do + GenServer.start(__MODULE__, n, name: __MODULE__) + end + + def square(x) do + GenServer.call(__MODULE__, {:square, x}) + end + + def sqrt(x) do + GenServer.call(__MODULE__, {:sqrt, x}) + end + + ### Server Impl ### + @impl true + def init(n) do + workers = + Enum.map(1..n, fn _ -> + {:ok, pid} = Arithmetic.Worker.start() + IO.inspect(pid) + pid + end) + + {:ok, {workers, 0}} + end + + @impl true + def handle_call({:square, x}, from, {workers, index}) do + worker = Enum.at(workers, index) + + GenServer.cast(worker, {:square, x, from}) + + next = rem(index + 1, length(workers)) + # dont reply here, worker will reply + {:noreply, {workers, next}} + end + + @impl true + def handle_call({:sqrt, x}, from, {workers, index}) do + worker = Enum.at(workers, index) + + GenServer.cast(worker, {:sqrt, x, from}) + + next = rem(index + 1, length(workers)) + {:noreply, {workers, next}} + end +end diff --git a/labs/03/card/.formatter.exs b/labs/03/card/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/labs/03/card/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/labs/03/card/.gitignore b/labs/03/card/.gitignore new file mode 100644 index 0000000..ff119b6 --- /dev/null +++ b/labs/03/card/.gitignore @@ -0,0 +1,26 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +card-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/labs/03/card/README.md b/labs/03/card/README.md new file mode 100644 index 0000000..4f4836e --- /dev/null +++ b/labs/03/card/README.md @@ -0,0 +1,21 @@ +# Card + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `card` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:card, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at . + diff --git a/labs/03/card/lib/card/worker.ex b/labs/03/card/lib/card/worker.ex new file mode 100644 index 0000000..9220916 --- /dev/null +++ b/labs/03/card/lib/card/worker.ex @@ -0,0 +1,73 @@ +defmodule Card.Worker do + use GenServer + + ### Client API ### + def start() do + GenServer.start(__MODULE__, fresh(), name: __MODULE__) + end + + def new() do + GenServer.cast(__MODULE__, :new) + end + + def shuffle() do + GenServer.cast(__MODULE__, :shuffle) + end + + def count() do + GenServer.call(__MODULE__, :count) + end + + def deal(n \\ 1) do + GenServer.call(__MODULE__, {:deal, n}) + end + + ### Server Impl ### + defp fresh() do + values = Enum.map(2..10, &Integer.to_string/1) ++ ["J", "Q", "K", "A"] + suits = ["\u2663", "\u2666", "\u2665", "\u2660"] + + for v <- values, f <- suits, do: v <> f + end + + @impl true + def init(arg) do + {:ok, arg} + end + + @impl true + def handle_cast(:new, _state) do + # {:noreply, new_state} + {:noreply, fresh()} + end + + @impl true + def handle_cast(:shuffle, state) do + # {:noreply, new_state} + {:noreply, Enum.shuffle(state)} + end + + @impl true + def handle_call(:count, _from, state) do + # {:reply, actual_reply, new_state} + {:reply, length(state), state} + end + + @impl true + def handle_call({:deal, n}, _from, state) do + case n do + _ when n < 0 -> + # {:reply, actual_reply, new_state} + {:reply, {:error, "Cannot deal negative number of cards"}, state} + + _ when n > length(state) -> + # {:reply, actual_reply, new_state} + {:reply, {:error, "Insufficient cards in deck to deal"}, state} + + _ -> + {d, r} = Enum.split(state, n) + # {:reply, actual_reply, new_state} + {:reply, {:ok, d}, r} + end + end +end diff --git a/labs/03/card/mix.exs b/labs/03/card/mix.exs new file mode 100644 index 0000000..4c31418 --- /dev/null +++ b/labs/03/card/mix.exs @@ -0,0 +1,28 @@ +defmodule Card.MixProject do + use Mix.Project + + def project do + [ + app: :card, + version: "0.1.0", + elixir: "~> 1.17", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/labs/03/card/test/card_test.exs b/labs/03/card/test/card_test.exs new file mode 100644 index 0000000..a876bbf --- /dev/null +++ b/labs/03/card/test/card_test.exs @@ -0,0 +1,8 @@ +defmodule CardTest do + use ExUnit.Case + doctest Card + + test "greets the world" do + assert Card.hello() == :world + end +end diff --git a/labs/03/card/test/test_helper.exs b/labs/03/card/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/labs/03/card/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()