How to append range to array?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 23 11:23:12 PDT 2015


On Saturday, May 23, 2015 08:36:47 weaselcat via Digitalmars-d-learn wrote:
> On Saturday, 23 May 2015 at 08:35:45 UTC, weaselcat wrote:
> > On Saturday, 23 May 2015 at 07:03:35 UTC, Vladimir Panteleev
> > wrote:
> >> int[] arr = [1, 2, 3];
> >> auto r = iota(4, 10);
> >> // ???
> >> assert(equal(arr, iota(1, 10)));
> >
> > import std.array : array;
> > arr ~ r.array;
>
> woops, meant ~=
>
> but this is probably fairly inefficient. Working with ranges and
> arrays at the same time feels really badly designed.

It's fine if you're not interchanging them. It sounds like he probably wants
to append to an existing array after some set of range operations are done.
And that's not really any different from converting a range to an array via
std.array.array except that the result ends up on the end of an existing
array. The problem is that the output range API doesn't support that,
because it treats arrays as buffers to be filled rather than appending to
them. So, you probably either end up having to use Appender instead of a
naked array, or you have to use foreach and append manually (or you could
use std.array.array. and append the result, but I'd be surprised if that
weren't less efficient).

The problem is when you're trying to do range-based operations and throw
array-specific operations in the middle of it. _That_ is what needs to be
avoided.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list