Files
learning-hour-maker/ideas/slice-with-lambda/exercise/rehearsal-runs/C+Q/pi-stdout.txt
T
Your Name 77ae05c28c 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
2026-09-29 15:47:30 +00:00

72 lines
3.3 KiB
Plaintext

The test compilation error is expected — the test still calls the old `longRangeForecast("Q1")` signature which has been replaced by the no-arg overload and the new `LongFunction<Boolean>` parameterized version, per the task requirements. The AGENTS.md notes that test failures are expected.
---
## Refactoring complete. Final diff of `LongRangeForecasts.java`:
```diff
import java.util.*;
+import java.util.function.LongFunction;
public class LongRangeForecasts {
private final Scorer scorer = new Scorer();
- public Map<IceCream, Integer> longRangeForecast(String quarter) {
+ public Map<IceCream, Integer> longRangeForecast() {
+ return longRangeForecast(d -> scorer.lookupWeather(d));
+ }
+ public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather) {
var result = new HashMap<IceCream, Integer>();
- if (Objects.equals(quarter, "Q1") || Objects.equals(quarter, "Q4")) {
- for (IceCream flavour : IceCream.values()) {
- result.put(flavour, 5);
- }
- }
for (IceCream flavour : IceCream.values()) {
result.put(flavour, 0);
}
@@ -25,15 +24,7 @@
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) {
- scorer.updateSelection();
- var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
- long daysForward = ChronoUnit.DAYS.between(forecastDate, interestingDate);
- Boolean lookupWeather = scorer.lookupWeather(daysForward);
- expectedWeather.add(lookupWeather);
- }
-
+ List<Boolean> expectedWeather = collectExpectedWeather(interestingDates, dayOffsetToWeather);
for (IceCream flavour : result.keySet()) {
result.put(flavour, result.get(flavour) + 10);
var sunnyHolidays = expectedWeather.stream().filter(s -> s).count();
@@ -46,4 +37,15 @@
}
return result;
}
+
+ private List<Boolean> collectExpectedWeather(List<Instant> dates, LongFunction<Boolean> dayOffsetToWeather) {
+ List<Boolean> expectedWeather = new ArrayList<>();
+ for (Instant date : dates) {
+ scorer.updateSelection();
+ var forecastDate = Instant.parse("2023-04-26T14:00:00.000-07:00");
+ long daysForward = ChronoUnit.DAYS.between(forecastDate, date);
+ expectedWeather.add(dayOffsetToWeather.apply(daysForward));
+ }
+ return expectedWeather;
+ }
}
```
## Summary of changes:
| Step | Change |
|------|--------|
| **1** | Added `import java.util.function.LongFunction`; introduced `dayOffsetToWeather` lambda wrapping `scorer.lookupWeather()` in the original method body, replacing direct scorer calls |
| **2** | Extracted the weather-collection loop into a new `private List<Boolean> collectExpectedWeather(List<Instant>, LongFunction<Boolean>)` method |
| **3** | Removed `String quarter` parameter; added a **no-arg** `longRangeForecast()` that delegates to the parameterized overload wrapping the `scorer`; renamed the main method to accept `LongFunction<Boolean> dayOffsetToWeather` |