Function tuples

bearophile bearophileHUGS at lycos.com
Fri Mar 14 09:37:59 PDT 2014


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


More information about the Digitalmars-d-learn mailing list