www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - problem with reduce on array of Tuples

reply Adrian Matoga <epi student.agh.edu.pl> writes:
The following code fails to compile:

    double sim(Document doc, string query)
    {
        alias Tuple!(double, "wij", double, "wiq") Weights;
        Document q = Document.fromString(query);
        Weights[] wi;
        foreach (s; StrFilt(query))
            wi ~= Weights(doc.termFreq(s) * invDocFreq(s), q.termFreq
(s) * invDocFreq(s));
        return
            reduce!((double acc, Weights w) { return acc + 0; /*w.wij
* w.wiq;*/ })(0.0, wi) /
            (sqrt(reduce!((double acc, Weights w) { return acc + 0; /
*sqr(w.wij);*/ })(0.0, wi) *
             sqrt(reduce!((double acc, Weights w) { return acc + 0; /
*sqr(w.wiq);*/ }(0.0, wi)))));
    }

and DMD 2.046 shows the following messages:
ir.d(119): Error: delegate std.algorithm.__dgliteral3 cannot access
frame of fun
ction __dgliteral3
ir.d(232): Error: template instance ir.Index.sim.Reduce!(delegate
double(double
acc, Tuple!(double,"wij",double,"wiq") w)
{
return acc + 0;
}
) error instantiating
ir.d(119):        instantiated from here: reduce!(delegate double
(double acc, Tu
ple!(double,"wij",double,"wiq") w)
{
return acc + 0;
}
)


Is it my fault because of some misunderstanding or other error, or
should I report it as a bug in compiler/library?
The same function compiled well with reduce on StrFilt(query) instead
of Weights.

I attach the full source.
begin 644 ir.d
M:6UP;W)T('-T9"YS=&1I;SL-"FEM<&]R="!S=&0N<W1R:6YG.PT*:6UP;W)T
M('-T9"YC;VYV.PT*:6UP;W)T('-T9"YF:6QE.PT*:6UP;W)T('-T9"YM871H
M.PT*:6UP;W)T('-T9"YA;&=O<FET:&T[#0II;7!O<G0 <W1D+G1Y<&5C;VYS
M.PT*#0I4('-Q<BA4*2A4(' I#0I[#0H)<F5T=7)N('  *B!X.PT*?0T*#0IC
M;&%S<R!3=')&:6QT#0I[#0H)<W1A=&EC(&)O;VQ;<W1R:6YG72!P<F5P<SL-
M" T*"7-T871I8R!T:&ES*"D-" E[#0H)"69O<F5A8V  *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EW<FET96QN*&1E+FYA;64I.R`-" D):6YD97 N861D*$1O8W5M96YT
M+F9R;VU&:6QE*&1E+FYA;64I*3L-" D)<F5T=7)N('1R=64[#0H)?0DI.PT*
M"0T*"69O<F5A8V  *&8[("A3=')&:6QT*&%R9W-;,5TI*2D >R!W<FET96QN
M*&8I.R!]#0H)9F]R96%C:"`H:RP =CL :6YD97 N<F5T<FEE=F4H87)G<ULQ
M72DI#0H)>PT*"0EW<FET969L;B B)7, +3X )7,B+"!K+"!V*3L-" E]#0H)
,<F5T=7)N(#`[#0I]
`
end
May 27 2010
parent Pelle <pelle.mansson gmail.com> writes:
This has nothing to do with the tuples, I think.

test2.d:

import std.algorithm;

void main() {
     map!((int x){return x+1;})([1,2,3,4,5]);
}

pp ~/dee% rdmd test2.d
test2.d(4): Error: delegate std.algorithm.__dgliteral1 cannot access 
frame of function __dgliteral1
test2.d(4): Error: template instance test2.main.map!(delegate int(int x)
{
return x + 1;
}
) error instantiating


It's a bug with the delegate literals inside the template.
May 27 2010