Readings, One at a Time
Stardate 55144.4 ยท UES Magnanimous
Briefing
The other Magnanimous kept everything. Its sensor log is nineteen years long and Lt. Skree does not want it in memory all at once; Skree wants to walk it, one reading at a time, stopping whenever the pattern shows up.
Build a sequence that produces readings on demand: a start value, a step, and a count. It should hand out one value when asked and not before, so that asking for the first three of a million costs three. Skree describes this as "the difference between reading a book and being hit with it".
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขProduces start, start+step, ... for exactly count values
- โขA count of zero produces nothing
- โขIt is lazy: values come one at a time on demand, not as a prebuilt list
- โขThe first three of a million cost three
- โขIt composes: the values can be doubled with a standard adaptor
Objective
Write readings(start, step, count) as a generator: it
yields start, then start + step, and so on, for exactly
count values. It must be lazy: a generator object, not a list.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Keep the struct as given and implement Iterator for it, so that
Readings::new(start, step, count) yields start, then
start + step, and so on, for exactly count values, then
None. Because it is a real iterator, .take(), .map()
and .collect() all work on it for free.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Python: yield inside a loop makes a generator, and generators are lazy by nature. Rust: a struct holding current, step and remaining, and impl Iterator with a next that returns None when nothing remains.
Debrief
Skree is walking the other ship's log. So far it is identical to ours, entry for entry, up to a stardate nineteen years ago. Skree has asked for the next reading. Then the next. Skree has stopped saying "hm" and started writing things down.