无法理解 Play 2.0 中的 Iteratee、Enumerator、Enumeratee [英] Can't understand Iteratee, Enumerator, Enumeratee in Play 2.0

查看:24
本文介绍了无法理解 Play 2.0 中的 Iteratee、Enumerator、Enumeratee的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 Play 2.0 框架.我无法理解的一件事是 play 教程中描述的 Iteratee、Enumerator 和 Enumeratee 模式.我在函数式语言方面的经验很少.

I have just started to learn the Play 2.0 Framework. The one thing I just can't understand is the Iteratee, Enumerator and Enumeratee pattern described in the play tutorial. I have very little experience in functional languages.

这个模式有什么作用?

它如何帮助我编写非阻塞/响应式代码?

How does it help me write non-blocking/reactive code?

一些简单的例子会有所帮助.

some simple examples would help.

推荐答案

playframework 2.0 下载带有一些示例.其中两个有 Iteratee/Comet 示例.例如,彗星时钟 示例应用 显示:

The playframework 2.0 download comes with some samples. Two of which have Iteratee/Comet examples. For instance, the comet-clock sample app shows:

lazy val clock = Enumerator.fromCallback { () =>
  Promise.timeout(Some(dateFormat.format(new Date)), 100 milliseconds)
}

然后是这样使用的:

Ok.stream(clock &> Comet(callback = "parent.clockChanged"))

这会将结果以块的形式提供给客户端.Enumerator 对象还有一个 fromFilefromStream(如在 java.io.InputStream 中)实用枚举器函数.

Which will feed the result to the client in chunks. The Enumerator object also has a fromFile, fromStream (as in java.io.InputStream) utility enumerator functions.

我不确定这是在哪里完成的,但假设这种分块处理不会占用线程.看到一些基准测试会很有趣,因为在迭代器的实现中肯定会有开销,因为要处理的数据以及计算都包含在各种对象中.

I am not sure where this is done but the assumption is that this chunked processing is not tying up threads. It would be very interesting to see some benchmarks, as there is certainly overhead in the implementation of iteratees as the data to be processed as well as the computation is wrapped in various objects.

从枚举器提供的数据被包装,以便它可以指示有更多数据要处理或数据已到达末尾 (EOF).迭代器的处理结果也被包装,以便它可以指示是否已经在某个输入上计算了结果,或者需要更多输入来计算结果.我推荐 John De Goes 的 nescala 演示,它展示了从折叠到迭代器.Brendan McAdams 有一个不错的 Scala Days 2012 presentation 关于异步和非阻塞 - 在演示结束时(约 26 分钟),它涉及迭代对象以及它如何帮助以异步方式处理数据库游标样式 IO.

Data that is fed from an enumerator is wrapped so that it can indicates there is more data to process or the data has reached the end (EOF). Processing results of iteratees are also wrapped so that it can indicate whether a result has been computed on some input or more input is needed to compute a result. I recommend John De Goes' nescala presentation that shows the evolution from a fold to Iteratees. Brendan McAdams has a nice Scala Days 2012 presentation on Async and non-blocking - towards the end of the presentation (~26min) it touches on iteratees and how it helps with processing database cursor style IO in async style.

Iteratees 的一个被吹捧的好处是它们可以组合.以下是它们的几种组成方式:

One touted benefit of Iteratees is that they compose. Here are a few ways they compose:

  • 你可以提供一个枚举器然后另一个
  • 你可以映射一个(T) =>类型的函数UT 的枚举器上得到 U
  • 的枚举器
  • 你可以交错两个枚举器
  • 一个迭代器可以留下一些输入供另一个迭代器使用
  • you can feed an enumator andThen another
  • you can map a function of type (T) => U over an enumerator of T to get an enumerator of U
  • you can interleave two enumerators
  • an iteratee can leave some input to be consumed by another iteratee

这篇关于无法理解 Play 2.0 中的 Iteratee、Enumerator、Enumeratee的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆