Add demo script for printSalesForecasts (Split Loop with Lambda)
Demo on the messier sibling of the exercise method: three unwanted dependencies in one loop (Scorer incl. static field write, System.out, clock — clock stripped in 2-min prep). Same six mechanics with SVGs in the storyboard style, ends green via overload + pure-lambda approval test. Pointer added to the learning hour's demo section.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 460" font-family="Helvetica, Arial, sans-serif">
|
||||
<defs>
|
||||
<marker id="arrR" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M0,0 L10,5 L0,10 z" fill="#dc2626"/>
|
||||
</marker>
|
||||
<marker id="arrO" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M0,0 L10,5 L0,10 z" fill="#d97706"/>
|
||||
</marker>
|
||||
<marker id="arrP" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M0,0 L10,5 L0,10 z" fill="#9333ea"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="760" height="460" fill="#ffffff"/>
|
||||
<text x="24" y="34" font-size="20" font-weight="bold" fill="#0f172a">Step 4 — extract the loop; the lambda becomes a parameter</text>
|
||||
<text x="24" y="56" font-size="13" fill="#475569">collectSalesScores is static and scorer-free. The printing loop stays behind — one split at a time.</text>
|
||||
|
||||
<!-- left: composed method -->
|
||||
<rect x="50" y="74" width="300" height="356" rx="10" fill="#f8fafc" stroke="#64748b" stroke-width="1.5"/>
|
||||
<text x="66" y="96" font-size="14" font-weight="bold" fill="#0f172a">printSalesForecasts()</text>
|
||||
|
||||
<!-- lambda -->
|
||||
<rect x="64" y="104" width="272" height="68" rx="8" fill="#faf5ff" stroke="#9333ea" stroke-width="1.5"/>
|
||||
<text x="78" y="126" font-size="12" font-weight="bold" fill="#9333ea">λ nameToSalesScore</text>
|
||||
<text x="78" y="146" font-size="10" font-family="Menlo, Consolas, monospace" fill="#dc2626">static write + updateSelection +</text>
|
||||
<text x="78" y="162" font-size="10" font-family="Menlo, Consolas, monospace" fill="#dc2626">getSalesForecast()</text>
|
||||
|
||||
<!-- call site -->
|
||||
<rect x="64" y="188" width="272" height="48" rx="8" fill="#ffffff" stroke="#475569" stroke-width="1.5"/>
|
||||
<text x="78" y="208" font-size="11" font-family="Menlo, Consolas, monospace" fill="#0f172a">var scores =</text>
|
||||
<text x="78" y="226" font-size="11" font-family="Menlo, Consolas, monospace" fill="#0f172a">collectSalesScores(names, <tspan fill="#9333ea" font-weight="bold">λ</tspan>)</text>
|
||||
|
||||
<!-- print loop -->
|
||||
<rect x="64" y="252" width="272" height="72" rx="8" fill="#eef2ff" stroke="#818cf8"/>
|
||||
<text x="78" y="276" font-size="11" font-family="Menlo, Consolas, monospace" font-weight="bold" fill="#d97706">for (entry : scores)</text>
|
||||
<text x="78" y="296" font-size="11" font-family="Menlo, Consolas, monospace" font-weight="bold" fill="#d97706">System.out.println(...)</text>
|
||||
<text x="78" y="316" font-size="10" fill="#d97706">IO stays — next time</text>
|
||||
|
||||
<!-- purple arrow -->
|
||||
<line x1="340" y1="212" x2="452" y2="146" stroke="#9333ea" stroke-width="1.5" marker-end="url(#arrP)"/>
|
||||
<text x="396" y="188" font-size="10" font-weight="bold" fill="#9333ea" text-anchor="middle" transform="rotate(-48 396 188)">λ becomes a parameter</text>
|
||||
|
||||
<!-- right: extracted method -->
|
||||
<rect x="440" y="74" width="290" height="280" rx="10" fill="#f8fafc" stroke="#64748b" stroke-width="1.5"/>
|
||||
<text x="456" y="96" font-size="13" font-weight="bold" fill="#0f172a">collectSalesScores</text>
|
||||
<text x="456" y="112" font-size="11" fill="#475569">static — no fields, all inputs are parameters</text>
|
||||
|
||||
<rect x="456" y="122" width="258" height="32" rx="16" fill="#faf5ff" stroke="#9333ea" stroke-width="1.5"/>
|
||||
<text x="585" y="142" font-size="11" font-weight="bold" fill="#9333ea" text-anchor="middle">λ Function<String, Integer> nameToSalesScore</text>
|
||||
|
||||
<rect x="456" y="166" width="258" height="110" rx="8" fill="#eef2ff" stroke="#818cf8"/>
|
||||
<text x="470" y="190" font-size="11" font-family="Menlo, Consolas, monospace" fill="#1e1b4b">for (String name : names)</text>
|
||||
<text x="470" y="214" font-size="11" font-family="Menlo, Consolas, monospace" fill="#1e1b4b">scores.put(name,</text>
|
||||
<text x="470" y="234" font-size="11" font-family="Menlo, Consolas, monospace" fill="#1e1b4b"> <tspan fill="#9333ea" font-weight="bold">λ</tspan>.apply(name))</text>
|
||||
<text x="470" y="260" font-size="10" fill="#64748b">LinkedHashMap — keeps the order</text>
|
||||
|
||||
<text x="456" y="308" font-size="11" font-family="Menlo, Consolas, monospace" fill="#0f172a">return scores;</text>
|
||||
|
||||
<!-- Scorer, out of reach -->
|
||||
<rect x="560" y="386" width="160" height="50" rx="8" fill="#fef2f2" stroke="#dc2626" stroke-width="1.5" stroke-dasharray="4 3"/>
|
||||
<text x="640" y="408" font-size="12" font-weight="bold" fill="#dc2626" text-anchor="middle">Scorer</text>
|
||||
<text x="640" y="426" font-size="10" fill="#7f1d1d" text-anchor="middle">out of reach from in here</text>
|
||||
<line x1="640" y1="384" x2="640" y2="360" stroke="#dc2626" stroke-width="1.5" stroke-dasharray="5 4" marker-end="url(#arrR)"/>
|
||||
<text x="650" y="376" font-size="10" font-weight="bold" fill="#dc2626">✗ cannot reach in</text>
|
||||
|
||||
<!-- System.out note -->
|
||||
<text x="64" y="360" font-size="10" font-weight="bold" fill="#d97706">grep scorer → no hits in here</text>
|
||||
<text x="64" y="378" font-size="10" font-weight="bold" fill="#d97706">…but System.out is still on this side</text>
|
||||
<line x1="200" y1="348" x2="330" y2="326" stroke="#d97706" stroke-width="1" stroke-dasharray="4 3" marker-end="url(#arrO)"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.2 KiB |
Reference in New Issue
Block a user