Skip to main content

Practice ยท 3 of 4

Enumerate Once

CHALLENGE
Difficulty: intermediate+25 XP

FilterTwice enumerates its pipeline two times (a Count() gate, then the render loop) โ€” the filter (and the source!) runs twice. Write Solution.Heavy(IEnumerable<int> source) that: - filters to even numbers (in order), - enumerates the pipeline **exactly once** in total, - returns the count and the joined values in one object: ``csharp public static class Solution { public sealed class Stats { public int Count; public string Joined = ""; } public static Stats Heavy(System.Collections.Generic.IEnumerable<int> source); // e.g. [1,2,3,4] -> Count=2, Joined="2|4" } ` CountingProbe (provided) records how many times a source is enumerated: wrap it via probe.Src() so the tests can see the true enumeration count. `csharp // provided: public sealed class CountingProbe { public int Hits; public IEnumerable<int> Src(params int[] xs); } ``

Back to lesson: Practice: Performance Practice: Build, Pick, Materialize