mirror of
https://github.com/rjNemo/exercism-elixir
synced 2026-06-06 02:16:48 +00:00
garden
This commit is contained in:
parent
50f5346cc4
commit
970b22c91f
11 changed files with 418 additions and 0 deletions
25
community-garden/.exercism/config.json
Normal file
25
community-garden/.exercism/config.json
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
{
|
||||||
|
"blurb": "Learn about the Agent module by helping your local community handle community garden registrations.",
|
||||||
|
"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"
|
||||||
|
}
|
||||||
1
community-garden/.exercism/metadata.json
Normal file
1
community-garden/.exercism/metadata.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{"track":"elixir","exercise":"community-garden","id":"c9d10f1ecc9446fbbae0860e23f3ee03","url":"https://exercism.org/tracks/elixir/exercises/community-garden","handle":"rjNemo","is_requester":true,"auto_approve":false}
|
||||||
4
community-garden/.formatter.exs
Normal file
4
community-garden/.formatter.exs
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
||||||
24
community-garden/.gitignore
vendored
Normal file
24
community-garden/.gitignore
vendored
Normal 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
|
||||||
|
|
||||||
75
community-garden/HELP.md
Normal file
75
community-garden/HELP.md
Normal 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)
|
||||||
|
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
|
||||||
|
- 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.
|
||||||
|
If you can't find what you're looking for in the documentation, feel free to ask help in the Exercism's BEAM [gitter channel](https://gitter.im/exercism/xerlang).
|
||||||
36
community-garden/HINTS.md
Normal file
36
community-garden/HINTS.md
Normal 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 may need 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://elixir-lang.org/getting-started/mix-otp/agent.html
|
||||||
93
community-garden/README.md
Normal file
93
community-garden/README.md
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
# 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. 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.
|
||||||
|
|
||||||
|
## 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 a 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.
|
||||||
|
|
||||||
|
```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
|
||||||
47
community-garden/lib/community_garden.ex
Normal file
47
community-garden/lib/community_garden.ex
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
# 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(opts \\ nil) do
|
||||||
|
Agent.start_link(fn -> {[], 0} end, name: Plot)
|
||||||
|
end
|
||||||
|
|
||||||
|
def list_registrations(pid) do
|
||||||
|
Agent.get(pid, fn {list, _} -> list end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def register(pid, register_to) do
|
||||||
|
id = Agent.get(pid, fn {_, uid} -> uid end)
|
||||||
|
|
||||||
|
plot = %Plot{plot_id: id, registered_to: register_to}
|
||||||
|
Agent.update(pid, fn {list, id} -> {[plot | list], id + 1} end)
|
||||||
|
plot
|
||||||
|
end
|
||||||
|
|
||||||
|
def release(pid, plot_id) do
|
||||||
|
Agent.update(pid, fn {list, id} ->
|
||||||
|
{Enum.filter(
|
||||||
|
list,
|
||||||
|
fn %Plot{plot_id: id} -> plot_id != id end
|
||||||
|
), id}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_registration(pid, plot_id) do
|
||||||
|
Agent.get(pid, fn {list, _} ->
|
||||||
|
res =
|
||||||
|
Enum.filter(
|
||||||
|
list,
|
||||||
|
fn %Plot{plot_id: id} -> plot_id == id end
|
||||||
|
)
|
||||||
|
|
||||||
|
case res do
|
||||||
|
[] -> {:not_found, "plot is unregistered"}
|
||||||
|
_ -> hd(res)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
28
community-garden/mix.exs
Normal file
28
community-garden/mix.exs
Normal 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
|
||||||
83
community-garden/test/community_garden_test.exs
Normal file
83
community-garden/test/community_garden_test.exs
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
defmodule CommunityGardenTest do
|
||||||
|
use ExUnit.Case, async: false
|
||||||
|
|
||||||
|
@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 "registered plots have unique id" do
|
||||||
|
assert {:ok, pid} = CommunityGarden.start()
|
||||||
|
CommunityGarden.register(pid, "Johnny Appleseed")
|
||||||
|
CommunityGarden.register(pid, "Frederick Law Olmsted")
|
||||||
|
CommunityGarden.register(pid, "Lancelot (Capability) Brown")
|
||||||
|
|
||||||
|
plots = pid |> CommunityGarden.list_registrations()
|
||||||
|
unique_ids = plots |> Enum.map(& &1.plot_id) |> Enum.uniq()
|
||||||
|
assert length(plots) == length(unique_ids)
|
||||||
|
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")
|
||||||
|
|
||||||
|
ids = CommunityGarden.list_registrations(pid) |> Enum.map(& &1.plot_id)
|
||||||
|
|
||||||
|
CommunityGarden.release(pid, plot_1.plot_id)
|
||||||
|
CommunityGarden.release(pid, plot_2.plot_id)
|
||||||
|
|
||||||
|
CommunityGarden.register(pid, "John Doe")
|
||||||
|
CommunityGarden.register(pid, "Jane Doe")
|
||||||
|
|
||||||
|
new_ids = CommunityGarden.list_registrations(pid) |> Enum.map(& &1.plot_id)
|
||||||
|
|
||||||
|
refute Enum.sort(ids) == Enum.sort(new_ids)
|
||||||
|
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
|
||||||
2
community-garden/test/test_helper.exs
Normal file
2
community-garden/test/test_helper.exs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
ExUnit.start()
|
||||||
|
ExUnit.configure(exclude: :pending, trace: true, seed: 0)
|
||||||
Loading…
Reference in a new issue