Skip to content

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 35 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
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
Apr 2, 2025
b822547
Revert "concepts.wip exercises changed to deprecated"
Apr 2, 2025
02ef965
Merge remote-tracking branch 'origin/main'
Apr 2, 2025
9e7ebc7
Merge remote-tracking branch 'origin/main'
Apr 3, 2025
09dfe5d
Change annelyns-infiltration slug for old version
Apr 3, 2025
17f24b1
Revert "Change annelyns-infiltration slug for old version"
Apr 3, 2025
5b7c4b9
Merge remote-tracking branch 'origin/main'
Apr 3, 2025
ac53662
Merge remote-tracking branch 'origin/main'
Apr 5, 2025
2af0aca
Merge remote-tracking branch 'origin/main'
Apr 5, 2025
c99cb29
Merge remote-tracking branch 'origin/main'
Apr 6, 2025
44d5047
Merge remote-tracking branch 'origin/main'
Apr 10, 2025
c368c5c
Merge remote-tracking branch 'origin/main'
Apr 11, 2025
1ff8c7e
Merge remote-tracking branch 'origin/main'
Apr 11, 2025
5b9492e
Merge remote-tracking branch 'origin/main'
Apr 14, 2025
f44fc08
Merge remote-tracking branch 'origin/main'
Apr 14, 2025
74542cc
Merge remote-tracking branch 'origin/main'
Apr 14, 2025
0eac864
Merge remote-tracking branch 'origin/main'
Apr 15, 2025
316fb7e
Merge remote-tracking branch 'origin/main'
Apr 17, 2025
ba5e3ad
Merge remote-tracking branch 'origin/main'
Apr 18, 2025
33da2a6
Merge remote-tracking branch 'origin/main'
Apr 18, 2025
d45f584
Merge remote-tracking branch 'origin/main'
Apr 19, 2025
b806247
Merge remote-tracking branch 'origin/main'
Apr 19, 2025
7eded14
Merge remote-tracking branch 'origin/main'
Apr 23, 2025
c037eca
Merge remote-tracking branch 'origin/main'
Apr 24, 2025
40cf0fd
Merge remote-tracking branch 'origin/main'
Apr 24, 2025
50c3513
Merge remote-tracking branch 'origin/main'
Apr 29, 2025
9ab3aa9
Merge remote-tracking branch 'origin/main'
May 15, 2025
21f835f
partial change to encounters
May 17, 2025
110a76a
Encounters concept exercise
May 17, 2025
5b9454a
Update design.md
colinleach May 23, 2025
f48c2a2
Update config.json
colinleach May 23, 2025
5fe734e
Update config.json
colinleach May 26, 2025
3e443c5
Merge branch 'exercism:main' into encounters
colinleach May 26, 2025
7515ef9
Update introduction.md
colinleach May 28, 2025
e3ff2eb
Update hints.md
colinleach May 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@
],
"status": "beta"
},
{
"slug": "encounters",
"name": "Encounters",
"uuid": "68001762-8a35-4494-9de4-624ca9efb5cd",
"concepts": [
"multiple-dispatch"
],
"prerequisites": [
"parametric-types"
],
"status": "beta"
},
{
"slug": "old-elyses-enchantments",
"name": "Old Elyse's Enchantments",
Expand Down Expand Up @@ -364,14 +376,6 @@
"prerequisites": [],
"status": "deprecated"
},
{
"slug": "old-encounters",
"name": "Old Encounters",
"uuid": "68001762-8a35-4494-9de4-624ca9efb5cd",
"concepts": [],
"prerequisites": [],
"status": "deprecated"
},
{
"slug": "old-emoji-times",
"name": "Old 🕑🕤🕗",
Expand Down
32 changes: 32 additions & 0 deletions exercises/concept/encounters/.docs/design.md
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).
38 changes: 38 additions & 0 deletions exercises/concept/encounters/.docs/hints.md
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
110 changes: 110 additions & 0 deletions exercises/concept/encounters/.docs/instructions.md
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.
```
Loading