digitalmars.D.learn - Trying Multiple Aggregate reduce
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (27/27) Mar 31 2014 I'm trying to figure out how to use reduce with multiply funs.
- bearophile (18/23) Mar 31 2014 import std.algorithm: min, max, reduce;
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (15/17) Mar 31 2014 Could someone please check the following. It looks like a compiler bug
- bearophile (5/7) Mar 31 2014 It's a little compiler bug, that should go in Bugzilla (even if
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/8) Mar 31 2014 https://d.puremagic.com/issues/show_bug.cgi?id=12501
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (2/3) Mar 31 2014 This, surely, must be a compiler bug right?
- monarch_dodra (10/14) Apr 01 2014 Arguably, it's a user bug ;) The user should have provided a
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (3/5) Apr 02 2014 Great!
- monarch_dodra (9/14) Apr 02 2014 Whenever you declare something in a nested context, it may or may
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/ lgorithm.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/ lgorithm.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/
Mar 31 2014
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
Mar 31 2014
On 03/31/2014 03:13 PM, bearophile wrote:Nordlöw: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. AliI'm trying to figure out how to use reduce with multiply funs.
Mar 31 2014
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
Mar 31 2014
On 03/31/2014 04:01 PM, bearophile wrote:Ali Çehreli:https://d.puremagic.com/issues/show_bug.cgi?id=12501 Alistatement.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).
Mar 31 2014
You can't use reduce with a const seed.This, surely, must be a compiler bug right? /Per
Mar 31 2014
On Monday, 31 March 2014 at 22:25:40 UTC, Nordlöw wrote: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/2060You can't use reduce with a const seed.This, surely, must be a compiler bug right? /Per
Apr 01 2014
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?
Apr 02 2014
On Wednesday, 2 April 2014 at 09:25:53 UTC, Nordlöw wrote: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.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?
Apr 02 2014