Why is std.algorithm so complicated to use?
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Tue Jul 10 08:39:23 PDT 2012
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
More information about the Digitalmars-d
mailing list