www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Trying Multiple Aggregate reduce

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Mar 31 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Mar 31 2014
prev sibling parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 You can't use reduce with a const seed.
This, surely, must be a compiler bug right? /Per
Mar 31 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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
Apr 01 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 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
parent "monarch_dodra" <monarchdodra gmail.com> writes:
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.
Apr 02 2014