www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8224] New: Problem with std.functional.unaryFun

http://d.puremagic.com/issues/show_bug.cgi?id=8224

           Summary: Problem with std.functional.unaryFun
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



import std.stdio: writefln;
import std.math: sin, asin, cos, acos, cbrt, pow;
import std.functional: compose;
import std.typetuple: TypeTuple;
void main() {
    const sin  = (in real x) => sin(x);
    const asin = (in real x) => asin(x);
    const cos  = (in real x) => cos(x);
    const acos = (in real x) => acos(x);
    const cube = (in real x) => x ^^ 3;
    const cbrt = (in real x) => cbrt(x);
    alias TypeTuple!(sin,  cos,  cube) dir;
    alias TypeTuple!(asin, acos, cbrt) inv;
    foreach (i, f; dir)
        writefln("%6.3f", compose!(f, inv[i])(0.5));
}


Output, dmd 2.060alpha:
 0.500
 0.866
 0.713

Expected output similar to:
 0.500
 0.500
 0.500



Including the source code of std.functional.compose and simplifying it, it
shows the problem still:

import std.functional: unaryFun;
template compose2(fun...) {
    alias unaryFun!(fun[0]) fun0;
    alias unaryFun!(fun[1]) fun1;
    auto compose2(T)(T a) {
        return fun0(fun1(a));
    }
}
void main() {
    import std.stdio, std.math, std.typetuple;
    const sin  = (in real x) => sin(x);
    const asin = (in real x) => asin(x);
    const cos  = (in real x) => cos(x);
    const acos = (in real x) => acos(x);
    const cube = (in real x) => x ^^ 3;
    const cbrt = (in real x) => cbrt(x);
    alias TypeTuple!(sin,  cos,  cube) dir;
    alias TypeTuple!(asin, acos, cbrt) inv;
    foreach (i, f; dir)
        writefln("%6.3f", compose2!(f, inv[i])(0.5));
}


Output, dmd 2.060alpha:
 0.500
 0.866
 0.713



Removing the unaryFun!() the results become correct:


import std.functional: unaryFun;
template compose2(fun...) {
    alias fun[0] fun0;
    alias fun[1] fun1;
    auto compose2(T)(T a) {
        return fun0(fun1(a));
    }
}
void main() {
    import std.stdio, std.math, std.typetuple;
    const sin  = (in real x) => sin(x);
    const asin = (in real x) => asin(x);
    const cos  = (in real x) => cos(x);
    const acos = (in real x) => acos(x);
    const cube = (in real x) => x ^^ 3;
    const cbrt = (in real x) => cbrt(x);
    alias TypeTuple!(sin,  cos,  cube) dir;
    alias TypeTuple!(asin, acos, cbrt) inv;
    foreach (i, f; dir)
        writefln("%6.3f", compose2!(f, inv[i])(0.5));
}


Output, dmd 2.060alpha:
 0.500
 0.500
 0.500

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 11 2012