Why is std.algorithm so complicated to use?

Jonathan M Davis jmdavisProg at gmx.com
Tue Jul 10 08:41:12 PDT 2012


On Tuesday, July 10, 2012 15:21:14 Christophe Travert wrote:
> "Simen Kjaeraas" , dans le message (digitalmars.D:171678), a écrit :
> >> Well, I haven't been able to use a single function from std.algorithm
> >> without adding a lot of calls to "array" or "to!(string)". I think the
> >> things I'm trying to do seems trivial and quite common. I'm I overrating
> >> std.algorithm or does it not fit my needs?
> > 
> > bearophile (who else? :p) has suggested the addition of eager and in-place
> > versions of some ranges, and I think he has a very good point.
> 
> That would have been useful before UFSC.
> Now, writing .array() at the end of an algorithm call is not a pain.
> 
> int[] = [1, 2, 2, 3].uniq().map!toString().array();

It's not like

auto result = array(map!"to!string(a)"(uniq([1, 2, 2, 3])));

is a pain either. It's just ordered differently.

But regardless, it's easy to get an eager result by calling array on a lazy 
range, so there's no point in adding eager versions. They'd just be 
duplicating code for no real benefit. Not to mention, if anyone having to call 
array on ranges all of the time, you should probably rethink how they're using 
ranges. It's definitely necessary some of the time, but most of the time, you 
can just operate on the lazy ranges and save yourself unnecessary allocations.

- Jonathan M Davis


More information about the Digitalmars-d mailing list