【问题标题】:scalaz Iteratees from scala Iteratorscalaz Iteratees 来自 scala Iterator
【发布时间】:2011-06-02 04:45:46
【问题描述】:

我编辑了下面的代码,因为我认为我在 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 web site 的源代码示例的this 可以正常工作:

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))))
  }
}

【问题讨论】:

    标签: scala scalaz


    【解决方案1】:

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

    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 的来源会知道。副作用就是这样。

    【讨论】:

      【解决方案2】:

      我想我想通了。这似乎主要是由于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
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

          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))))
          }
          

          【讨论】:

          • 起初似乎可行,但后来我认为迭代器永远不会真正移动,因为def firstTwo[E]: IterV[E, (Option[E],Option[E])] = for (a &lt;- head; b &lt;- head) yield (a,b) 返回(Some(1), Some(1))
          猜你喜欢
          • 2013-08-09
          • 2010-10-14
          • 2018-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-03
          • 1970-01-01
          相关资源
          最近更新 更多