私はマニングの「Scalaでは関数型プログラミング」を通じて作業し、ストリームに問題があり、これはファイルですよ:ScalaのストリームメソッドTakeWhile
package chapter05
sealed trait Stream[+A]{
case object Empty extends Stream[Nothing]
case class Cons[+A](h:() => A, t:() => Stream[A]) extends Stream[A]
def headOption: A = this match {
case Empty => throw new Exception("optional")
case Cons(h, t) => h()
}
def toList: List[A] = this match {
case Cons(h, t) => h() :: t().toList
case Empty => Nil
}
def takeWhile1(p: A => Boolean): Stream[A] =
this match {
case Cons(h, t) if (p(h())) => Stream.cons(h(), t().takeWhile1(p))
case _ => Empty
}
object Stream {
def cons[A](hd: => A, tl : => Stream[A]): Stream[A] = {
lazy val head = hd
lazy val tail = tl
Cons(() => head,() => tail)
}
def empty[A]: Stream[A] = Empty
def apply[A](as: A*): Stream[A] =
if (as.isEmpty) empty else cons(as.head, apply(as.tail:_*))
}
}
適用し、takeWhileは私にはない、コンパイルされません。なぜ、それの論理が正しいと思われるのか知っている(適用は本から取られる)。
私は、少なくとも一つの{欠けがありますが、いくつかの '}' – pedrofurla
が欠けていると思います。 – stholzm
Eclipseは大括弧( "{"、 "}")について不平を言っていません –