phobos and splitting things... but not with whitespace.

Chad J chadjoan at __spam.is.bad__gmail.com
Sat Jun 23 08:39:55 PDT 2012


On 06/23/2012 11:31 AM, simendsjo wrote:
> On Sat, 23 Jun 2012 17:19:59 +0200, Chad J
> <chadjoan at __spam.is.bad__gmail.com> wrote:
>> http://dlang.org/phobos/std_array.html#splitter
>
>> The first thing I don't understand is why splitter is in /std.array/
>> and yet only works on /strings/. It is defined > in terms of
>> whitespace, and I don't understand how whitespace is well-defined for
>> things besides text. Why wouldn't > it be in std.string?
>
> See http://dlang.org/phobos/std_algorithm.html#splitter
>
>> I would expect these functions to exist:
>> auto getHead(C)(C[] s, C[] delim, ref C[] tail);
>> auto getHead(C)(C[] s, C[] delim);
>> auto getTail(C)(C[] s, C[] delim);
>
> As head is simply splitter(..)[0] and tail splitter(...)[1..$], extra
> functions could be implemented much like this
>
> @property T head(T[] arr) { return arr.front; }
> @property T[] tail(T[] arr) { return arr[1..$]; }
>
> ..and UFCS takes care of the rest:
> auto fields = splitter(...);
> auto head = fields.head;
> auto tail = fields.tail;

But I don't want tail as an array.  Assume that arr is HUGE and scanning 
the rest of it is a bad idea.  join(arr[1..$]) then becomes a slow 
operation: O(n) when I could have O(1).


More information about the Digitalmars-d-learn mailing list