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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Streams – Lazy evaluation in C++14

61 点作者 normanv将近 11 年前

5 条评论

wwwwwwwwww将近 11 年前
honest question: is there any reason to add lazy evaluation to c++ other than to just add more features to the language?<p>lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides
评论 #7930293 未加载
评论 #7930437 未加载
评论 #7931238 未加载
评论 #7931206 未加载
评论 #7930678 未加载
mattpodwysocki将近 11 年前
Seems very close in implementation to RxCPP for push based streams and IxCPP for pull based, which has been created by Microsoft and now controlled by Microsoft Open Technologies. This library though does not require C++14 and is being actively developed: <a href="https://github.com/Reactive-Extensions/RxCPP" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Reactive-Extensions&#x2F;RxCPP</a>
srean将近 11 年前
@wwwwwwwwww you have been hell-banned and I dont see anything particularly offensive in your immediate comment history.<p>&gt; is there any reason to add lazy evaluation to c++ [...]<p>I think you are suffering from a misapprehension. Nothing is being added to C++ the language. This is just an implementation of lazy streams in C++14. Think of it as an optional 3rd party library.<p>&gt; lazy evaluation is one of those features where i have absolutely no idea what benefit it actually provides<p>Let me give you two examples. First try and solve this problem in your favorite language. Produce a sequence of k numbers that are multiples of 2,3 and 5 only, but produce them in sorted order. Yes of course this is contrived, but it will give you and idea of the conveniences that laziness can give you. [sorry, I gave away the important spoiler]<p>Second example say you want to compute<p><pre><code> (x + y)*z - w </code></pre> where each of them are objects representing long vectors. You also want to keep the code readable and close to the mathematical notation of the domain. Naive way to do it will create lots of temporaries and unnecessary loops. Every binary operation is going to create a temporary (and entail a loop). But if you have laziness you just need one loop.<p>[EDIT: <i>If one has laziness one need not instantiate the actual vector (x+y). One can return an object that only represents the operation. So one can keep building on the parse tree and finally when the result is actually used somewhere do you evaluate the expression encoded in the parse tree. Now since you have the entire expression tree you can evaluate it in just one loop. If you are curious you can search for deforestation and expression templates. You can of course do the same thing even if the language does not support laziness, but laziness is the key enabler here, if the language does not support it, _you_ have to implement it</i>]. Without this features it is pretty much impossible to approach Fortran like speeds and expressiveness in array operations in C++.<p>A reason why basic Numpy is slow is because is that written naively it creates all these temporaries. There are a few ways to avoid it. Numexpr uses laziness (and tiling). You could also use the &#x27;out&#x27; feature of Numpy functions creatively.<p>So in short, laziness has a lot of practical value, and I am not even bringing up aspects of purity etc. I had to roll something on my own for these needs. Would have been happier if there were such libraries available. [Ok full disclosure, I would have likely rolled my own anyway to understand how it works] There are a few libraries that do these things or help a lot in building these things. Some of the are Spirit and Fusion. Although I am conflating two different topics (i) fusion&#x2F;deforestation and (ii) laziness, but they are related enough.<p>@dang thanks for the clarification, wwwwwwww&#x27;s comment was indeed showing up as dead on this thread. wwwwwwww must have deleted his comment because at that time there was only one.
评论 #7929648 未加载
评论 #7929714 未加载
评论 #7929734 未加载
评论 #7929730 未加载
评论 #7930375 未加载
评论 #7930676 未加载
aetherspawn将近 11 年前
The performance isn&#x27;t going to be very good without stream fusion, I would imagine.
ris将近 11 年前
Ah, python&#x27;s itertools available in c++, at last.
评论 #7929905 未加载