Incorporating functional programming into C++

When I first learned programming in C++ (perhaps less than 2 years ago though it feels much longer), armed with only rudimentary knowledge of C my first project was 'spaghetti' code as I've heard it called. I ended up with object references being passed deep into lower level functions, sometimes through other functions with the only intention being of passing to another function within.

So my next efforts moved gradually to a 'cleaner' interface which involved using singletons. Well, when I decided I wanted to have a sort of 'state stack' for my game modes the singleton turned out not to be 'required' at all.

A few days back I was searching on the topic and I learned of something called 'state' and a relation to functional programming languages, lambda calculus, and monads.

Is this something that can help me prevent spaghetti code in C++?

Is the <functional> header adequate?

The problem set is real time simulations for games and I have been trying to program in a data oriented way with a clean interface.

At the very least I will learn some lambda calculus and try some things in Supercollider but I have yet to see any code/examples that could get me started in C++ so any links on that matter would be helpful.

Thanks.
Functional programming is done in completely different languages than C++. It's a completely different approach to programming than C++.

If you want to use clean object orientation, learning about design patters (maybe with this book- it's java but all of it applies for C++ too http://www.oreilly.de/catalog/9780596007126/# or online here http://www.oodesign.com/ ) will help.


Oh, and good that you have found out singletons are a bad idea. Not that Singletons are BAD, but they are not there to be used as global variable replacements (actually, using them as such would be the same as using global variables), they are actually there to prevent multiple instances of an object existing at the same time where it would be harmful (which rarely ever happens, but never say never). Using classes is one thing, REAL object oriented programming something entirely different. And don't worry, everyone struggles with it. OO is awesome when used correctly, but it really requires a lot of discipline.

(To be fair, you can of course use some functional programming paradigms in C++0x, but... well, for real functional languages you could check out Lisp)
Last edited on
<functional> could help, but under C++98 it is still quite kludgy. C++0x will provide much better support for functional programming in <functional> and in <lambda>. The most useful part of the STL for functional programming right now is <iterator> and <algorithm>.

The functional concepts of apply (std::for_each), map (std::transform), reduce (std::accumulate), filter (std::copy_if) plus many others exist in<algorithm>.

There are a lot of facilities in modern C++ to aid in functional programming. The most portable way to do this at the moment is to install the Boost libraries and play around with the Boost bind (equivalent to C++0x's std::bind), and then try out out the Boost Lambda library. Bind provides a simple means of doing partial application. Lambda allow the creation of unnamed functions.

The Boost Iterator library allow the creation of lazily evaluated maps and filters. One can use the boost::transform_iterator as a way to implement a lazy evaluation map function. Same for the boost::filter_iterator and filter function.
Topic archived. No new replies allowed.