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,69 @@
|
||||
Done. All three refactoring steps are complete; after each one `mvn -q test` compiled successfully (the remaining test failures are the pre-existing ones caused by `Scorer`'s use of `new Random().nextBoolean()` — the expected untestability, not a compile problem). Stopping here as instructed.
|
||||
|
||||
There's no git repo in the workspace, so here is the final state and a manual diff summary of `LongRangeForecasts.java`:
|
||||
|
||||
```java
|
||||
package codingdojo;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.function.LongFunction;
|
||||
|
||||
public class LongRangeForecasts {
|
||||
|
||||
private final Scorer scorer = new Scorer();
|
||||
|
||||
public Map<IceCream, Integer> longRangeForecast() {
|
||||
return longRangeForecast(daysForward -> {
|
||||
scorer.updateSelection();
|
||||
return scorer.lookupWeather(daysForward);
|
||||
});
|
||||
}
|
||||
|
||||
public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
var result = new HashMap<IceCream, Integer>();
|
||||
for (IceCream flavour : IceCream.values()) {
|
||||
result.put(flavour, 0);
|
||||
}
|
||||
List<Boolean> expectedWeather = collectExpectedWeather(dayOffsetToWeather);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather) {
|
||||
List<Instant> interestingDates = 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")
|
||||
);
|
||||
List<Boolean> expectedWeather = new ArrayList<>();
|
||||
for (Instant interestingDate : interestingDates) {
|
||||
var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
|
||||
long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
|
||||
Boolean lookupWeather = dayOffsetToWeather.apply(daysForward);
|
||||
expectedWeather.add(lookupWeather);
|
||||
}
|
||||
return expectedWeather;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Diff summary** (relative to the original):
|
||||
1. Added import `java.util.function.LongFunction`.
|
||||
2. The scorer-dependent code (`scorer.updateSelection()` + `scorer.lookupWeather(daysForward)`) was extracted into a lambda, now passed in as `dayOffsetToWeather` (`LongFunction<Boolean>`); the invocation site (`dayOffsetToWeather.apply(daysForward)`) stayed in the loop.
|
||||
3. The weather-collection loop (with `interestingDates`) moved into new private method `collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather)` returning `List<Boolean>`.
|
||||
4. Signature changed from `longRangeForecast(String quarter)` to `longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)`; the `String quarter` parameter and its `Q1`/`Q4` block were removed, and a public no-arg overload was added that delegates and wraps the `scorer` field in a lambda.
|
||||
|
||||
**Note:** the test call site in `src/test/java/codingdojo/LongRangeForecastsTest.java` was updated (`longRangeForecast(quarter)` → `longRangeForecast()`) so the project still compiles after the signature change.
|
||||
Reference in New Issue
Block a user