scala迭代器中的scalaz迭代器

scala迭代器中的scalaz迭代器,scala,scalaz,Scala,Scalaz,我编辑了下面的代码,因为我认为我在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])

我编辑了下面的代码,因为我认为我在
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
我期待的是
(一些(1),一些(1))
(一些(1),一些(1),一些(2))

我怀疑这与iter有关。下一步是副作用。最好的办法是什么

为了进行比较,直接从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))))
  }
}

通过创建大小为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))))
}

关于国际热核聚变实验堆,你说得对。下一个会引起副作用。我认为问题可以归结为流和迭代器之间的区别。有相关信息。

我想我知道了。这似乎主要是由于
El
使用了一个按名称参数,该参数重新计算
iter.next
——以及我最初如何使用两个不同的
peek(iter.run)错误地调用计算。我重写了枚举数,首先将
iter.next
赋值给一个val(并在这个过程中使其尾部递归):

然后:


迭代者是懒惰的。副作用(迭代器)和懒惰不会混合在一起。也许这样做是对的:

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

一开始似乎是有效的,但后来我认为迭代器从来没有真正像
def firstTwo[E]:IterV[E,(Option[E],Option[E])]=for(a)那样移动过
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
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))))
    )
}