How To: Passing curried functions around

Bahman Movaqar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 6 12:43:10 PDT 2015


On Sunday, 6 September 2015 at 19:22:41 UTC, welkam wrote:
> I dont know much about functional programming, but what stops 
> you defining
>
> int readInt(string prompt, validator_t validator) { ... }
>
> as a free standing function and just call it from both parts of 
> your code? What is the benefit of indirection that you create 
> when passing function pointer?

Nothing specific to FP here. I simply do not want to share the 
details of reading an integer with part B.

Imagine the same thing done with classes:

     // part A
     //
     class IntReader {
        string prompt;
        validator_t validator;

        int read() const { ... }
     }

     // part B
     //
     auto ir = new IntReader(...);
     foo(ir);

This way, all `foo` needs to know is calling `read` to get an 
integer. I'm trying to achieve the same thing here with 
functions, if possible.


More information about the Digitalmars-d-learn mailing list