Ruby-style "each" in D?
monarch_dodra
monarchdodra at gmail.com
Wed Mar 19 23:33:19 PDT 2014
On Wednesday, 19 March 2014 at 23:00:08 UTC, Walter Bright wrote:
> I don't have a solid enough experience with this style of
> programming, but one opportunity of 'each' could be that it
> could be specialized and achieve greater speed for some
> arguments.
Speaking of which, it would be nice to have a compiler default
iteration scheme that is optimal for any range.
The current status is:
"for ( ; !r.empty ; r.popFront )" is sub-optimal for string
types, and sometimes arrays.
alias E = ElementEncodingType!Range;
"foreach(E e; range);" will create copies of elements
"foreach(ref E e; range);" will always work, but is arguably
wrong for RValue ranges
"foreach(auto ref E e; range);" is not legal, but there is an ER
requesting it, and I think it would make perfect sense to have
this.
Finally: The hand written library code
while ( decode(s, i) < s.length )
{ ... }
Is faster for strings, than the compiler's/druntime's "foreach(E
e; range);".
It would be awesome if:
foreach(auto ref E e; range);
Worked, *and* was always optimal.
The current scheme makes those who care jump through hoops, such
as in `find`'s implementation.
More information about the Digitalmars-d
mailing list