Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
March 31, 2014 Trying Multiple Aggregate reduce | ||||
---|---|---|---|---|
| ||||
I'm trying to figure out how to use reduce with multiply funs. Here's my try on minmaxElement: import std.typecons: tuple; /** Returns: Tuple of Minmum and Maximum Element in X. */ auto minmaxElement(alias F = min, alias G = max, R)(in R range) @safe pure nothrow if (isInputRange!R) { return reduce!(F, G)(tuple(ElementType!R.max, ElementType!R.min), range); } unittest { assert([1, 2, 3].minmaxElement == tuple(1, 3)); } which errors as /home/per/opt/x86_64-unknown-linux-gnu/dmd/bin/../include/d2/std/algorithm.d(774,29): Error: can only initialize const member _expand_field_0 inside constructor /home/per/opt/x86_64-unknown-linux-gnu/dmd/bin/../include/d2/std/algorithm.d(774,29): Error: can only initialize const member _expand_field_1 inside constructor algorithm_ex.d(157,25): Error: template instance std.algorithm.reduce!(min, max).reduce!(Tuple!(const(int), const(int)), const(int)[]) error instantiating algorithm_ex.d(160,28): instantiated from here: minmaxElement!(min, max, const(int)[]) If I replace ElementType!R.min and ElementType!R.max with a values, say 0 and 1 it compiles. What is going on here? This seems related: http://forum.dlang.org/thread/bug-10408-3@http.d.puremagic.com/issues/ |
March 31, 2014 Re: Trying Multiple Aggregate reduce | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw: > I'm trying to figure out how to use reduce with multiply funs. import std.algorithm: min, max, reduce; import std.typecons: tuple, Unqual; import std.range: isInputRange, ElementType; /// Returns: Tuple of Minmum and Maximum Element in X. auto minMax(alias F = min, alias G = max, R)(in R range) @safe pure nothrow if (isInputRange!R) { return reduce!(F, G)(tuple(Unqual!(ElementType!R).max, Unqual!(ElementType!R).min), range); } unittest { assert([1, 2, 3].minMax == tuple(1, 3)); } void main() {} > If I replace ElementType!R.min and ElementType!R.max with a values, say 0 and 1 it compiles. What is going on here? > > This seems related: > http://forum.dlang.org/thread/bug-10408-3@http.d.puremagic.com/issues/ You can't use reduce with a const seed. Bye, bearophile |
March 31, 2014 Re: Trying Multiple Aggregate reduce | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 03/31/2014 03:13 PM, bearophile wrote: > Nordlöw: > >> I'm trying to figure out how to use reduce with multiply funs. Could someone please check the following. It looks like a compiler bug on git head (DMD64 D Compiler v2.066-devel-75159e4): import std.algorithm; int foo(int value) { return value; } void main() { reduce!(foo, foo)(tuple(0, 0), [ 1 ]); } statement.c:274: ErrorStatement::ErrorStatement(): Assertion `global.gaggedErrors || global.errors' failed. Ali |
March 31, 2014 Re: Trying Multiple Aggregate reduce | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > You can't use reduce with a const seed.
>
This, surely, must be a compiler bug right?
/Per
|
March 31, 2014 Re: Trying Multiple Aggregate reduce | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Ali Çehreli:
> statement.c:274: ErrorStatement::ErrorStatement(): Assertion `global.gaggedErrors || global.errors' failed.
It's a little compiler bug, that should go in Bugzilla (even if perhaps it was recently added).
Bye,
bearophile
|
March 31, 2014 Re: Trying Multiple Aggregate reduce | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 03/31/2014 04:01 PM, bearophile wrote: > Ali Çehreli: > >> statement.c:274: ErrorStatement::ErrorStatement(): Assertion >> `global.gaggedErrors || global.errors' failed. > > It's a little compiler bug, that should go in Bugzilla (even if perhaps > it was recently added). https://d.puremagic.com/issues/show_bug.cgi?id=12501 Ali |
April 02, 2014 Re: Trying Multiple Aggregate reduce | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Monday, 31 March 2014 at 22:25:40 UTC, Nordlöw wrote: >> You can't use reduce with a const seed. >> > > This, surely, must be a compiler bug right? > > /Per Arguably, it's a user bug ;) The user should have provided a non-const seed. *But*, there have been many cases of reduce being made to accept const seeds before, via unqualified copy. This case must have just been a missed one. In any case, I had submitted a re-write for reduce, and it just so happens to support this. So I added your code to the test cases: https://github.com/D-Programming-Language/phobos/pull/2060 |
April 02, 2014 Re: Trying Multiple Aggregate reduce | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | > so happens to support this. So I added your code to the test cases:
Great!
BTW: Why is static qualifier needed on definition of minmaxElement() in the unittest?
|
April 02, 2014 Re: Trying Multiple Aggregate reduce | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Wednesday, 2 April 2014 at 09:25:53 UTC, Nordlöw wrote:
>> so happens to support this. So I added your code to the test cases:
>
> Great!
>
> BTW: Why is static qualifier needed on definition of minmaxElement() in the unittest?
Whenever you declare something in a nested context, it may or may not have a hidden context pointer, depending on the type, and the implementation.
"static", in this context, ensures this does not happen. It's not actually *needed* in this context though. It's more of a matter of style.
In your example, the function was declared in global context, so the "static" would have been gratuitous.
|
Copyright © 1999-2021 by the D Language Foundation