Brad Roberts wrote:
On Fri, 30 Jan 2009, Andrei Alexandrescu wrote:
Consider:
int[] a = [ 1, 2, 3, 4, 5];
foreach (e; a)
{
if (e == 3) break;
}
assert(a == [4, 5]);
Do you seriously expect that to be the case? Of course not. However, you would
inconsistently expect this to happen:
int[] a = [ 1, 2, 3, 4, 5];
foreach (e; take(4, a))
{
if (e == 3) break;
}
assert(a == [4, 5]);
Ranges are modeled to correspond to slices. Slices have few operations that
manipulate them by reference (notably the controversial ~=) and range are made
to be unsurprising when compared to slices. They are really extensions of
slices.
Andrei
I hate to come back to naming, but I think that's a major part of what's
causing grief here. Take implies mutation of the input range. How about
'subrange' or 'subset' or 'leftmost' anything that doesn't imply removal.
Later,
Brad
Names are important. "take" is taken from Haskell.
Andrei