Boost logo

Boost :

Subject: [boost] [Curry] generic and currying multimethod class
From: Nowake (nowake_at_[hidden])
Date: 2009-03-08 07:58:56


I implement curried function library and upload to Vault.

- This function object create the object that imitates curried function.
  you can call target function in incremental steps.
  # but you must regist each argument sets by hand.

- This function is generic function.
  you can set different function object to each argument sets.

- This function can also use multimethod.
  # but you must use holder (similar as boost::any) to hold argument.
  # this class act dynamic visitor for holder.

- This function object is poor performance.
  it takes much time to execute function.


// target function object
struct test_function {
  string operator()() {
     return "operator()()";
  string operator()(int& a) {
     return "operator()(int:" + lexical_cast<string>(a) + ")";
  string operator()(int& a, double& b) {
     return "operator()(int:" + lexical_cast<string>(a)
        + ", double:" + lexical_cast<string>(b) + ")";
  string operator()(const int& a) {
     return "operator()(const int:" + lexical_cast<string>(a) + ")";
  string operator()(const int& a, const double& b) {
     return "operator()(const int:" + lexical_cast<string>(a)
        + ", const double:" + lexical_cast<string>(b) + ")";

  // setup curry

  // create string return curry
  boost::curry<string> m00;
  // "entry" function to each argument sets.
  // nullary function

  // unary function

  // binary function
  m00.entry<int, double>(test_function());
  // you must "entry" each const type if you use const arguments.
  m00.entry<const int>(test_function());
  m00.entry<const int, const double>(test_function());

  // execute

  // incremental execution
  BOOST_AUTO(c00, m00());
  BOOST_AUTO(c01, c00(100));
  BOOST_AUTO(c02, c01(100.0));
  BOOST_AUTO(r00, c02());
  BOOST_CHECK_EQUAL(r00, "operator()(const int:100, const double:100)");

  int i(100);
  double d(100.0);
  BOOST_CHECK_EQUAL(m00()(), "operator()()");
  BOOST_CHECK_EQUAL(m00()(i)(), "operator()(int:100)");
  BOOST_CHECK_EQUAL(m00()(i)(d)(), "operator()(int:100, double:100)");

  // const value
     "operator()(const int:100)");
     "operator()(const int:100, const double:100)");

  // with holder object (holder is similar to boost::any)
  boost::holder h0(100);
  boost::holder h1(100.0);
  BOOST_CHECK_EQUAL(m00()(h0)(), "operator()(int:100)");
  BOOST_CHECK_EQUAL(m00()(h0)(h1)(), "operator()(int:100, double:100)");



Boost list run by bdawes at, gregod at, cpdaniel at, john at