Rename split-loop-with-lambda to slice-with-lambda
The refactoring is now published as Slice on sammancoaching.org: https://sammancoaching.org/refactorings/slice.html - git mv ideas/split-loop-with-lambda -> ideas/slice-with-lambda - rename learning-hour file and update frontmatter name - update paths in tools/rehearse scripts and report title - update prose: 'Split Loop with Lambda' -> 'Slice with Lambda' in learning hour, exercise READMEs, storyboard, solution comments - link the published Slice refactoring from the pattern draft
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
target/
|
||||
*.received.txt
|
||||
.idea/
|
||||
*.iml
|
||||
@@ -0,0 +1,27 @@
|
||||
# Reference solution — end state after step 3
|
||||
|
||||
Derived from [IceCreamForecasting-Refactoring-Kata](https://github.com/emilybache/IceCreamForecasting-Refactoring-Kata)
|
||||
(same license as the kata applies to the copied `IceCream.java`).
|
||||
|
||||
Run with `mvn test` — the approval test passes deterministically without
|
||||
touching the weather service.
|
||||
|
||||
## What changed vs the exercise start
|
||||
|
||||
1. Weather loop extracted to `collectExpectedWeather(LongFunction<Boolean>)`
|
||||
— a static method whose only "unwanted dependency" is the lambda parameter.
|
||||
2. Public no-arg `longRangeForecast()` delegates to a new overload that takes
|
||||
the weather function, keeping existing callers working.
|
||||
3. Test passes a pure lambda (`daysForward -> true`) — the original test
|
||||
called the no-arg method and was flaky because of `Scorer`'s `Random`.
|
||||
|
||||
## Variations to try (for the debrief or a second round)
|
||||
|
||||
- `daysForward -> false` (no sunny holidays): scores drop to `+10` only.
|
||||
- `daysForward -> daysForward == 5` (exactly one sunny holiday, and it is not
|
||||
`expectedWeather.get(1)`, so no `+2`): all flavours `10`.
|
||||
- Delete the `scorer.updateSelection()` call inside the production lambda:
|
||||
the approved file does not change — the forecast never reads `Scorer.flavour`.
|
||||
- Variation end state: skip the overload; make `collectExpectedWeather`
|
||||
package-private and test it directly with pure lambdas. Trade-off:
|
||||
tests the pure slice but not the composition (scoring loop).
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>codingdojo</groupId>
|
||||
<artifactId>icecream-split-loop-lambda-exercise</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.10.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.approvaltests</groupId>
|
||||
<artifactId>approvaltests</artifactId>
|
||||
<version>18.6.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,7 @@
|
||||
package codingdojo;
|
||||
|
||||
public enum IceCream {
|
||||
Strawberry,
|
||||
Chocolate,
|
||||
Vanilla;
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package codingdojo;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.function.LongFunction;
|
||||
|
||||
/**
|
||||
* Reference end state of the learning hour exercise (step 3 of the
|
||||
* Slice with Lambda mechanics — stop here; step 4, moving the method
|
||||
* to another class, is future work).
|
||||
*
|
||||
* Derived from https://github.com/emilybache/IceCreamForecasting-Refactoring-Kata
|
||||
*/
|
||||
public class LongRangeForecasts {
|
||||
|
||||
private static final Instant ANCHOR_DATE = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
private static final List<Instant> DATES = List.of(
|
||||
Instant.parse("2023-05-01T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-05-18T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-06-05T14:00:00.000-07:00"),
|
||||
Instant.parse("2023-06-23T14:00:00.000-07:00")
|
||||
);
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
public Map<IceCream, Integer> longRangeForecast() {
|
||||
return longRangeForecast(daysForward -> {
|
||||
scorer.updateSelection();
|
||||
return scorer.lookupWeather(daysForward);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The forecast, with the weather source injected as a function.
|
||||
* Testable: pass any pure function instead of calling the weather service.
|
||||
*/
|
||||
public Map<IceCream, Integer> longRangeForecast(
|
||||
LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var expectedWeather = collectExpectedWeather(dayOffsetToWeather);
|
||||
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
for (IceCream flavour : IceCream.values()) {
|
||||
result.put(flavour, 0);
|
||||
}
|
||||
for (IceCream flavour : result.keySet()) {
|
||||
result.put(flavour, result.get(flavour) + 10);
|
||||
var sunnyHolidays = expectedWeather.stream().filter(s -> s).count();
|
||||
if (sunnyHolidays > 2 && flavour == IceCream.Vanilla) {
|
||||
result.put(flavour, result.get(flavour) + 5);
|
||||
}
|
||||
if (sunnyHolidays > 1 && expectedWeather.get(1)) {
|
||||
result.put(flavour, result.get(flavour) + 2);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracted method. Note: it uses no fields — all its inputs are
|
||||
* parameters. The only leftover dependency is the lambda, which can
|
||||
* be replaced by any pure function in tests.
|
||||
*/
|
||||
private static List<Boolean> collectExpectedWeather(
|
||||
LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var expectedWeather = new ArrayList<Boolean>();
|
||||
for (Instant interestingDate : DATES) {
|
||||
long daysForward = ChronoUnit.DAYS.between(ANCHOR_DATE, interestingDate);
|
||||
expectedWeather.add(dayOffsetToWeather.apply(daysForward));
|
||||
}
|
||||
return expectedWeather;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package codingdojo;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Trimmed version of the kata's Scorer: only the parts LongRangeForecasts uses.
|
||||
* lookupWeather stands in for an API call to a weather service — in the real
|
||||
* system this is the "unwanted dependency".
|
||||
*/
|
||||
public class Scorer {
|
||||
|
||||
public static IceCream flavour = null;
|
||||
|
||||
public int getScore() {
|
||||
boolean sunnyToday = lookupWeather();
|
||||
if (flavour == null) {
|
||||
return -1;
|
||||
}
|
||||
switch (flavour) {
|
||||
case Strawberry:
|
||||
return sunnyToday ? 10 : 5;
|
||||
case Chocolate:
|
||||
return 6;
|
||||
case Vanilla:
|
||||
return sunnyToday ? 7 : 5;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean lookupWeather() {
|
||||
return new Random().nextBoolean();
|
||||
}
|
||||
|
||||
public boolean lookupWeather(long daysForward) {
|
||||
// placeholder implementation - real version would make API call to weather service
|
||||
return new Random().nextBoolean();
|
||||
}
|
||||
|
||||
public void updateSelection() {
|
||||
// placeholder implementation - real version would use machine learning to predict sales
|
||||
var score = getScore();
|
||||
if (score > 5) {
|
||||
var randomizer = new Random();
|
||||
IceCream[] allFlavours = IceCream.values();
|
||||
flavour = allFlavours[randomizer.nextInt(allFlavours.length)];
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package codingdojo;
|
||||
|
||||
import org.approvaltests.Approvals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.function.LongFunction;
|
||||
|
||||
public class LongRangeForecastsTest {
|
||||
|
||||
@Test
|
||||
void longRangeForecast() {
|
||||
// pure weather implementation: every holiday is sunny
|
||||
LongFunction<Boolean> alwaysSunny = daysForward -> true;
|
||||
var actual = new LongRangeForecasts().longRangeForecast(alwaysSunny);
|
||||
Approvals.verify(printIceCreamForecast(actual));
|
||||
}
|
||||
|
||||
private static String printIceCreamForecast(Map<IceCream, Integer> actual) {
|
||||
var printedResult = new StringBuilder();
|
||||
printedResult.append("{");
|
||||
var sortedKeys = new ArrayList<>(actual.keySet());
|
||||
Collections.sort(sortedKeys);
|
||||
for (IceCream key : sortedKeys) {
|
||||
printedResult.append(key);
|
||||
printedResult.append(":");
|
||||
printedResult.append(actual.get(key));
|
||||
printedResult.append(", ");
|
||||
}
|
||||
printedResult.append("}");
|
||||
return printedResult.toString();
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
{Strawberry:12, Chocolate:12, Vanilla:17, }
|
||||
Reference in New Issue
Block a user