"Andrei Alexandrescu" <[email protected]> wrote in message
news:[email protected]...
> On 7/10/12 11:11 AM, Christophe Travert wrote:
>> If you do not want the heap allocation of the array, you can create a
>> one-element range to feed to chain (maybe such a thing could be placed
>> in phobos, next to takeOne).
>>
>> struct OneElementRange(E)
>> {
>> E elem;
>> bool passed;
>> @property ref E front() { return elem; }
>> void popFront() { passed = true; }
>> @property bool empty() { return passed; }
>> @property size_t length() { return 1-passed; }
>> //...
>> }
>
> Yah, probably we should add something like this:
>
> auto singletonRange(E)(E value)
> {
> return repeat(value).takeExactly(1);
> }
>
> I don't think it would be considerably less efficient than a handwritten
> specialization. But then I've been wrong before in assessing efficiency.
>
>
> Andrei
Could it be extended to accept multiple values? (sort of like chain)
eg.
foreach(x; makeRange(23, 7, 1990)) // NO allocations!
{
....
}
I would use this in a lot of places I currently jump through hoops to get a
static array without allocating.