Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 743329b

Browse files
authored
Update exercises (#126)
* Update exercise instructions * Formatting exercise 2-21
1 parent e3e2cec commit 743329b

File tree

111 files changed

+3795
-4064
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+3795
-4064
lines changed

exercises/exercise_000_sudoku_solver_initial_state/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ in place in this project. scalafmt supports both Scala 2 and Scala 3. You can
147147
Scala 2 to Scala 3, you need to make sure that a matching scalafmt configuration is
148148
in place.
149149

150+
### Next steps
151+
152+
After successfully completing the tasks in this exercise, move to the next one by
153+
running the `cmtc next-exercise` from the command line.
154+
150155
### Markdown viewer in IntelliJ IDEA
151156

152157
The font size can be a bit too small for the taste of some people. You can change the

exercises/exercise_001_dotty_deprecated_syntax_rewriting/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ wildcard import syntax from using an asterix (`*`) instead of an underscore (`_`
148148
changes.
149149
- The end result should be that, after the compiler has applied its rewrites, the source code
150150
compiles clean.
151+
- Remove the `-rewrite` from `scalacOptions` in the sbt build definition.
152+
- Checkpoint the current state of your code by commiting the changes to git:
153+
154+
```scala
155+
$ git commit -a -m "Snapshot after Scala 3 compiler syntax rewrites"
156+
```
157+
158+
- Move to the next exercise by running the `cmtc next-exercise` command from the command line.
151159

152160
> NOTE: The extra bit of code that was added via `cmtc pull-template ...` can either be left as-is
153161
> or be removed. Your choice.

exercises/exercise_002_dotty_new_syntax_and_indentation_based_syntax/README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,23 @@ one came from, the formatting may be different, but semantically equivalent).
2828
val rewriteOldSyntax = Seq("-rewrite", "-old-syntax")
2929
```
3030

31-
- Before proceeding, let's take a snapshot of the current state of the exercises
32-
by executing the following commands:
31+
- If you didn't take a snapshot by committing the code changes at the end of the
32+
previous exercise, do so now by executing the following command:
3333

3434
```scala
35-
$ git add -A
36-
$ git commit -m "Snapshot before Scala 3 compiler syntax rewrites"
35+
$ git commit -a -m "Snapshot before Scala 3 compiler syntax rewrites"
3736
```
3837

3938
You can now have the compiler rewrite the source code to switch to one of the
4039
alternative syntax options.
4140

4241
- The values mentioned above each contain a specific set of compiler options
4342
for a specific syntax rewrite.
43+
- Note that syntax rewrites have to be executed one at a time. Also, consecutive
44+
syntax rewrites have to be executed in a certain order. Make sure you understand
45+
what's explained in the section named `Setting and Rewrites` at the end of the
46+
[Optional Braces](https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html)
47+
section in the Scala 3 reference documentation.
4448

4549
- Now go through the following sequence of actions:
4650
- Add one of the syntax rewrite values to the compiler option section.
@@ -62,6 +66,15 @@ documentation.
6266
> For the remainder of the exercises in this course, we will use the New Control
6367
> Structure syntax and the Fewer Braces syntax.
6468
69+
- Checkpoint the current state of the code by executing the following command:
70+
71+
```scala
72+
$ git commit -a -m "Snapshot after Scala 3 compiler syntax rewrites"
73+
```
74+
75+
- You've just completed this exercise. Let's move on to the next exercise by
76+
executing the `cmtc next-exercise` command.
77+
6578
## Source code formatting & Markdown viewer in IntelliJ
6679

6780
### Source code formatting

exercises/exercise_002_dotty_new_syntax_and_indentation_based_syntax/src/main/scala/org/lunatechlabs/dotty/sudoku/ReductionRules.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ object ReductionRules:
2222
}
2323

2424
val cellIndexesToValues =
25-
CellPossibleValues.zip(valueOccurrences).groupBy { case (value, occurrence) => occurrence }.filter {
26-
case (loc, occ) => loc.length == occ.length && loc.length <= 6
27-
}
25+
CellPossibleValues
26+
.zip(valueOccurrences)
27+
.groupBy { case (value, occurrence) => occurrence }
28+
.filter:
29+
case (loc, occ) => loc.length == occ.length && loc.length <= 6
2830

2931
val cellIndexListToReducedValue = cellIndexesToValues.map { case (index, seq) =>
3032
(index, seq.map { case (value, _) => value }.toSet)

exercises/exercise_002_dotty_new_syntax_and_indentation_based_syntax/src/main/scala/org/lunatechlabs/dotty/sudoku/SudokuDetailProcessor.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class SudokuDetailProcessor[DetailType <: SudokuDetailType: SudokuDetailProcesso
5050
import SudokuDetailProcessor.*
5151

5252
def operational(id: Int, state: ReductionSet, fullyReduced: Boolean): Behavior[Command] =
53-
Behaviors.receiveMessage {
53+
Behaviors.receiveMessage:
5454
case Update(cellUpdates, replyTo) if !fullyReduced =>
5555
val previousState = state
5656
val updatedState = mergeState(state, cellUpdates)
@@ -78,21 +78,21 @@ class SudokuDetailProcessor[DetailType <: SudokuDetailType: SudokuDetailProcesso
7878
case ResetSudokuDetailState =>
7979
operational(id, InitialDetailState, fullyReduced = false)
8080

81-
}
82-
8381
private def mergeState(state: ReductionSet, cellUpdates: CellUpdates): ReductionSet =
8482
cellUpdates.foldLeft(state) { case (stateTally, (index, updatedCellContent)) =>
8583
stateTally.updated(index, stateTally(index) & updatedCellContent)
8684
}
8785

8886
private def stateChanges(state: ReductionSet, updatedState: ReductionSet): CellUpdates =
89-
state.zip(updatedState).zipWithIndex.foldRight(cellUpdatesEmpty) {
90-
case (((previousCellContent, updatedCellContent), index), cellUpdates)
91-
if updatedCellContent != previousCellContent =>
92-
(index, updatedCellContent) +: cellUpdates
93-
94-
case (_, cellUpdates) => cellUpdates
95-
}
87+
state
88+
.zip(updatedState)
89+
.zipWithIndex
90+
.foldRight(cellUpdatesEmpty):
91+
case (((previousCellContent, updatedCellContent), index), cellUpdates)
92+
if updatedCellContent != previousCellContent =>
93+
(index, updatedCellContent) +: cellUpdates
94+
95+
case (_, cellUpdates) => cellUpdates
9696

9797
private def isFullyReduced(state: ReductionSet): Boolean =
9898
val allValuesInState = state.flatten

exercises/exercise_002_dotty_new_syntax_and_indentation_based_syntax/src/main/scala/org/lunatechlabs/dotty/sudoku/SudokuIO.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ object SudokuIO:
6565
def convertFromCellsToComplete(cellsIn: Vector[(String, Int)]): Vector[(Int, CellUpdates)] =
6666
for
6767
(rowCells, row) <- cellsIn
68-
updates = rowCells.zipWithIndex.foldLeft(cellUpdatesEmpty) {
68+
updates = rowCells.zipWithIndex.foldLeft(cellUpdatesEmpty):
6969
case (cellUpdates, (c, index)) if c != ' ' =>
7070
(index, Set(c.toString.toInt)) +: cellUpdates
7171
case (cellUpdates, _) => cellUpdates
72-
}
7372
yield (row, updates)
7473

7574
def readSudokuFromFile(sudokuInputFile: java.io.File): Vector[(Int, CellUpdates)] =

exercises/exercise_002_dotty_new_syntax_and_indentation_based_syntax/src/main/scala/org/lunatechlabs/dotty/sudoku/SudokuProblemSender.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class SudokuProblemSender private (
7474
) // on a 5 node RPi 4 based cluster in steady state, this can be lowered to about 6ms
7575

7676
def sending(): Behavior[Command] =
77-
Behaviors.receiveMessage {
77+
Behaviors.receiveMessage:
7878
case SendNewSudoku =>
7979
context.log.debug("sending new sudoku problem")
8080
val nextRowUpdates = rowUpdatesSeq.next()
@@ -83,4 +83,3 @@ class SudokuProblemSender private (
8383
case SolutionWrapper(solution: SudokuSolver.SudokuSolution) =>
8484
context.log.info(s"${SudokuIO.sudokuPrinter(solution)}")
8585
Behaviors.same
86-
}

exercises/exercise_002_dotty_new_syntax_and_indentation_based_syntax/src/main/scala/org/lunatechlabs/dotty/sudoku/SudokuProgressTracker.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SudokuProgressTracker private (
2727
import SudokuProgressTracker.*
2828

2929
def trackProgress(updatesInFlight: Int): Behavior[Command] =
30-
Behaviors.receiveMessage {
30+
Behaviors.receiveMessage:
3131
case NewUpdatesInFlight(updateCount) if updatesInFlight - 1 == 0 =>
3232
context.log.debug("NewUpdatesInFlight({}) - UpdatesInFlight={}", updateCount, updatesInFlight + updateCount)
3333
rowDetailProcessors.foreach { case (_, processor) =>
@@ -40,12 +40,11 @@ class SudokuProgressTracker private (
4040
case msg: SudokuDetailState =>
4141
context.log.error("Received unexpected message in state 'trackProgress': {}", msg)
4242
Behaviors.same
43-
}
4443

4544
def collectEndState(
4645
remainingRows: Int = 9,
4746
endState: Vector[SudokuDetailState] = Vector.empty[SudokuDetailState]): Behavior[Command] =
48-
Behaviors.receiveMessage {
47+
Behaviors.receiveMessage:
4948
case detail: SudokuDetailState if remainingRows == 1 =>
5049
sudokuSolver ! Result((detail +: endState).sortBy { case SudokuDetailState(idx, _) => idx }.map {
5150
case SudokuDetailState(_, state) => state
@@ -56,4 +55,3 @@ class SudokuProgressTracker private (
5655
case msg: NewUpdatesInFlight =>
5756
context.log.error("Received unexpected message in state 'collectEndState': {}", msg)
5857
Behaviors.same
59-
}

exercises/exercise_002_dotty_new_syntax_and_indentation_based_syntax/src/main/scala/org/lunatechlabs/dotty/sudoku/SudokuSolver.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ object SudokuSolver:
3737

3838
def apply(sudokuSolverSettings: SudokuSolverSettings): Behavior[Command] =
3939
Behaviors
40-
.supervise[Command] {
40+
.supervise[Command]:
4141
Behaviors.withStash(capacity = sudokuSolverSettings.SudokuSolver.StashBufferSize) { buffer =>
4242
Behaviors.setup { context =>
4343
new SudokuSolver(context, buffer).idle()
4444
}
4545
}
46-
}
4746
.onFailure[Exception](
4847
SupervisorStrategy.restartWithBackoff(minBackoff = 5.seconds, maxBackoff = 1.minute, randomFactor = 0.2))
4948

@@ -66,7 +65,7 @@ class SudokuSolver private (context: ActorContext[SudokuSolver.Command], buffer:
6665
context.spawn(SudokuProgressTracker(rowDetailProcessors, progressTrackerResponseMapper), "sudoku-progress-tracker")
6766

6867
def idle(): Behavior[Command] =
69-
Behaviors.receiveMessage {
68+
Behaviors.receiveMessage:
7069

7170
case InitialRowUpdates(rowUpdates, sender) =>
7271
rowUpdates.foreach { case SudokuDetailProcessor.RowUpdate(row, cellUpdates) =>
@@ -79,10 +78,8 @@ class SudokuSolver private (context: ActorContext[SudokuSolver.Command], buffer:
7978
context.log.error("Received an unexpected message in 'idle' state: {}", unexpectedMsg)
8079
Behaviors.same
8180

82-
}
83-
8481
def processRequest(requestor: Option[ActorRef[Response]], startTime: Long): Behavior[Command] =
85-
Behaviors.receiveMessage {
82+
Behaviors.receiveMessage:
8683
case SudokuDetailProcessorResponseWrapped(response) =>
8784
response match
8885
case SudokuDetailProcessor.RowUpdate(rowNr, updates) =>
@@ -184,7 +181,6 @@ class SudokuSolver private (context: ActorContext[SudokuSolver.Command], buffer:
184181
case msg: InitialRowUpdates =>
185182
buffer.stash(msg)
186183
Behaviors.same
187-
}
188184

189185
private def resetAllDetailProcessors(): Unit =
190186
for

exercises/exercise_002_dotty_new_syntax_and_indentation_based_syntax/src/test/scala/org/lunatechlabs/dotty/sudoku/CellMappingSuite.scala

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,32 @@ import org.lunatechlabs.dotty.sudoku.CellMappings.*
44

55
class CellMappingSuite extends munit.FunSuite:
66

7-
test("Mapping row coordinates should result in correct column & block coordinates") {
8-
assertEquals(rowToColumnCoordinates(0, 0), ((0, 0)))
9-
assertEquals(rowToBlockCoordinates(0, 0), ((0, 0)))
10-
assertEquals(rowToColumnCoordinates(8, 8), ((8, 8)))
11-
assertEquals(rowToBlockCoordinates(8, 8), ((8, 8)))
12-
assertEquals(rowToColumnCoordinates(3, 4), ((4, 3)))
13-
assertEquals(rowToBlockCoordinates(3, 4), ((4, 1)))
14-
assertEquals(rowToBlockCoordinates(4, 3), ((4, 3)))
15-
}
7+
test("Mapping row coordinates should result in correct column & block coordinates"):
8+
assertEquals(rowToColumnCoordinates(0, 0), ((0, 0)))
9+
assertEquals(rowToBlockCoordinates(0, 0), ((0, 0)))
10+
assertEquals(rowToColumnCoordinates(8, 8), ((8, 8)))
11+
assertEquals(rowToBlockCoordinates(8, 8), ((8, 8)))
12+
assertEquals(rowToColumnCoordinates(3, 4), ((4, 3)))
13+
assertEquals(rowToBlockCoordinates(3, 4), ((4, 1)))
14+
assertEquals(rowToBlockCoordinates(4, 3), ((4, 3)))
1615

17-
test("Mapping column coordinates should result in correct row & block coordinates") {
18-
assertEquals(columnToRowCoordinates(0, 0), ((0, 0)))
19-
assertEquals(columnToBlockCoordinates(0, 0), ((0, 0)))
20-
assertEquals(columnToRowCoordinates(8, 8), ((8, 8)))
21-
assertEquals(columnToBlockCoordinates(8, 8), ((8, 8)))
22-
assertEquals(columnToRowCoordinates(3, 4), ((4, 3)))
23-
assertEquals(columnToBlockCoordinates(3, 4), ((4, 3)))
24-
assertEquals(columnToBlockCoordinates(4, 3), ((4, 1)))
25-
}
16+
test("Mapping column coordinates should result in correct row & block coordinates"):
17+
assertEquals(columnToRowCoordinates(0, 0), ((0, 0)))
18+
assertEquals(columnToBlockCoordinates(0, 0), ((0, 0)))
19+
assertEquals(columnToRowCoordinates(8, 8), ((8, 8)))
20+
assertEquals(columnToBlockCoordinates(8, 8), ((8, 8)))
21+
assertEquals(columnToRowCoordinates(3, 4), ((4, 3)))
22+
assertEquals(columnToBlockCoordinates(3, 4), ((4, 3)))
23+
assertEquals(columnToBlockCoordinates(4, 3), ((4, 1)))
2624

27-
test("Mapping block coordinates should result in correct row & column coordinates") {
28-
assertEquals(blockToRowCoordinates(0, 0), ((0, 0)))
29-
assertEquals(blockToColumnCoordinates(0, 0), ((0, 0)))
30-
assertEquals(blockToRowCoordinates(8, 8), ((8, 8)))
31-
assertEquals(blockToColumnCoordinates(8, 8), ((8, 8)))
32-
assertEquals(blockToRowCoordinates(4, 3), ((4, 3)))
33-
assertEquals(blockToColumnCoordinates(4, 3), ((3, 4)))
34-
assertEquals(blockToRowCoordinates(3, 4), ((4, 1)))
35-
assertEquals(blockToColumnCoordinates(3, 4), ((1, 4)))
36-
assertEquals(blockToRowCoordinates(5, 5), ((4, 8)))
37-
assertEquals(blockToColumnCoordinates(5, 5), ((8, 4)))
38-
}
25+
test("Mapping block coordinates should result in correct row & column coordinates"):
26+
assertEquals(blockToRowCoordinates(0, 0), ((0, 0)))
27+
assertEquals(blockToColumnCoordinates(0, 0), ((0, 0)))
28+
assertEquals(blockToRowCoordinates(8, 8), ((8, 8)))
29+
assertEquals(blockToColumnCoordinates(8, 8), ((8, 8)))
30+
assertEquals(blockToRowCoordinates(4, 3), ((4, 3)))
31+
assertEquals(blockToColumnCoordinates(4, 3), ((3, 4)))
32+
assertEquals(blockToRowCoordinates(3, 4), ((4, 1)))
33+
assertEquals(blockToColumnCoordinates(3, 4), ((1, 4)))
34+
assertEquals(blockToRowCoordinates(5, 5), ((4, 8)))
35+
assertEquals(blockToColumnCoordinates(5, 5), ((8, 4)))

0 commit comments

Comments
 (0)