On Saturday, 22 October 2022 at 15:21:07 UTC, Kevin Bailey wrote:
OTOH, a forward iterator (i.e. copyable but does not need to go backwards) solves the problem elegantly and efficiently.
The builtin function [`byKeyValue`][1] returns a forward range (D's version of a forward iterator) over the contents of an associative array. To save the current iteration state, use the `.save` method.
Here's a simple example: ```d import std.algorithm, std.range, std.stdio; void main() { auto aa = ["x": 123, "y": 456, "z": 789, "w": 10]; // name of range type is private, so use auto + typeof auto it = aa.byKeyValue; typeof(it) saved; // iterate once and save our position partway through for (; !it.empty; it.popFront) { auto e = it.front; if (e.key == "y") saved = it.save; writefln("%s: %s", e.key, e.value); } writeln("--------"); // iterate again from saved position foreach (e; saved) writefln("%s: %s", e.key, e.value); } ``` [1]: https://druntime.dpldocs.info/object.byKeyValue.1.html