March 31, 2014 Re: "fold": a replacement for "reduce" | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 31 March 2014 at 13:05:24 UTC, bearophile wrote:
> OK, thank you. So the price for that "in" is to use a dup :-) (Or use lower level code).
>
> Bye,
> bearophile
I'd think so yes. But given you are calling "array" for every iteration, it doesn't look like a ludicrous overhead.
That said, if you were reduce *into* your actual seed (the dup'ed array) instead of duplicating it on every iteration, it should be better.
I think your code is simplified, but consider this:
//----
uint[] foo3(const(uint[])[] X)
{
assert(!X.empty);
auto seed = X.front.dup;
X.popFront();
return reduce!((i, j) => i[] |= j[])
(seed, X);
}
//----
This gives the same "logical" result, but reuses the seed contents on every iteration.
Then, the original dup looks less gratuitous: You are allocating your result.
|
March 31, 2014 Re: "fold": a replacement for "reduce" | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | monarch_dodra: > uint[] foo3(const(uint[])[] X) > { > assert(!X.empty); > auto seed = X.front.dup; > X.popFront(); > return reduce!((i, j) => i[] |= j[]) > (seed, X); > } > //---- > > This gives the same "logical" result, but reuses the seed contents on every iteration. Nice, thank you :-) And with a miniscule de-optimization it becomes very good: https://d.puremagic.com/issues/show_bug.cgi?id=10523 Bye, bearophile |
Copyright © 1999-2021 by the D Language Foundation