TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

FunctionalPlus – helps you write concise and readable C++ code

57 pointsby Dobiasdover 9 years ago

4 comments

ajtullochover 9 years ago
I would recommend instead Facebook&#x27;s folly::gen library, which provides these functional combinators in a high-performance fashion, by expressing them as streaming combinators. You get all the map&#x2F;filter&#x2F;take&#x2F;drop&#x2F;reduce primitives, but in a much cleaner way.<p>See <a href="https:&#x2F;&#x2F;github.com&#x2F;facebook&#x2F;folly&#x2F;blob&#x2F;master&#x2F;folly&#x2F;gen&#x2F;test&#x2F;BaseTest.cpp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;facebook&#x2F;folly&#x2F;blob&#x2F;master&#x2F;folly&#x2F;gen&#x2F;test...</a> for an example.
评论 #10631882 未加载
评论 #10632439 未加载
评论 #10632113 未加载
jzwinckover 9 years ago
One of the main reasons to use C++ today is the potential for high performance. One of the examples given is a great demonstration of what not to do: allocate a new container on the heap, scan it once, and delete it. I&#x27;m talking about the &quot;I in team&quot; example, which if you coded it by hand would almost certainly use a better algorithm. It&#x27;s hard enough teaching people to avoid inefficiency when it&#x27;s right there in front of them, and harder still when you encapsulate inefficiency in a pretty package. This is why some &quot;obvious&quot; functions don&#x27;t exist in the STL.
评论 #10631847 未加载
评论 #10631511 未加载
评论 #10631616 未加载
makecheckover 9 years ago
The examples are clearly achieving the goal of &quot;concise&quot;. I would counter with a different style though, because I try to imagine how the code would change over time and how it would fare when given constraints on memory and runtime.<p>Compare:<p><pre><code> for (int x : numbers) { if (1 == (x % 2)) { odds.push_back(x); } } auto odds = KeepIf(IsOdd, numbers); </code></pre> To me, the first case is preferable. The 2nd case uses custom functions that may not be familiar to the maintainer, and semantics are clearly hidden (e.g. does KeepIf() modify the list, return a copy, or return some kind of intelligent generator object that still depends on the lifetime of &quot;numbers&quot;?).<p>Suppose I discover that my application starts crashing with the odd number 15 and I have to work around that case very quickly. The loop version above is trivial to modify, whereas I have <i>no idea</i> how the 2nd version would be modified to filter out one value! And if I want to do several things with the numbers while they&#x27;re being found (e.g. print them out), the loop is easy to change but the 2nd version basically has to be replaced entirely (and replacing even a concise expression is risky due to the issues above, e.g. unclear semantics).<p>Now compare approaches for another case:<p><pre><code> if (FindWord(&quot;I&quot;, team)) ... if (Contains(&quot;I&quot;, SplitWords(team))) ... </code></pre> This shows a general problem with a &quot;functional style&quot; at times: it&#x27;s placing constraints on the implementation that don&#x27;t need to be there.<p>To me, the first style is preferable because FindWord() could easily walk a raw string and <i>stop</i> as soon as it finds the target word, with a chance for better-than-N average complexity. The second case would mandate: copying strings, visiting all characters in the string every time, and revisiting at least some of the characters in the form of words.<p>Finally, I think that the C++ standard algorithms, lambdas, and compact-&quot;for&quot; do allow a lot of these tricks to be avoided. Consider algorithms like std::all_of() for instance, which can be combined with lambdas to produce the same results as examples like &quot;AllBy(And(...&quot; but without the downsides I mention at the beginning of my comment.
评论 #10632237 未加载
评论 #10632423 未加载
评论 #10632544 未加载
评论 #10632465 未加载
Dobiasdover 9 years ago
While working on my last projects I accumulated a collection of pure functions I needed now and then. So I thought, why not write a short pitch as a README and make it public? So here it is. It would be very nice of you if you could tell me what you think, so I can learn and improve.
评论 #10631579 未加载
评论 #10634213 未加载
评论 #10631833 未加载