Why is std.algorithm so complicated to use?
Jacob Carlborg
doob at me.com
Tue Jul 10 02:30:55 PDT 2012
On 2012-07-10 09:04, Jonathan M Davis wrote:
> The problem is that map is lazy, so it can't be a random access range, and
> sort requires a random access range. If map were eager, it would just be
> creating an array anyway. But you don't need to convert it to an array again
> after sorting it. sort returns a SortedRange so that functions such as find can
> know that it's sorted and take advantage of it, but it sorts in place
> (SortedRange is just a wrapper around the range which was passed in and copies
> no data), so once you've called sort on your array, it's sorted. You can just
> ignore the return type if you're not looking to pass it to a function which
> would take advantage of the fact that it's sorted. But since SortedRange
> _isn't_ lazy (it's just a wrapper around the newly sorted original range,
> after all), it's still a random access range and will work with functions
> which require that (unlike map).
>
> You only end up with a range with fewer capabilities than the original when
> the algorithm itself intrinsicly requires it, and that sort of range is
> generally lazy (since it's more efficient that way, and making it non-lazy would
> be equivalent to wrapping it in a call to array anyway).
So it's basically what I said. Sine I want an array as the result of the
operation I do need to convert it to an array again. For my need, it
just seem to be more trouble to use std.algorithm.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list