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,92 @@
|
||||
All four steps are complete. Here's the summary:
|
||||
|
||||
## What was done
|
||||
|
||||
1. **Lambda introduced** (manual edit): the `scorer.updateSelection()` + `scorer.lookupWeather(...)` code became a `LongFunction<Boolean> dayOffsetToWeather` lambda defined at the top of the method; the loop now calls `dayOffsetToWeather.apply(daysForward)`.
|
||||
2. **Extracted** the weather-collection loop with `java-refactor extract` (the tool generated `collectExpectedWeather(interestingDates, dayOffsetToWeather, expectedWeather)`), then a minimal manual cleanup gave it the suggested shape `private List<Boolean> collectExpectedWeather(LongFunction<Boolean> dayOffsetToWeather)` returning the list.
|
||||
3. **Signature changed** (manual edit — neither `change-method-signature` nor `introduce-param` supports removing the `quarter` parameter): now `public Map<IceCream, Integer> longRangeForecast(LongFunction<Boolean> dayOffsetToWeather)` plus a public no-arg overload delegating with a lambda wrapping the `scorer` field. The `quarter` branch was dropped (it was inert anyway — its `put(flavour, 5)` was immediately overwritten by `put(flavour, 0)`), and the test call site updated to `longRangeForecast()`.
|
||||
4. `mvn -q test` was run after every change: code compiles throughout; the 2 remaining failures (`LongRangeForecastsTest`, `DailyForecastsTest`) are the expected pre-existing ones caused by the `Scorer` state dependency.
|
||||
|
||||
## Final diff — `src/main/java/codingdojo/LongRangeForecasts.java`
|
||||
|
||||
```diff
|
||||
@@ -3,36 +3,26 @@
|
||||
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(String quarter) {
|
||||
+ 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>();
|
||||
- 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);
|
||||
}
|
||||
- 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) {
|
||||
- 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);
|
||||
- }
|
||||
+ var expectedWeather = collectExpectedWeather(dayOffsetToWeather);
|
||||
|
||||
for (IceCream flavour : result.keySet()) {
|
||||
result.put(flavour, result.get(flavour) + 10);
|
||||
@@ -46,4 +36,21 @@
|
||||
}
|
||||
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;
|
||||
+ }
|
||||
}
|
||||
```
|
||||
|
||||
Supporting change in `src/test/java/codingdojo/LongRangeForecastsTest.java`:
|
||||
|
||||
```diff
|
||||
- var quarter = "Q3";
|
||||
- var actual = new LongRangeForecasts().longRangeForecast(quarter);
|
||||
+ var actual = new LongRangeForecasts().longRangeForecast();
|
||||
```
|
||||
|
||||
Stopping here as instructed.
|
||||
Reference in New Issue
Block a user