lab03
This commit is contained in:
8 files changed
+238
No files matched your search
@@ -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
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
||||||
@@ -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/
|
||||||
@@ -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 <https://hexdocs.pm/card>.
|
||||||
|
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
defmodule CardTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
doctest Card
|
||||||
|
|
||||||
|
test "greets the world" do
|
||||||
|
assert Card.hello() == :world
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ExUnit.start()
|
||||||
Reference in new issue
Block a user