On 2012-07-10 14:53, Dmitry Olshansky wrote:

Too bad, as there is no need to make an array when you map something.

How do you store your ranges in a struct or class? Most of them are voldemort types.

Then you need something like transform. Anyway the result of map should
be sortable it's a bug.

Thank you for clearing that up.

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.


Now scale the problem to at least ~ 10^6 ...

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;



The last array is unnecessary as sort is in-place.

Again, I want an array, not a range.

Also why would you not sort before map? It'd be faster this way as it's
sorting integers.

Isn't it obvious that is just an example and not real code. I'm trying to keep the code as short as possible here.

Thus it's only 2 and one of them is literal. The map can't be sorted
because uniq produces lazy bidirectional range. Thus you turn it into
array as simple as that.

My version would be:

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

fixed?

Still need an array. Real code:

https://github.com/jacob-carlborg/dstep/blob/master/dstep/translator/IncludeHandler.d#L124

I want the end result to be sorted.

Because uniq work only on sorted ranges? Have you tried reading docs?
"
Iterates unique consecutive elements of the given range (functionality
akin to the uniq system utility). Equivalence of elements is assessed by
using the predicate pred, by default "a == b". If the given range is
bidirectional, uniq also yields a bidirectional range.
"
Though it doesn't explicitly mentions it, the example is:

Yes, exactly.

int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ];
assert(equal(uniq(arr), [ 1, 2, 3, 4, 5 ][]));

How should I know that from the example?

--
/Jacob Carlborg

Reply via email to