Chaining a dynamic number of Ranges
Jonathan M Davis
jmdavisProg at gmx.com
Sat Jul 21 13:18:15 PDT 2012
On Saturday, July 21, 2012 21:47:19 Enerqi wrote:
> Thanks! That does join up the arrays as I'd like.
> An issue remaining is that, unlike with the chain function, I
> can't sort the output of the joiner function.
>
> Error: template instance std.algorithm.sort!("a <
> b",cast(SwapStrategy)0,Result) error instantiating
>
> Seems the return type of joiner doesn't implement random access
> in the same way the return type of chain does.
> Chain's documentation says "If all input ranges offer random
> access and $(D
> length), $(D Chain) offers them as well."
>
> I wonder if joiner is sortable somehow?
For sort to work, it needs a random access range. Operating on anything else
would be horribly inefficient. With the overload of joiner which doesn't take a
seperator, it might be possible to make joiner return a random access range if
all of the ranges passed to it were random access and had length, but it's not
implemented that way right now, and certainly if any of the ranges passed in
weren't random access, then that wouldn't work regardless. And it would never
work with the separator, since the separator would be in the range multiple
times, and sorting it could really mess it up.
The solution is to create an array and sort that. You can either use
std.array.array on the result of joiner, or - since I believe that you're
operating on arrays specifically - you can use std.array.join which will just
create an array immediately rather than generating a lazy range.
- Jonathan M Daves
More information about the Digitalmars-d-learn
mailing list