Can someone guide me how do functional programming in C++? Is there some good online material that I can refer?
Please note that I know about the library FC++. I want to know how to do that with C++ standard library alone.
Thanks.
A closure is a programming technique that allows variables outside of the scope of a function to be accessed. Usually, a closure is created when a function is defined in another function, allowing the inner function to access variables in the outer one.
Obviously, C is a procedural language and doesn't really support functional programming natively.
Closure is a feature in JavaScript where a function has access to its own scope variables, access to the outer function variables and access to the global variables. Closure has access to its outer function scope even after the outer function has returned.
A closure is a kind of object that contains a pointer or reference of some kind to a function to be executed along with the an instance of the data needed by the function.
You can accomplish a surprising amount of "functional programming" style with modern C++. In fact, the language has been trending in that direction since its' standardization.
The standard library contains algorithms analogous to map, reduce, etc (for_each, transform, adjacent_sum...). The next revision, C++0x, contains many features designed to let programmers work with these in a more functional style (lambda expressions, etc.).
Look into the various Boost libraries for more fun. Just to illustrate that standard C++ contains plenty of functional goodness, here's a factorial function in continuation-passing style in standard C++.
#include <iostream> // abstract base class for a continuation functor struct continuation { virtual void operator() (unsigned) const = 0; }; // accumulating continuation functor struct accum_cont: public continuation { private: unsigned accumulator_; const continuation &enclosing_; public: accum_cont(unsigned accumulator, const continuation &enclosing) : accumulator_(accumulator), enclosing_(enclosing) {}; virtual void operator() (unsigned n) const { enclosing_(accumulator_ * n); }; }; void fact_cps (unsigned n, const continuation &c) { if (n == 0) c(1); else fact_cps(n - 1, accum_cont(n, c)); } int main () { // continuation which displays its' argument when called struct disp_cont: public continuation { virtual void operator() (unsigned n) const { std::cout << n << std::endl; }; } dc; // continuation which multiplies its' argument by 2 // and displays it when called struct mult_cont: public continuation { virtual void operator() (unsigned n) const { std::cout << n * 2 << std::endl; }; } mc; fact_cps(4, dc); // prints 24 fact_cps(5, mc); // prints 240 return 0; }
Ok, I lied a little bit. It's a factorial functor. After all, closures are a poor man's objects... and vice versa. Most of the functional techniques used in C++ rely on the use of functors (i.e. function objects)---you'll see this extensively in the STL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With