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

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

问题描述

我刚刚开始学习Play 2.0框架.我无法理解的一件事是播放教程中描述的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)
}

然后按以下方式使用它:

Then it is used like this:

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)在Async和非阻塞-在演示结束时(约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枚举器上映射类型为(T) => U的函数以获得U
  • 的枚举器
  • 您可以交织两个枚举器
  • 一个iteratee可以保留一些输入以供另一个iteratee使用
  • 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天全站免登陆