Boost logo

Boost :

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


Hi,

I implement curried function library and upload to Vault.
http://www.boostpro.com/vault/index.php?action=downloadfile&filename=curry.zip&directory=Function%20Objects&

- 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.

Samples:
____________________________________________________________________

// 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) + ")";
  };
};

BOOST_AUTO_TEST_CASE(test_pod)
{
  // setup curry

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

  // unary function
  m00.entry<int>(test_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
  BOOST_CHECK_EQUAL(m00()(100)(),
     "operator()(const int:100)");
  BOOST_CHECK_EQUAL(m00()(100)(100.0)(),
     "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)");

};

Regrads.
Nowake


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk