来自 scala 迭代器的 scalaz Iteratees

发布于 2024-10-10 08:53:38 字数 1760 浏览 3 评论 0原文

我编辑了下面的代码,因为我相信我在 iter.next 问题之上错误地组合了 IterV 对象。

我正在 scalaz 中试验 Iteratee ,我想知道为什么以下内容不起作用。这是我所拥有的:

import scalaz._
import Scalaz._
import IterV._

implicit val iteratorEnumerator = new Enumerator[Iterator] {
  def apply[E,A](iter: Iterator[E], i: IterV[E,A]): IterV[E,A] =
    if (iter.isEmpty) i
    else i.fold(done = (acc,input) => i, 
                cont = k => apply(iter, k(El(iter.next))))
}

/* probably incorrect
val iter = Iterator(1,2,3)
println("peek(iter) " + peek(iter).run)
println("peek(iter) " + peek(iter).run)
*/

def peekpeek[E]: IterV[E, (Option[E],Option[E])] = 
  for (a <- peek; b <- peek) yield (a,b)
def peekheadpeek[E]: IterV[E, (Option[E],Option[E],Option[E])] = 
  for (a <- peek; b <- head; c <- peek) yield (a,b,c)

peekpeek(Iterator(1,2,3,4)).run
peekheadpeek(Iterator(1,2,3,4)).run

这返回:

res0: (Option[Int], Option[Int]) = (Some(1),Some(2)) 
res1: (Option[Int], Option[Int], Option[Int]) = (Some(1),Some(2),Some(3)) 

我期待的 (Some(1),Some(1))(Some(1),Some(1),Some(2) )

我怀疑这与 iter.next 的副作用有关。处理这个问题的最佳方法是什么?

为了进行比较, 直接取自 scalaz 网站 的源代码示例,可以正常工作:

implicit val StreamEnumerator = new Enumerator[Stream] {
  def apply[E, A](e: Stream[E], i: IterV[E, A]): IterV[E, A] = e match {
    case Stream() => i
    case x #:: xs => i.fold(done = (_, _) => i, 
                            cont = k => apply(xs, k(El(x))))
  }
}

I edited the code below as I believe I had been combining the IterV objects incorrectly on top of the iter.next issue.

I'm experimenting with Iteratee in scalaz and I am wondering why the following does not work. Here is what I have:

import scalaz._
import Scalaz._
import IterV._

implicit val iteratorEnumerator = new Enumerator[Iterator] {
  def apply[E,A](iter: Iterator[E], i: IterV[E,A]): IterV[E,A] =
    if (iter.isEmpty) i
    else i.fold(done = (acc,input) => i, 
                cont = k => apply(iter, k(El(iter.next))))
}

/* probably incorrect
val iter = Iterator(1,2,3)
println("peek(iter) " + peek(iter).run)
println("peek(iter) " + peek(iter).run)
*/

def peekpeek[E]: IterV[E, (Option[E],Option[E])] = 
  for (a <- peek; b <- peek) yield (a,b)
def peekheadpeek[E]: IterV[E, (Option[E],Option[E],Option[E])] = 
  for (a <- peek; b <- head; c <- peek) yield (a,b,c)

peekpeek(Iterator(1,2,3,4)).run
peekheadpeek(Iterator(1,2,3,4)).run

This returns:

res0: (Option[Int], Option[Int]) = (Some(1),Some(2)) 
res1: (Option[Int], Option[Int], Option[Int]) = (Some(1),Some(2),Some(3)) 

Where I was expecting (Some(1),Some(1)) and (Some(1),Some(1),Some(2)).

I suspect this has to do with iter.next being side effecting. What's the best way to deal with that?

For comparison, this taken directly from the source code examples from the scalaz web site works properly:

implicit val StreamEnumerator = new Enumerator[Stream] {
  def apply[E, A](e: Stream[E], i: IterV[E, A]): IterV[E, A] = e match {
    case Stream() => i
    case x #:: xs => i.fold(done = (_, _) => i, 
                            cont = k => apply(xs, k(El(x))))
  }
}

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

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

发布评论

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

评论(4

鱼窥荷 2024-10-17 08:53:38

我想我已经明白了这一点。这似乎主要是由于 El 使用了按名称参数,它重新计算了 iter.next - 以及我最初如何使用两个不同的 错误地调用了计算peek(iter).run。我重写了枚举器,首先将 iter.next 分配给 val (并在此过程中使其尾部递归):

implicit val iteratorEnumerator = new Enumerator[Iterator] {
  @annotation.tailrec
  def apply[E,A](iter: Iterator[E], i: IterV[E,A]): IterV[E,A] = i match {
    case _ if iter.isEmpty => i
    case Done(acc, input) => i
    case Cont(k) => 
      val x = iter.next
      apply(iter, k(El(x)))
  }
}

然后:

def peekpeek[A] = 
  for (a1 <- peek[A]; a2 <- peek[A]) yield (a1,a2)
def peekheadpeek[A] = 
  for (a1 <- peek[A]; a2 <- head[A]; a3 <- peek[A]) yield (a1,a2,a3)
def headpeekhead[A] = 
  for (a1 <- head[A]; a2 <- peek[A]; a3 <- head[A]) yield (a1,a2,a3)

peekpeek(Iterator(1,2,3)).run
peekheadpeek(Iterator(1,2,3)).run
headpeekhead(Iterator(1,2,3)).run
length(Iterator.from(1).take(1000000)).run

将返回以下内容:

res13: (Option[Int], Option[Int]) = (Some(1),Some(1))
res14: (Option[Int], Option[Int], Option[Int]) = (Some(1),Some(1),Some(2))
res15: (Option[Int], Option[Int], Option[Int]) = (Some(1),Some(2),Some(2))
res16: Int = 1000000

I think I figured this out. It seemed to be primarily due to El using a by name parameter, which recomputes iter.next - as well as how I initially called the computation incorrectly with two different peek(iter).run. I rewrote the enumerator to first assign iter.next to a val (and also made it tail recursive in the process):

implicit val iteratorEnumerator = new Enumerator[Iterator] {
  @annotation.tailrec
  def apply[E,A](iter: Iterator[E], i: IterV[E,A]): IterV[E,A] = i match {
    case _ if iter.isEmpty => i
    case Done(acc, input) => i
    case Cont(k) => 
      val x = iter.next
      apply(iter, k(El(x)))
  }
}

Then:

def peekpeek[A] = 
  for (a1 <- peek[A]; a2 <- peek[A]) yield (a1,a2)
def peekheadpeek[A] = 
  for (a1 <- peek[A]; a2 <- head[A]; a3 <- peek[A]) yield (a1,a2,a3)
def headpeekhead[A] = 
  for (a1 <- head[A]; a2 <- peek[A]; a3 <- head[A]) yield (a1,a2,a3)

peekpeek(Iterator(1,2,3)).run
peekheadpeek(Iterator(1,2,3)).run
headpeekhead(Iterator(1,2,3)).run
length(Iterator.from(1).take(1000000)).run

would return this:

res13: (Option[Int], Option[Int]) = (Some(1),Some(1))
res14: (Option[Int], Option[Int], Option[Int]) = (Some(1),Some(1),Some(2))
res15: (Option[Int], Option[Int], Option[Int]) = (Some(1),Some(2),Some(2))
res16: Int = 1000000
梦里寻她 2024-10-17 08:53:38

您关于 iter.next 引起副作用的说法是正确的。我认为问题归结为 Stream 和 Iterator 之间的区别是什么。 此问题包含相关信息。

You're correct about iter.next causing side effects. I think that the question boils down to what is the difference between a Stream and an Iterator. This question has relevant information.

爱的十字路口 2024-10-17 08:53:38

迭代者很懒。副作用(迭代器)和惰性不能混为一谈。也许这会做正确的事:

implicit val iteratorEnumerator = new Enumerator[Iterator] {
  def apply[E,A](iter: Iterator[E], i: IterV[E,A]): IterV[E,A] =
    iter.foldRight(i)((x, y) =>
      y.fold(done = (acc, input) => y,
             cont = k => apply(iter, k(El(x))))
    )
}

话又说回来,也许不会。只有foldRight的来源才知道。副作用就是这样。

Iteratees are lazy. Side-effects (iterator) and laziness do not mix. Maybe this will do the right thing:

implicit val iteratorEnumerator = new Enumerator[Iterator] {
  def apply[E,A](iter: Iterator[E], i: IterV[E,A]): IterV[E,A] =
    iter.foldRight(i)((x, y) =>
      y.fold(done = (acc, input) => y,
             cont = k => apply(iter, k(El(x))))
    )
}

Then again, maybe not. Only the source of foldRight will know. Side-effects are like that.

鸢与 2024-10-17 08:53:38

您可以通过创建大小为一的一次性迭代器来避免副作用:

implicit val iteratorEnumerator = new Enumerator[Iterator] {
  def apply[E,A](iter: Iterator[E], i: IterV[E,A]): IterV[E,A] =
    if (iter.isEmpty) i
    else i.fold(done = (acc,input) => i, 
                cont = k => apply(iter, k(El(iter.take(1).next))))
}

You can avoid side effects by creating a throwaway iterator of size one:

implicit val iteratorEnumerator = new Enumerator[Iterator] {
  def apply[E,A](iter: Iterator[E], i: IterV[E,A]): IterV[E,A] =
    if (iter.isEmpty) i
    else i.fold(done = (acc,input) => i, 
                cont = k => apply(iter, k(El(iter.take(1).next))))
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文