Passing a function (with arguments) as function input

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Sun Apr 22 16:19:41 PDT 2012


Is there a way in which to pass a function as input to another function, with 
the arguments of the first function already determined?

The case I'm thinking of is one where I have a function which wants to take a 
random number generation scheme, and use it on several occasions, without having 
any info on that scheme or its parameters.

Here's a little test attempt I made:

////////////////////////////////////////////////////
import std.random, std.range, std.stdio;

void printRandomNumbers(RandomNumberGenerator)(RandomNumberGenerator rng, size_t n)
{
       foreach(i; 0..n)
             writeln(rng);
}

void main()
{
       foreach(double upper; iota(1.0, 2.0, 0.2) ) {
             double delegate() rng = () {
                   return uniform(0.0, 1.0);
             };

             printRandomNumbers(rng,10);
       }
}
////////////////////////////////////////////////////

... which just prints out: "double delegate()" over many lines.

What am I doing wrong here?  And is there any way to avoid the messy business of 
defining a delegate and just hand over uniform(0.0, 1.0) ... ?


More information about the Digitalmars-d-learn mailing list