Functoids In Cpp Experiment

This is an experiment to see how far FunctoidsInCpp can be taken to meet the needs discussed in ContinuationPassingStyleInCppQuadraticEquationExample. In particular, it demonstrates nested implementation of objects of type Fun0 which have no arguments and return type T. -- JohnFletcher


void message( )
{
std::cout << "message" << std::endl;
}
Fun0 messagefun()
{
return ptr_to_fun(&message);
}
Fun0 > messagefunfun()
{
return ptr_to_fun(&messagefun);
}
Fun0 > > messagefunfunfun()
{
return ptr_to_fun(&messagefunfun);
}
template
F callF(const Fun0& f)
{
return f();
}
template
F callFrec(const Fun0& f)
{
// I want to make this recursive but I need a test for
// when to stop. Try a specialisation.
return callFrec(f());
}
template <>
void callFrec(const Fun0& f)
{
return f();
}
template
F callFull(const Full0 >& f)
{
return f();
}
int experiments()
{
LambdaVar<1> F;
Fun0 messageF = ptr_to_fun(&message);
Fun0 > messageFF = ptr_to_fun(&messagefun);
Fun0 > > messageFFF = ptr_to_fun(&messagefunfun);
Fun0 > > > messageFFFF = ptr_to_fun(&messagefunfunfun);
messageF();
messageFF()();
messageFFF()()();
messageFFFF()()()();
std::cout << "----" << std::endl;
callF(messageF);
callF(callF(messageFF));
callF(callF(callF(messageFFF)));
callF(callF(callF(callF(messageFFFF))));
std::cout << "----" << std::endl;
callFrec(messageF);
// callFrec(messageFF); // won't compile
std::cout << "----" << std::endl;
Full0 > fullmessageF = makeFull0(messageF);
Full0 > > fullmessageFF = makeFull0(messageFF);
Full0 > > > fullmessageFFF = makeFull0(messageFFF);
Full0 > > > > fullmessageFFFF = makeFull0(messageFFFF);
fullmessageF();
fullmessageFF()();
fullmessageFFF()()();
fullmessageFFFF()()()();
std::cout << "----" << std::endl;
callFull(fullmessageF);
callF(callFull(fullmessageFF));
callF(callF(callFull(fullmessageFFF)));
callF(callF(callF(callFull(fullmessageFFFF))));
std::cout << "----" << std::endl;
lambda(F) [ F[_*_] ] (messageF);
lambda(F) [ F[_*_][_*_] ] (messageFF);
lambda(F) [ F[_*_][_*_][_*_] ] (messageFFF);
lambda(F) [ F[_*_][_*_][_*_][_*_] ] (messageFFFF);
std::cout << "----" << std::endl;
lambda(F) [ F[_*_] ] (fullmessageF);
lambda(F) [ F[_*_][_*_] ] (fullmessageFF);
lambda(F) [ F[_*_][_*_][_*_] ] (fullmessageFFF);
lambda(F) [ F[_*_][_*_][_*_][_*_] ] (fullmessageFFFF);
return 0;
}

Notes:

[_*_] is a fix from FC++ to call a functoid with no arguments, as [ ] fails.
The recursive call callFrec does not work. I need to test for the end condition.

CategoryCpp CategoryFunctionalProgramming