Code layout for range-intensive D code
Jonathan M Davis
jmdavisProg at gmx.com
Sun Jun 10 16:46:53 PDT 2012
On Sunday, June 10, 2012 23:49:12 ixid wrote:
> Having to append .array() all the time is rather annoying. I
> can't help but feel that there's a better solution than this. Are
> lazy Result methods really the default way of doing things? I'd
> rather have eager versions.
Eager versions are only good if you don't want to pass the result to another
function. And since Phobos (and plenty of user code) uses ranges all over the
place, you'd end up allocating memory all over the place, whereas right now,
you only allocate it when you explicitly call a function which allocates
memory to hold the data - e.g. array. Something like
foreach(e; map!"to!string(a)"(filter!"a <= 50"(arr, 42)))
{
doSomething(e);
if(someCondition)
break;
}
would end up allocating for both the result of filter and map, when it doesn't
actually need to allocate for _either_ of them. Right now, it can process them
lazily, only filtering and mapping for the elements that actually get
processed. By using lazy ranges, you avoid both unnecessary allocations and
avoid having to process all of the elements in a range if you don't need to.
On top of all of that, if a function doesn't return a lazy range, you _can't_
use it with infinite ranges, so if range-based functions all eagerly operated
on ranges, infinite ranges would be useless.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list