C++ Algorithms, Boost and function currying(blog.objectmentor.com)
blog.objectmentor.com
C++ Algorithms, Boost and function currying
http://blog.objectmentor.com/articles/2010/06/12/c-algorithms-boost-and-function-currying
10 comments
A more declarative programming style has many potential benefits. The trouble is, the version in that style is supposed to look something like this:
In this context, I think one of the commenters on the original article probably had it right: given that C++ is essentially an imperative language, using the new range-based for loops is a more natural way to write this algorithm.
total dice = sum (map faceValue dice)
C++ is showing its age now, as ideas from other programming styles are entering the mainstream. Modern programming languages, designed with the benefit of hindsight, support those idioms natively and much more effectively than a bolted-on library, the cleverness of the Boost implementers notwithstanding.In this context, I think one of the commenters on the original article probably had it right: given that C++ is essentially an imperative language, using the new range-based for loops is a more natural way to write this algorithm.
Okay, I admit this is the first time I've seen the word 'currying'. I've heard it spoken and thought it was a neologism derived from "courier", but it's apparently named after a man called Haskell: http://en.m.wikipedia.org/wiki/Currying?wasRedirected=true
This is also currently on HN as part of zedshaw's post on Shedding Bikes: http://sheddingbikes.com/posts/1276445247.html
http://news.ycombinator.com/item?id=1427599
why use bind and not a "c++ lambda" if you are using boost?
The expression:
bind(std::plus<int>(), _1, bind(&Die::faceValue, _2))
makes the code harder to read instead of easier, which isn't an improvement.
The expression:
bind(std::plus<int>(), _1, bind(&Die::faceValue, _2))
makes the code harder to read instead of easier, which isn't an improvement.
Where functional programming in C++ falls apart is this step:
Luckily the lambda syntax from the new standard already appears to be available in experimental gcc. This is going to be a game-changer.
bind(std::plus<int>(), _1, bind(&Die::faceValue, _2))
When I first learned the STL, I worked pretty hard to figure out bind, etc., and get rid of all my for loops. The problem is that it doesn't actually buy you that much. The for loop is uglier, but simpler to maintain, read, and debug.Luckily the lambda syntax from the new standard already appears to be available in experimental gcc. This is going to be a game-changer.
The lambda stuff is in g++ 4.5, which is a stable release, right? It's not really experimental.
[deleted]
Lambdas are also supported in MSVC++ 2010.
http://gcc.gnu.org/gcc-4.5/cxx0x_status.html : "Status of Experimental C++0x Support in GCC 4.5"
Last month's release of 4.5.0 is stable, but the C++0x support is experimental.
Last month's release of 4.5.0 is stable, but the C++0x support is experimental.
to this:
and shows his intermediate steps. I took his last variant and converted it to the following, using Intel Threading Building Blocks to perform a parallel reduce:
I had to guess what his Dice implementation contained and if I wrote in from scratch, I may have done it differently (eg, I'd handle that vector in Sum differently). Also, it doesn't really seem worth it performance-wise ;-) but still interesting to see it in action.