什么是期货?

发布于 2024-07-04 20:15:18 字数 24 浏览 5 评论 0原文

什么是期货? 这与惰性评估有关。

What are futures? It's something to do with lazy evaluation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

汐鸠 2024-07-11 20:15:25

这篇博文一起给出了非常彻底的解释并举例说明如何自己实现未来。 我真的推荐它:)

This blog post gives a very thorough explanation together with an example of how you could implement a future yourself. I really recommend it :)

柒夜笙歌凉 2024-07-11 20:15:24

Future 也用于某些设计模式,特别是实时模式,例如 ActiveObject 模式,它将方法调用与方法执行分开。 未来被设置为等待完成执行。 当您需要从多线程环境转移到与单线程环境通信时,我倾向于看到它。 在某些情况下,硬件不具有线程的内核支持,并且在这种情况下使用 future。 乍一看,你如何沟通并不明显,令人惊讶的是,Future 让它变得相当简单。 我有一些 C# 代码。 我会把它挖出来并发布。

Futures are also used in certain design patterns, particularly for real time patterns, for example, the ActiveObject pattern, which seperates method invocation from method execution. The future is setup to wait for the completed execution. I tend to see it when you need to move from a multithreaded enviroment to communicate with a single threaded environment. There may be instances where a piece of hardware doesn't have kernel support for threading, and futures are used in this instance. At first glance it not obvious how you would communicate, and surprisingly futures make it fairly simple. I've got a bit of c# code. I'll dig it out and post it.

幼儿园老大 2024-07-11 20:15:23

Wiki 文章对 Future 进行了很好的概述。 该概念通常用在并发系统中,用于对可能已计算或尚未计算的值进行调度计算,并且进一步,其计算可能正在进行或尚未进行。

来自文章:

未来与
计算其的特定线程
价值。 该计算可以开始
要么在未来到来时热切地
创建,或在其值为时懒惰地创建
首先需要。

文章中没有提到,futures 是一个 Monad,因此可以将函数投影到将未来值放入 monad 中,以便在未来值可用时将它们应用于未来值,从而产生另一个未来,而该未来又代表该函数的结果。

The Wiki Article gives a good overview of Futures. The concept is generally used in concurrent systems, for scheduling computations over values that may or may not have been computed yet, and further, whose computation may or may not already be in progress.

From the article:

A future is associated with a
specific thread that computes its
value. This computation may be started
either eagerly when the future is
created, or lazily when its value is
first needed.

Not mentioned in the article, futures are a Monad, and so it is possible to project functions on future values into the monad to have them applied to the future value when it becomes available, yielding another future which in turn represents the result of that function.

软糯酥胸 2024-07-11 20:15:22

Future 封装了延迟计算,通常用于将惰性求值硬塞到非惰性语言中。 第一次评估 future 时,将运行评估它所需的代码,并用结果替换 future。

由于 future 被替换,后续的计算不会再次执行代码,而只是产生结果。

A Future encapsulates a deferred calculation, and is commonly used to shoehorn lazy evaluation into a non-lazy language. The first time a future is evaluated, the code required to evaluate it is run, and the future is replaced with the result.

Since the future is replaced, subsequent evaluations do not execute the code again, and simply yield the result.

蓝海 2024-07-11 20:15:22

大家提到期货都是为了偷懒计算。 然而,另一个并不像宣传的那样的用途是一般情况下使用 Futures for IO。 特别是它们对于加载文件和等待网络数据非常有用

Everyone mentions futures for the purpose of lazy calculation. However another use that isn't as advertised is the use of Futures for IO in general. Especially they're useful for loading files and waiting on network data

微暖i 2024-07-11 20:15:21

当您创建 future 时,将启动一个新的后台线程,开始计算实际值。 如果您请求未来的值,它将阻塞,直到线程完成计算。 当您需要并行生成一些值并且不想手动跟踪所有值时,这非常有用。

对于 Ruby,请参阅 lazy.rb,或 Scala、futures 和延迟计算

它们可能可以用任何带有线程的语言来实现,尽管在像 C 这样的低级语言中显然比在高级函数式语言中更困难。

When you create a future, a new background thread is started that begins calculating the real value. If you request the value of the future, it will block until the thread has finished calculating. This is very useful for when you need to generate some values in parallel and don't want to manually keep track of it all.

See lazy.rb for Ruby, or Scala, futures, and lazy evaluation.

They can probably be implemented in any language with threads, though it would obviously be more difficult in a low-level language like C than in a high-level functional language.

仙女 2024-07-11 20:15:20

有一篇关于期货的维基百科文章。 简而言之,这是一种使用未知值的方法。 然后可以根据需要计算该值(惰性计算),并且可以选择与主计算同时计算。

C++ 示例如下。


假设您要计算两个数字的总和。 您可以采用典型的 eager 实现:

int add(int i, int j) { return i + j; }
// first calculate both Nth_prime results then pass them to add
int sum = add(Nth_prime(4), Nth_prime(2)); 

也可以使用 C++11 的 std::async 的 futures 方式,它返回一个 std::future。 在这种情况下,add 函数仅在尝试使用尚未计算的值时才会阻塞(也可以创建一种纯粹的惰性替代方案)。

int add(future<int> i, future<int> j) { return i.get() + j.get(); }
int sum = add(async(launch::async, [](){ return Nth_prime(4); }),
              async(launch::async, [](){ return Nth_prime(2); }));

There is a Wikipedia article about futures. In short, it's a way to use a value that is not yet known. The value can then be calculated on demand (lazy evaluation) and, optionally, concurrently with the main calculation.

C++ example follows.


Say you want to calculate the sum of two numbers. You can either have the typical eager implementation:

int add(int i, int j) { return i + j; }
// first calculate both Nth_prime results then pass them to add
int sum = add(Nth_prime(4), Nth_prime(2)); 

or you can use the futures way using C++11's std::async, which returns an std::future. In this case, the add function will only block if it tries to use a value that hasn't yet been computed (one can also create a purely lazy alternative).

int add(future<int> i, future<int> j) { return i.get() + j.get(); }
int sum = add(async(launch::async, [](){ return Nth_prime(4); }),
              async(launch::async, [](){ return Nth_prime(2); }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文