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.

Curious C++ Lambda Examples: Recursion, constexpr, Containers C++23 included

67 pointsby joebafalmost 3 years ago

9 comments

josephcsiblealmost 3 years ago
The examples with `fact_impl` are basically how you'd write recursion in terms of a fixed-point combinator (e.g., the very Y combinator that this site is named after).
评论 #32456461 未加载
评论 #32457185 未加载
throwaway72937almost 3 years ago
Is there a book like “C++ for C++ programmers” for people that knew C++ 20 years ago but didn’t keep up with the latest features?
评论 #32457965 未加载
评论 #32464043 未加载
评论 #32458056 未加载
评论 #32458764 未加载
评论 #32458061 未加载
评论 #32462680 未加载
评论 #32462746 未加载
评论 #32457962 未加载
dig1almost 3 years ago
Instead of using &quot;this auto&amp;&amp;&quot; trick to get recursive lambdas, I wonder why they didn&#x27;t go with named lambdas (Clojure made it really nice). E.g.<p><pre><code> auto factorial23 = [] fact_recur (int n) { if (n &lt;= 1) return 1; return n * fact_recur(n - 1); }; </code></pre> Maybe this has to do with the usual C++ complicated parsing rules... But, on the other hand, named lambdas, besides recursion, in the language mentioned above helps with stacktraces, so you get the idea where the issue could be, instead of getting an anonymous mangled name mess.
评论 #32458689 未加载
评论 #32459449 未加载
nh23423fefealmost 3 years ago
People say things like, &quot;haskell is hard to reason about because of lazy evaluation&quot; but then this.<p>Also many sets of people are disjoint.
dlesliealmost 3 years ago
Oof, just a whole lot of oof.<p>So much developer thought and mental energy expended to do something as simple as recursion.
评论 #32457757 未加载
评论 #32456372 未加载
评论 #32457186 未加载
mgaunardalmost 3 years ago
I find it interesting that other commenters are finding those trivialities convoluted and unreadable while at the same time constantly championing languages which are genuinely complicated.
评论 #32457693 未加载
评论 #32460114 未加载
krater23almost 3 years ago
This examples are reading all like &#x27;How to use the newest C++ features to write unreadable code&#x27;.
评论 #32458435 未加载
评论 #32456977 未加载
cjfdalmost 3 years ago
I think they should stop adding new major feature to C++. It is big enough. One can do enough with it. O, you cannot call a lambda recursively? Then just tell people to write a normal named function instead of polluting the language further with unneeded stuff.
评论 #32457703 未加载
jeffbeealmost 3 years ago
Recursion is one of those things that for some reason they continue to teach kids in college, which has no utility in practice. Things like recursion and linked lists should be erased from the curricula.<p>There probably is a language and an application where recursion makes sense but C++ isn&#x27;t one of those languages. Certainly a factorial is a horrible example because iterative is strictly better.
评论 #32456386 未加载
评论 #32456587 未加载
评论 #32456554 未加载
评论 #32457716 未加载
评论 #32456328 未加载
评论 #32456414 未加载
评论 #32458188 未加载