Ad hoc ranges

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Jan 21 16:12:56 PST 2011


On 1/21/11 3:15 PM, Tomek Sowiński wrote:
> Jonathan M Davis napisał:
>
>>> I don't know a terser way to get a full-fledged range. It comes at a cost,
>>> though. Lazy parameters are just sugar over delegates, so it's not exactly
>>> Usain Bolt**... And you can't return it because by bug or by design lazy
>>> parameters (unlike vanilla delegates) don't work like closures. Still,
>>> even with the overhead and limitations the idiom is remarkably useful,
>>> especially in face of range-unfriendly libraries from outside D realm.
>>>
>>> Enjoy.
>>
>> What types of stuff do you need ad-hoc ranges for? What's the use case? I've
>> never actually needed such a thing. I'm curious. If it's really something that's
>> likely to be generally useful, then a function similar to what you're suggesting
>> probably should be added to std.range.
>
> Like I said, anything that doesn't bother to expose range-interfaced iterators and is not performance critical is considered a target for ad hoc ranges. Working with non-D libraries, or libraries ported to D but preserving mother-language idioms. Tasks like traversing a tree of GUI widgets, or business specific objects where defining proper ranges rarely happens and is use-case driven in practice. I expect they could be of some use in unittesting as mock input. Vaguely related: educational -- ad hoc ranges read almost like a for loop so the learning curve for ranges in general is eased off.
>
> Adding them to Phobos is an interesting idea. We need to evaluate their worth, though.
>
> Everybody: if you could write up a one-liner like range(empty, popFront, front), what would you use it for?

How about a singleton range - a range with exactly one element. It could 
be done with repeat(x, 1) but let's try it with your function as a 
warm-up exercise.

Andrei


More information about the Digitalmars-d mailing list