www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function tuples

reply "bearophile" <bearophileHUGS lycos.com> writes:
This comes from a Rosettacode entry:
http://rosettacode.org/wiki/First-class_functions#D


import std.stdio, std.math, std.typetuple, std.functional;

enum static sin  = (in real x) pure nothrow => std.math.sin(x),
             asin = (in real x) pure nothrow => std.math.asin(x),
             cos  = (in real x) pure nothrow => std.math.cos(x),
             acos = (in real x) pure nothrow => std.math.acos(x),
             cube = (in real x) pure nothrow => x ^^ 3,
             cbrt = (in real x) /*pure*/ nothrow => 
std.math.cbrt(x);

void main() {
     alias dir = TypeTuple!(sin,  cos,  cube);
     alias inv = TypeTuple!(asin, acos, cbrt);
     foreach (immutable i, f; dir) {
         writefln("%6.3f %6.3f",
                  compose!(f, inv[i])(0.5),
                  compose!(dir[i], inv[i])(0.5));
     }
}

It prints:

  0.500  0.500
  0.866  0.500
  0.713  0.500

Do you know why there's such difference in the results?

(You can't define a staticZip in D?)

Bye,
bearophile
Mar 14 2014
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/14/2014 09:37 AM, bearophile wrote:
 This comes from a Rosettacode entry:
 http://rosettacode.org/wiki/First-class_functions#D


 import std.stdio, std.math, std.typetuple, std.functional;

 enum static sin  = (in real x) pure nothrow => std.math.sin(x),
              asin = (in real x) pure nothrow => std.math.asin(x),
              cos  = (in real x) pure nothrow => std.math.cos(x),
              acos = (in real x) pure nothrow => std.math.acos(x),
              cube = (in real x) pure nothrow => x ^^ 3,
              cbrt = (in real x) /*pure*/ nothrow => std.math.cbrt(x);

 void main() {
      alias dir = TypeTuple!(sin,  cos,  cube);
      alias inv = TypeTuple!(asin, acos, cbrt);
      foreach (immutable i, f; dir) {
          writefln("%6.3f %6.3f",
                   compose!(f, inv[i])(0.5),
                   compose!(dir[i], inv[i])(0.5));
      }
 }

 It prints:

   0.500  0.500
   0.866  0.500
   0.713  0.500
DMD64 D Compiler v2.066-devel-1a70764 prints the following for me: 0.500 0.500 0.500 0.500 0.500 0.500 Maybe a bug fixed? (Or a bug introduced if you are using a more recent one. :) )
 Do you know why there's such difference in the results?

 (You can't define a staticZip in D?)

 Bye,
 bearophile
Ali
Mar 14 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Ali Çehreli:

 DMD64 D Compiler v2.066-devel-1a70764 prints the following for 
 me:

  0.500  0.500
  0.500  0.500
  0.500  0.500

 Maybe a bug fixed?
Probably yes, thank for all the answers :-) Bye, bearophile
Mar 14 2014
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 14 March 2014 at 16:38:00 UTC, bearophile wrote:
 This comes from a Rosettacode entry:
 http://rosettacode.org/wiki/First-class_functions#D


 import std.stdio, std.math, std.typetuple, std.functional;

 enum static sin  = (in real x) pure nothrow => std.math.sin(x),
             asin = (in real x) pure nothrow => std.math.asin(x),
             cos  = (in real x) pure nothrow => std.math.cos(x),
             acos = (in real x) pure nothrow => std.math.acos(x),
             cube = (in real x) pure nothrow => x ^^ 3,
             cbrt = (in real x) /*pure*/ nothrow => 
 std.math.cbrt(x);

 void main() {
     alias dir = TypeTuple!(sin,  cos,  cube);
     alias inv = TypeTuple!(asin, acos, cbrt);
     foreach (immutable i, f; dir) {
         writefln("%6.3f %6.3f",
                  compose!(f, inv[i])(0.5),
                  compose!(dir[i], inv[i])(0.5));
     }
 }

 It prints:

  0.500  0.500
  0.866  0.500
  0.713  0.500

 Do you know why there's such difference in the results?
It's a bug, and quite a wierd one at that. See the output from this: http://dpaste.dzfl.pl/1962d61bb185
 (You can't define a staticZip in D?)
You can. I hope it will end up in in std.meta, along with a whole load of other such goodies that I've been working on :)
Mar 14 2014
next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 14 March 2014 at 17:10:53 UTC, John Colvin wrote:
 On Friday, 14 March 2014 at 16:38:00 UTC, bearophile wrote:
 This comes from a Rosettacode entry:
 http://rosettacode.org/wiki/First-class_functions#D


 import std.stdio, std.math, std.typetuple, std.functional;

 enum static sin  = (in real x) pure nothrow => std.math.sin(x),
            asin = (in real x) pure nothrow => std.math.asin(x),
            cos  = (in real x) pure nothrow => std.math.cos(x),
            acos = (in real x) pure nothrow => std.math.acos(x),
            cube = (in real x) pure nothrow => x ^^ 3,
            cbrt = (in real x) /*pure*/ nothrow => 
 std.math.cbrt(x);

 void main() {
    alias dir = TypeTuple!(sin,  cos,  cube);
    alias inv = TypeTuple!(asin, acos, cbrt);
    foreach (immutable i, f; dir) {
        writefln("%6.3f %6.3f",
                 compose!(f, inv[i])(0.5),
                 compose!(dir[i], inv[i])(0.5));
    }
 }

 It prints:

 0.500  0.500
 0.866  0.500
 0.713  0.500

 Do you know why there's such difference in the results?
It's a bug, and quite a wierd one at that. See the output from this: http://dpaste.dzfl.pl/1962d61bb185
It's calling sin every time instead of cos or cube
Mar 14 2014
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
John Colvin:

 (You can't define a staticZip in D?)
You can. I hope it will end up in in std.meta, along with a whole load of other such goodies that I've been working on :)
Is the code for staticZip somewhere online? Bye, bearophile
Mar 14 2014