On Friday, March 09, 2018 11:41:46 H. S. Teoh via Digitalmars-d wrote: > Today I found myself needing a lazy, caching version of map() on an > array. More precisely, given an array `T[] src` of source data and a > function func(T) that's pretty expensive to compute, return an object > `result` such that: > > - result[i] == func(src[i]), for 0 ≤ i < src.length. > > - If result[j] is never actually used, func(src[j]) is never invoked > (lazy). > > - If result[j] is referenced multiple times, a cached value is returned > after the first time, i.e., the result of func(src[j]) is cached, and > func(src[j]) is never invoked more than once. > > I couldn't figure out how to build this using Phobos primitives, so I > wrote my own implementation of it. Pretty simple, really, it's just a > wrapper struct that lazily initializes an array of Nullable!T and lazily > populates it with func(j) when opIndex(j) is invoked, and just returns > the cached value if opIndex(j) has been invoked before. > > Can this be done using current Phobos primitives?
Wasn't that what std.algorithm.iteration.cache was supposed to solve? I've never used it, so I don't know how well it fits what you need, but IIRC, using it with map was basically the use that it was invented for. - Jonathan M Davis