-
-
Notifications
You must be signed in to change notification settings - Fork 73
Encounters concept exercise #925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
28f91a8
concepts.wip exercises changed to deprecated
b822547
Revert "concepts.wip exercises changed to deprecated"
02ef965
Merge remote-tracking branch 'origin/main'
9e7ebc7
Merge remote-tracking branch 'origin/main'
09dfe5d
Change annelyns-infiltration slug for old version
17f24b1
Revert "Change annelyns-infiltration slug for old version"
5b7c4b9
Merge remote-tracking branch 'origin/main'
ac53662
Merge remote-tracking branch 'origin/main'
2af0aca
Merge remote-tracking branch 'origin/main'
c99cb29
Merge remote-tracking branch 'origin/main'
44d5047
Merge remote-tracking branch 'origin/main'
c368c5c
Merge remote-tracking branch 'origin/main'
1ff8c7e
Merge remote-tracking branch 'origin/main'
5b9492e
Merge remote-tracking branch 'origin/main'
f44fc08
Merge remote-tracking branch 'origin/main'
74542cc
Merge remote-tracking branch 'origin/main'
0eac864
Merge remote-tracking branch 'origin/main'
316fb7e
Merge remote-tracking branch 'origin/main'
ba5e3ad
Merge remote-tracking branch 'origin/main'
33da2a6
Merge remote-tracking branch 'origin/main'
d45f584
Merge remote-tracking branch 'origin/main'
b806247
Merge remote-tracking branch 'origin/main'
7eded14
Merge remote-tracking branch 'origin/main'
c037eca
Merge remote-tracking branch 'origin/main'
40cf0fd
Merge remote-tracking branch 'origin/main'
50c3513
Merge remote-tracking branch 'origin/main'
9ab3aa9
Merge remote-tracking branch 'origin/main'
21f835f
partial change to encounters
110a76a
Encounters concept exercise
5b9454a
Update design.md
colinleach f48c2a2
Update config.json
colinleach 5fe734e
Update config.json
colinleach 3e443c5
Merge branch 'exercism:main' into encounters
colinleach 7515ef9
Update introduction.md
colinleach e3ff2eb
Update hints.md
colinleach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Design | ||
|
||
## Goal | ||
|
||
The goal of this exercise is to introduce the student to multiple dispatch, Julia's central paradigm. | ||
|
||
## Learning objectives | ||
|
||
- Understand how to create multiple methods for a function. | ||
- Understand something of how Julia chooses the "best" method to dispatch. | ||
- Understand something of why multiple dispatch is so important in making Julia performant. | ||
|
||
## Out of scope | ||
|
||
These features are not currently covered, though this could be up for discussion in future. | ||
|
||
- Method ambiguity | ||
- Parametric methods | ||
|
||
## Concepts | ||
|
||
The Concepts this exercise unlocks are: | ||
|
||
- None. | ||
|
||
## Prerequisites | ||
|
||
- `parametric-types` | ||
|
||
## Source | ||
|
||
This exercise is based on an example used in the talk "The Unreasonable Effectiveness of Multiple Dispatch" given by Stefan Karpinski at JuliaCon 2019, available on [YouTube](https://youtu.be/kc9HwsxE1OY?t=422). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Hints | ||
|
||
## 1. Define custom types | ||
|
||
Abstract types and type inheritance were discussed in the [Composite Types][composite] Concept. | ||
|
||
## 2. Get the name of the pet. | ||
|
||
- This is trivial for Dogs and Cats, but helpful to the tests for fallback methods. | ||
|
||
## 3. Define what happens when cats and dogs meet | ||
|
||
- How many combinations for cat/dog encounters are there? | ||
- Remember that a cat meeting a dog reacts differently to a dog meeting a cat. | ||
- We need the response of the first argument: `a` in `meet(a, b)`. | ||
|
||
## 4. Define an encounter between two entities. | ||
|
||
- The return value is a longer string than for `meet()`. | ||
- Only use a single method for `encounter()`. | ||
- [String interpolation][interpolation] is your friend when assembling a return value. | ||
|
||
## 5. Define a fallback reaction for encounters between pets | ||
|
||
- The second argument is now a `Pet` other than `Cat` or `Dog`, so add a `meet` method. | ||
- Declaring abstract parameter types or constraining them via parametric methods are two ways to accomplish this. | ||
|
||
## 6. Define a fallback if a pet encounters something it doesn't know | ||
|
||
- The second argument can now be anything. | ||
|
||
## 7. Define a generic fallback | ||
|
||
- Both arguments can now be anything. | ||
- At the end of the exercise, you will have 7 methods for `meet`. | ||
|
||
[composite]: https://exercism.org/tracks/julia/concepts/composite-types | ||
[interpolation]: https://docs.julialang.org/en/v1/manual/strings/#string-interpolation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Instructions | ||
|
||
In this exercise you will be implementing a simulation of encounters. | ||
This will familiarise you with the basics of multiple dispatch, Julia's main paradigm. | ||
|
||
~~~~exercism-important | ||
For this exercise, all function definitions should be a single-line declaration with no if/else logic. | ||
~~~~ | ||
|
||
In general, encounters involve one entity `a` meeting another entity `b` and reacting to it. | ||
|
||
```julia | ||
encounter(a, b) = "$(name(a)) meets $(name(b)) and $(meets(a, b))." | ||
``` | ||
|
||
At first, we will simulate what happens when cats and dogs meet. | ||
|
||
## 1. Define custom types | ||
|
||
First add an abstract type `Pet`. | ||
|
||
Then define types `Cat` and `Dog` as subtypes of `Pet`. | ||
Each has a single field `name`. | ||
|
||
## 2. Get the name of the pet. | ||
|
||
Implement a function `name()` which returns the name of a pet. | ||
|
||
```julia-repl | ||
julia> fido = Dog("Fido") | ||
Dog("Fido") | ||
|
||
julia> name(fido) | ||
"Fido" | ||
``` | ||
|
||
## 3. Define what happens when cats and dogs meet | ||
|
||
Implement `meets()` methods for the following encounters: | ||
|
||
- When two dogs meet, they **sniff** each other. | ||
- When a cat meets a dog, it **hisses** at the dog. | ||
- When a dog meets a cat, it **chases** the cat. | ||
- When two cats meet, they **slink**. | ||
|
||
```julia-repl | ||
julia> ginger = Cat("Ginger") | ||
Cat("Ginger") | ||
|
||
julia> meets(ginger, fido) | ||
"hisses" | ||
``` | ||
|
||
## 4. Define an encounter between two entities. | ||
|
||
Implement the `encounter()` function with one method, to report what happens when two named entities meet. | ||
|
||
```julia-repl | ||
julia> encounter(ginger, fido) | ||
"Ginger meets Fido and hisses." | ||
``` | ||
|
||
## 5. Define a fallback reaction for encounters between pets | ||
|
||
What happens if they encounter a different pet on their walk, like a [horse](https://www.dw.com/en/horse-takes-daily-stroll-through-frankfurt-without-owner/a-47833431)? | ||
|
||
- If a pet meets another pet that it doesn't recognize, it **is cautious**. | ||
|
||
```julia-repl | ||
julia> bella = Horse("Bella") | ||
Horse("Bella") | ||
|
||
julia> encounter(fido, bella) | ||
"Fido meets Bella and is cautious." | ||
``` | ||
|
||
## 6. Define a fallback if a pet encounters something it doesn't know | ||
|
||
There are many other things that pets may encounter that aren't pets: cars, humans, plants, natural disasters, asteroids… What happens then? | ||
|
||
- If a pet meets something it has never seen before, it **runs away**. | ||
|
||
```julia-repl | ||
julia> colnago = Bicycle("Colnago") | ||
Bicycle("Colnago") | ||
|
||
julia> encounter(ginger, colnago) | ||
"Ginger meets Colnago and runs away." | ||
``` | ||
|
||
## 7. Define a generic fallback | ||
|
||
There are many other encounters that could occur in our simulation. | ||
A car meets a dog, two electrons encounter each other and interact… | ||
|
||
It is impossible to cover all encounters in advance, therefore we will implement a generic fallback. | ||
|
||
- If two unknown things or beings encounter each other, **nothing happens**. | ||
|
||
```julia-repl | ||
julia> γ1 = Photon("γ1") | ||
Photon("γ1") | ||
|
||
julia> γ2 = Photon("γ2") | ||
Photon("γ2") | ||
|
||
julia> encounter(γ1, γ2) | ||
"γ1 meets γ2 and nothing happens." | ||
# This is true, photons just pass through each other. | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.