TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

C++ Algorithms, Boost and function currying

11 点作者 spivey将近 15 年前

6 条评论

dkersten将近 15 年前
The author went from this:<p><pre><code> int Dice::total() const { int total = 0; for(const_iterator current = dice.begin(); current != dice.end(); ++current) total += (*current)-&#62;faceValue(); return total; } </code></pre> to this:<p><pre><code> int Dice::total() const { return std::accumulate( dice.begin(), dice.end(), 0, bind(std::plus&#60;int&#62;(), _1, bind(&#38;Die::faceValue, _2)) ); } </code></pre> 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:<p><pre><code> class Sum { const std::vector&#60;Face*&#62;&#38; v; public: int sum; void operator()( const tbb::blocked_range&#60;size_t&#62;&#38; r ) { const std::vector&#60;Face*&#62;&#38; a = v; for( size_t i = r.begin(); i != r.end(); ++i ) sum += a[i]-&#62;faceValue(); } Sum( Sum&#38; x, tbb::split ) : v(x.v), sum(0) {} void join( const Sum&#38; y ) {sum += y.sum;} Sum(const std::vector&#60;Face*&#62;&#38; a ) : v(a), sum(0) {} }; int Dice::parallel_total () const { Sum s(dice); tbb::parallel_reduce(tbb::blocked_range&#60;size_t&#62;(0, dice.size()), s, tbb::auto_partitioner()); return s.sum; } </code></pre> 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.
Chris_Newton将近 15 年前
A more declarative programming style has many potential benefits. The trouble is, the version in that style is supposed to look something like this:<p><pre><code> total dice = sum (map faceValue dice) </code></pre> 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.<p>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.
ANH将近 15 年前
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: <a href="http://en.m.wikipedia.org/wiki/Currying?wasRedirected=true" rel="nofollow">http://en.m.wikipedia.org/wiki/Currying?wasRedirected=true</a>
steveklabnik将近 15 年前
This is also currently on HN as part of zedshaw's post on Shedding Bikes: <a href="http://sheddingbikes.com/posts/1276445247.html" rel="nofollow">http://sheddingbikes.com/posts/1276445247.html</a> <a href="http://news.ycombinator.com/item?id=1427599" rel="nofollow">http://news.ycombinator.com/item?id=1427599</a>
d0m将近 15 年前
why use bind and not a "c++ lambda" if you are using boost?<p>The expression:<p>bind(std::plus&#60;int&#62;(), _1, bind(&#38;Die::faceValue, _2))<p>makes the code harder to read instead of easier, which isn't an improvement.
tman将近 15 年前
Where functional programming in C++ falls apart is this step:<p><pre><code> bind(std::plus&#60;int&#62;(), _1, bind(&#38;Die::faceValue, _2)) </code></pre> 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.<p>Luckily the lambda syntax from the new standard already appears to be available in experimental gcc. This is going to be a game-changer.
评论 #1427765 未加载