community_garden

This commit is contained in:
Danil Negrienko 2024-03-05 17:11:42 -05:00
parent e2e3430ef8
commit af3b5c40f8
11 changed files with 461 additions and 0 deletions

View File

@ -0,0 +1,25 @@
{
"authors": [
"neenjaw"
],
"contributors": [
"angelikatyborska",
"jrr",
"efx",
"justin-m-morgan",
"cr0t"
],
"files": {
"solution": [
"lib/community_garden.ex"
],
"test": [
"test/community_garden_test.exs"
],
"exemplar": [
".meta/exemplar.ex"
]
},
"language_versions": ">=1.10",
"blurb": "Learn about the Agent module by helping your local community handle community garden registrations."
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"community-garden","id":"5138f62059e2481ca67da71571d11fb7","url":"https://exercism.org/tracks/elixir/exercises/community-garden","handle":"negrienko","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

24
elixir/community-garden/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# 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").
agents-*.tar

View File

@ -0,0 +1,75 @@
# Help
## Running the tests
From the terminal, change to the base directory of the exercise then execute the tests with:
```bash
$ mix test
```
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
Documentation:
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
## Pending tests
In test suites of practice exercises, all but the first test have been tagged to be skipped.
Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.
For example:
```elixir
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
```
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
```bash
$ mix test --include pending
```
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`.
```elixir
# ExUnit.configure(exclude: :pending, trace: true)
```
## Useful `mix test` options
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
* `--failed` - runs only tests that failed the last time they ran
* `--max-failures` - the suite stops evaluating tests when this number of test failures
is reached
* `--seed 0` - disables randomization so the tests in a single file will always be ran
in the same order they were defined in
## Submitting your solution
You can submit your solution using the `exercism submit lib/community_garden.ex` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir)
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found.

View File

@ -0,0 +1,36 @@
# Hints
## General
- Read about the [`Agent` module][getting-started-agent] in the Getting Started guide.
- Read the documentation about the [`Agent` module][elixir-doc-agent].
- Watch [ElixirCasts: Introduction to Agents][elixircasts-agent].
## 1. Open the garden
- Review the [`Agent`][elixir-doc-agent] documentation.
- The function to initialize the state of the _agent process_ must return the initial state.
## 2. List the registrations
- The [`Agent`][elixir-doc-agent] module contains many functions to obtain the current state of the _agent process_.
## 3. Register plots to a person
- The [`Agent`][elixir-doc-agent] module contains functions to obtain and update the state of the _agent process_.
- The functions generally require a function which transforms the state and returns a specific form.
- In order to keep track of the id for the next plot to assign, your _agent process_'s state needs to keep track of the plots and also the next id to use for a plot.
## 4. Release plots
- The [`Agent`][elixir-doc-agent] module contains functions to obtain and update the state of the _agent process_.
- The functions generally require a function which transforms the state and returns the next state.
## 5. Get a registered plot
- The [`Agent`][elixir-doc-agent] module contains functions to obtain the state of the _agent process_.
- Obtain the plot from, then handle the result to return the correct result.
[elixircasts-agent]: https://elixircasts.io/intro-to-agents
[elixir-doc-agent]: https://hexdocs.pm/elixir/Agent.html
[getting-started-agent]: https://hexdocs.pm/elixir/agents.html

View File

@ -0,0 +1,97 @@
# Community Garden
Welcome to Community Garden on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
## Agent
The `Agent` module facilitates an abstraction for spawning [processes and the _receive-send_ loop][exercism-processes]. From here, we will call processes started using the `Agent` module _'agent processes'_. An _agent process_ might be chosen to represent a central shared state.
To start an _agent process_, `Agent` provides the `start/2` function. The supplied function when `start`_-ing_ the _agent process_ returns the initial state of the process:
```elixir
# Start an agent process with an initial value of an empty list
{:ok, agent_pid} = Agent.start(fn -> [] end)
```
Just like `Map` or `List`, `Agent` provides many functions for working with _agent processes_.
It is customary to organize and encapsulate all `Agent`-related functionality into a module for the domain being modeled.
[exercism-processes]: https://exercism.org/tracks/elixir/concepts/processes
## Instructions
Your community association has asked you to implement a simple registry application to manage the community garden registrations. The `Plot` struct has already been provided for you.
## 1. Open the garden
Implement the `CommunityGarden.start/1` function, it should receive an optional keyword list of options to pass forward to the _agent process_. The garden's initial state should be initialized to represent an empty collection of plots. It should return an `:ok` tuple with the garden's pid.
```elixir
{:ok, pid} = CommunityGarden.start()
# => {:ok, #PID<0.112.0>}
```
## 2. List the registrations
Implement the `CommunityGarden.list_registrations/1` function. It should receive the `pid` for the community garden. It should return a list of the stored plots that are registered.
```elixir
CommunityGarden.list_registrations(pid)
# => []
```
> At this point, we haven't added the ability to register a plot, so this list should be empty
## 3. Register plots to a person
Implement the `CommunityGarden.register/2` function. It should receive the `pid` for the community garden and a name to register the plot. It should return the `Plot` struct with the plot's id and person registered to when it is successful.
The ids should be incremental and unique. You can keep an id counter in the agent's state.
```elixir
CommunityGarden.register(pid, "Emma Balan")
# => %Plot{plot_id: 1, registered_to: "Emma Balan"}
CommunityGarden.list_registrations(pid)
# => [%Plot{plot_id: 1, registered_to: "Emma Balan"}]
```
## 4. Release plots
Implement the `CommunityGarden.release/2` function. It should receive the `pid` and `id` of the plot to be released. It should return `:ok` on success. When a plot is released, the id is not re-used, it is used as a unique identifier only.
```elixir
CommunityGarden.release(pid, 1)
# => :ok
CommunityGarden.list_registrations(pid)
# => []
```
## 5. Get a registered plot
Implement the `CommunityGarden.get_registration/2` function. It should receive the `pid` and `id` of the plot to be checked. It should return the plot if it is registered, and `:not_found` if it is unregistered.
```elixir
CommunityGarden.get_registration(pid, 1)
# => %Plot{plot_id: 1, registered_to: "Emma Balan"}
CommunityGarden.get_registration(pid, 7)
# => {:not_found, "plot is unregistered"}
```
## Source
### Created by
- @neenjaw
### Contributed to by
- @angelikatyborska
- @jrr
- @efx
- @justin-m-morgan
- @cr0t

View File

@ -0,0 +1,38 @@
# Use the Plot struct as it is provided
defmodule Plot do
@enforce_keys [:plot_id, :registered_to]
defstruct [:plot_id, :registered_to]
end
defmodule CommunityGarden do
def start(),
do: Agent.start(fn -> %{index: 0, plots: []} end)
def list_registrations(pid),
do: Agent.get(pid, fn %{plots: plots} -> plots end)
def register(pid, register_to),
do:
Agent.get_and_update(pid, fn %{plots: plots, index: counter} ->
index = counter + 1
plot = %Plot{plot_id: index, registered_to: register_to}
{plot, %{plots: [plot | plots], index: index}}
end)
def release(pid, plot_id),
do:
Agent.cast(pid, fn %{plots: plots} = state ->
%{state | plots: Enum.reject(plots, fn element -> compare(element, plot_id) end)}
end)
def get_registration(pid, plot_id) do
list_registrations(pid)
|> Enum.find(
{:not_found, "plot is unregistered"},
fn element -> compare(element, plot_id) end
)
end
defp compare(%Plot{plot_id: element_id}, plot_id) when element_id == plot_id, do: true
defp compare(_element, _plot_id), do: false
end

View File

@ -0,0 +1,28 @@
defmodule CommunityGarden.MixProject do
use Mix.Project
def project do
[
app: :community_garden,
version: "0.1.0",
# elixir: "~> 1.10",
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

View File

@ -0,0 +1,131 @@
defmodule CommunityGardenTest do
use ExUnit.Case
@tag task_id: 1
test "start returns an alive pid" do
assert {:ok, pid} = CommunityGarden.start()
assert Process.alive?(pid)
end
@tag task_id: 2
test "when started, the registry is empty" do
assert {:ok, pid} = CommunityGarden.start()
assert [] == CommunityGarden.list_registrations(pid)
end
@tag task_id: 3
test "can register a new plot" do
assert {:ok, pid} = CommunityGarden.start()
assert %Plot{} = CommunityGarden.register(pid, "Johnny Appleseed")
end
@tag task_id: 3
test "maintains a registry of plots" do
assert {:ok, pid} = CommunityGarden.start()
assert %Plot{} = plot = CommunityGarden.register(pid, "Johnny Appleseed")
assert [plot] == CommunityGarden.list_registrations(pid)
end
@tag task_id: 3
test "the first plot has an id of 1" do
assert {:ok, pid} = CommunityGarden.start()
plot = CommunityGarden.register(pid, "Johnny Appleseed")
assert plot.plot_id == 1
end
@tag task_id: 3
test "registered plots have incremental unique id" do
assert {:ok, pid} = CommunityGarden.start()
plot_1 = CommunityGarden.register(pid, "Johnny Appleseed")
plot_2 = CommunityGarden.register(pid, "Frederick Law Olmsted")
plot_3 = CommunityGarden.register(pid, "Lancelot (Capability) Brown")
assert plot_1.plot_id == 1
assert plot_2.plot_id == 2
assert plot_3.plot_id == 3
end
@tag task_id: 3
test "registered plots have incremental unique id when registered concurrently" do
{:ok, pid} = CommunityGarden.start()
total_plots = 20
test_process_pid = self()
Enum.each(1..total_plots, fn n ->
spawn(fn ->
plot = CommunityGarden.register(pid, "Mary Bumblebee #{n}")
send(test_process_pid, {n, plot})
end)
end)
plot_ids =
Enum.map(1..total_plots, fn n ->
receive do
{^n, plot} -> plot.plot_id
after
100 -> nil
end
end)
assert Enum.sort(plot_ids) == Enum.to_list(1..total_plots)
end
@tag task_id: 4
test "can release a plot" do
assert {:ok, pid} = CommunityGarden.start()
assert %Plot{} = plot = CommunityGarden.register(pid, "Johnny Appleseed")
assert :ok = CommunityGarden.release(pid, plot.plot_id)
assert [] == CommunityGarden.list_registrations(pid)
end
@tag task_id: 4
test "do not re-use id of released plots" do
assert {:ok, pid} = CommunityGarden.start()
plot_1 = CommunityGarden.register(pid, "Keanu Reeves")
plot_2 = CommunityGarden.register(pid, "Thomas A. Anderson")
assert plot_1.plot_id == 1
assert plot_2.plot_id == 2
CommunityGarden.release(pid, plot_1.plot_id)
CommunityGarden.release(pid, plot_2.plot_id)
plot_3 = CommunityGarden.register(pid, "John Doe")
plot_4 = CommunityGarden.register(pid, "Jane Doe")
assert plot_3.plot_id == 3
assert plot_4.plot_id == 4
end
@tag task_id: 4
test "release only one plot at time" do
assert {:ok, pid} = CommunityGarden.start()
plot_1 = CommunityGarden.register(pid, "Keanu Reeves")
plot_2 = CommunityGarden.register(pid, "Thomas A. Anderson")
assert plot_1.plot_id == 1
assert plot_2.plot_id == 2
CommunityGarden.release(pid, plot_1.plot_id)
assert [^plot_2] = CommunityGarden.list_registrations(pid)
end
@tag task_id: 5
test "can get registration of a registered plot" do
assert {:ok, pid} = CommunityGarden.start()
assert %Plot{} = plot = CommunityGarden.register(pid, "Johnny Appleseed")
assert %Plot{} = registered_plot = CommunityGarden.get_registration(pid, plot.plot_id)
assert registered_plot.plot_id == plot.plot_id
assert registered_plot.registered_to == "Johnny Appleseed"
end
@tag task_id: 5
test "return not_found when attempt to get registration of an unregistered plot" do
assert {:ok, pid} = CommunityGarden.start()
assert {:not_found, "plot is unregistered"} = CommunityGarden.get_registration(pid, 1)
end
end

View File

@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true, seed: 0)