reduce a BitArray[]

Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 29 10:30:41 PST 2015


V Tue, 29 Dec 2015 17:42:26 +0000
Alex Parrill via Digitalmars-d-learn
<digitalmars-d-learn at 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






More information about the Digitalmars-d-learn mailing list