Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional Programming in C++ [closed]

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.

like image 912
Red Hyena Avatar asked Dec 30 '09 17:12

Red Hyena


People also ask

What is a functional programming closure?

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.

Can functional programming be done in C?

Obviously, C is a procedural language and doesn't really support functional programming natively.

Is closure a functional programming language?

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.

What are closures in C?

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.


1 Answers

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.

like image 136
Derrick Turk Avatar answered Oct 09 '22 03:10

Derrick Turk