www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reduce a BitArray[]

reply Alex <sascha.orlov gmail.com> writes:
Hi there,
a silly question from me for the turn of the year... I apparently 
missing the forest through the trees.
The first part of the code works as expected:

[code]
     int[] arr8 = [1,2,3,4,5];

     int sum = 0;
     foreach(int summand; arr8)
     {
     	sum += summand;
     }
     writeln("sum1: ", sum);

     writeln("sum2: ", reduce!((a,b) => a + b)(arr8));
[/code]

The second one does not:

[code]
     import std.bitmanip;

     BitArray[] arr7 = [BitArray([0, 1, 0, 1, 0, 1]), BitArray([0, 
1, 0, 0, 0, 0]), BitArray([0, 1, 0, 1, 0, 0]), BitArray([0, 1, 0, 
1, 0, 0])];

     BitArray common = BitArray([1,1,1,1,1,1]);
     foreach(BitArray ba; arr7)
     {
         common &=  ba;
     }
     writeln("common2: ", common);

     //writeln("common1: ", reduce!((a,b) => a & b)(arr7));
[/code]

The problem is, that the last line with the reduce does not 
compile. Why?
Dec 29 2015
parent reply Alex Parrill <initrd.gz gmail.com> writes:
On Tuesday, 29 December 2015 at 09:26:31 UTC, Alex wrote:
 The problem is, that the last line with the reduce does not 
 compile. Why?
If you get an error, it is imperative that you tell us what it is. For the record, this code: import std.bitmanip; import std.stdio; import std.algorithm; void main() { BitArray[] arr7 = [BitArray([0, 1, 0, 1, 0, 1]), BitArray([0, 1, 0, 0, 0, 0]), BitArray([0, 1, 0, 1, 0, 0]), BitArray([0, 1, 0, 1, 0, 0])]; BitArray common = BitArray([1,1,1,1,1,1]); foreach(BitArray ba; arr7) { common &= ba; } writeln("common2: ", common); writeln("common1: ", reduce!((a,b) => a & b)(arr7)); } Gives me the error: /opt/compilers/dmd2/include/std/algorithm/iteration.d(2570): Error: variable f868.main.F!(__lambda1).e cannot be declared to be a function /opt/compilers/dmd2/include/std/meta.d(546): Error: template instance f868.main.F!(__lambda1) error instantiating /opt/compilers/dmd2/include/std/algorithm/iteration.d(2477): instantiated from here: staticMap!(ReduceSeedType, __lambda1) /d486/f868.d(16): instantiated from here: reduce!(BitArray[]) /d486/f868.d(16): Error: template std.algorithm.iteration.reduce cannot deduce function from argument types !((a, b) => a & b)(BitArray[]), candidates are: /opt/compilers/dmd2/include/std/algorithm/iteration.d(2451): std.algorithm.iteration.reduce(fun...) if (fun.length >= 1) On 2.069.1 (dpaste: http://dpaste.dzfl.pl/8d7652e7ebeb). I don't have time ATM to diagnose it further. It definitely shouldn't be that cryptic of an error message.
Dec 29 2015
parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
V Tue, 29 Dec 2015 17:42:26 +0000
Alex Parrill via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> napsáno:

 On Tuesday, 29 December 2015 at 09:26:31 UTC, Alex wrote:
 The problem is, that the last line with the reduce does not 
 compile. Why?  
If you get an error, it is imperative that you tell us what it is. For the record, this code: import std.bitmanip; import std.stdio; import std.algorithm; void main() { BitArray[] arr7 = [BitArray([0, 1, 0, 1, 0, 1]), BitArray([0, 1, 0, 0, 0, 0]), BitArray([0, 1, 0, 1, 0, 0]), BitArray([0, 1, 0, 1, 0, 0])]; BitArray common = BitArray([1,1,1,1,1,1]); foreach(BitArray ba; arr7) { common &= ba; } writeln("common2: ", common); writeln("common1: ", reduce!((a,b) => a & b)(arr7)); } Gives me the error: /opt/compilers/dmd2/include/std/algorithm/iteration.d(2570): Error: variable f868.main.F!(__lambda1).e cannot be declared to be a function /opt/compilers/dmd2/include/std/meta.d(546): Error: template instance f868.main.F!(__lambda1) error instantiating /opt/compilers/dmd2/include/std/algorithm/iteration.d(2477): instantiated from here: staticMap!(ReduceSeedType, __lambda1) /d486/f868.d(16): instantiated from here: reduce!(BitArray[]) /d486/f868.d(16): Error: template std.algorithm.iteration.reduce cannot deduce function from argument types !((a, b) => a & b)(BitArray[]), candidates are: /opt/compilers/dmd2/include/std/algorithm/iteration.d(2451): std.algorithm.iteration.reduce(fun...) if (fun.length >= 1) On 2.069.1 (dpaste: http://dpaste.dzfl.pl/8d7652e7ebeb). I don't have time ATM to diagnose it further. It definitely shouldn't be that cryptic of an error message.
It is cause by this https://dlang.org/phobos/std_bitmanip.html#.BitArray.init So until it will be removed it will does not compile. Problem is in https://dlang.org/phobos/std_range_primitives.html#.ElementType which is implementet as template ElementType(R) { static if (is(typeof(R.init.front.init) T)) alias ElementType = T; else alias ElementType = void; } maybe rewriting this like: template ElementType(R) { static if (is(typeof(R.init.front) T)) alias ElementType = T; else alias ElementType = void; } could help too
Dec 29 2015