Why is std.algorithm so complicated to use?

Jacob Carlborg doob at me.com
Tue Jul 10 04:37:20 PDT 2012


On 2012-07-10 12:05, Dmitry Olshansky wrote:

> and what type has the return of map ? Let me guess - array.

Yes, and that is what I _want_. I have no need for streaming data from 
the network into a linked list, filter it and then convert it to an 
array (or something similar). I want to start with an array and end with 
an array.

> Please count the number of allocations in your paste of Ruby.

Probably four (if you count the initial allocation for the array 
literal). Plus a couple for the "to_s" method.

But I can use the same methods and modify the array in place instead:

a = [5, 3, 5, 6, 8]
a.uniq!
a.map!{ |e| e.to_s }
a.sort!
p a

Prints:

["3", "5", "6", "8"]

The corresponding D version would be:

auto a = [5, 3, 5, 6, 8].uniq.map!(x => x.to!(string)).array.sort.array;
writeln(a);

I'm guessing that's three allocations. But that doesn't even work, it 
prints:

["3", "5", "5", "6", "8"]

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list