Skip to content

Commit

Permalink
Merge pull request #966 from DockYard-Academy/early_content_review
Browse files Browse the repository at this point in the history
Task Drill Solutions
  • Loading branch information
BrooklinJazz committed Jun 4, 2023
2 parents 3281ac8 + ed1208a commit 2134be8
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 78 deletions.
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ Have you tried turning it off and on again?
<summary>Example Solution</summary>

```elixir
initial_count = 0
initial_count + high

```

</details>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ See [start.livemd](https://github.com/DockYard-Academy/curriculum/blob/main/star

<!-- course-outline-end -->


2 changes: 1 addition & 1 deletion exercises/capstone_entity_relationship_diagram.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ See https://mermaid.js.org/syntax/entityRelationshipDiagram.html for the syntax.
### Requirements

* Document the entities and their fields.
* Document the associations (many-to-one, many-to-many, one-to-one) in your capstone project
* Document the associations (many-to-one, many-to-many, one-to-one).

## Commit Your Progress

Expand Down
2 changes: 1 addition & 1 deletion exercises/games_supervised_score_tracker.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Increase the `ScoreTracker`'s score whenever a user wins the game.
<!-- livebook:{"force_markdown":true} -->

```elixir
# Games.ScoreTracker Should Be A Named Process, So It Is Not Necessary To Send The Pid.
# Games.ScoreTracker should be a named process, so we don't need to provide the pid.
{:ok, _pid} = Games.ScoreTracker.start_link()
:ok = Games.Score.add_points(10)
:ok = Games.Score.add_points(10)
Expand Down
2 changes: 1 addition & 1 deletion exercises/group_project_blog.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ $ git init

Edit the `README.md` file initialized in your Phoenix project.

Include alteast the following information:
Include at least the following information:

* Project Name
* Project Summary
Expand Down
2 changes: 1 addition & 1 deletion exercises/stack.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ In addition to the above, add documentation and at least one doctest for the `pu

This will be a **pair testing** exercise Using TDD (Test Driven Development). We recommend using Visual Studio Code LiveShare to collaborate.

1. One student (**the tester**) will write a test
1. One student (**the tester**) will write a single test
2. The other student (**the implementer**) will implement **only** the code necessary to make the test pass.
3. **Swap roles after each test.**

Expand Down
44 changes: 15 additions & 29 deletions exercises/supervised_stack.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ Mix.install([

## Supervised Stack

Previously we created a [Stack](./stack_server.livemd) [GenServer](https://hexdocs.pm/elixir/GenServer.html) process.
You're going to create a stack project that starts a name [Stack](./stack_server.livemd) GenServer process under a supervisor.

We've made a slight modification to the `Stack` process by adding a `start_link/1` function so this named `Stack` can be started under a [Supervisor](https://hexdocs.pm/elixir/Supervisor.html).
```
mix new stack
```

Here's an example Stack GenServer you can use.

```elixir
defmodule Stack do
Expand Down Expand Up @@ -63,30 +67,13 @@ defmodule Stack do
end
```

We're able to push and pop elements off of the stack.

```elixir
{:ok, stack_pid} = GenServer.start_link(Stack, [])

GenServer.call(stack_pid, {:push, 1}) |> IO.inspect(label: "push")
GenServer.call(stack_pid, {:push, 2}) |> IO.inspect(label: "push")
GenServer.call(stack_pid, {:push, 3}) |> IO.inspect(label: "push")

GenServer.call(stack_pid, :pop) |> IO.inspect(label: "pop")
GenServer.call(stack_pid, :pop) |> IO.inspect(label: "pop")
GenServer.call(stack_pid, :pop) |> IO.inspect(label: "pop")

# The Final State Is Empty.
:sys.get_state(stack_pid)
```

However, there's a bug. If we try to `pop/1` an item off of an empty stack, the process
We're able to push and pop elements off of the stack. However, there's a bug. If we try to `pop/1` an item off of an empty stack, the process
will crash due to a function clause error because the `handle_call/2` function expects a list with one or more elements.

Uncomment the following code to watch the `Stack` crash.

```elixir
# {:ok, Pid} = GenServer.start_link(Stack, [])
# {:ok, pid} = GenServer.start_link(Stack, [])
# GenServer.call(stack_pid, :pop)
```

Expand All @@ -102,7 +89,7 @@ def handle_call(:pop, _from, []) do
end
```

Instead, you're going to start the Stack process under a supervisor so that it will be restarted when it crashes. In the Elixir cell below, start the `Stack` process under a supervisor so that it will restart with an empty stack when it crashes.
Instead, you're going to start the `Stack` process under a supervisor in your application so that it will be restarted when it crashes.

<details style="background-color: lightgreen; padding: 1rem; margin: 1rem 0;">
<summary>Example Solution</summary>
Expand All @@ -117,18 +104,17 @@ children = [

</details>

Keep in mind, if you have already started a supervisor with the `Stack` process, your livebook may crash. You can resolve this issue by simply re-running the cell below to start the supervisor again.
<!-- livebook:{"break_markdown":true} -->

```elixir
### Crash The Stack

```

You should be able to send a `:pop` message to the `Stack` process and the [Supervisor](https://hexdocs.pm/elixir/Supervisor.html) will restart the `Stack` process.
Open the [IEx](https://hexdocs.pm/iex/IEx.html) shell and send the `Stack` a `:pop` message to cause it to crash and restart.

Uncomment and evaluate the code below to test your supervisor.
<!-- livebook:{"force_markdown":true} -->

```elixir
# GenServer.call(Stack, :pop)
$ iex -S mix
iex> GenServer.call(Stack, :pop)
```

## Commit Your Progress
Expand Down

0 comments on commit 2134be8

Please sign in to comment.