fork download
  1. import Stream._
  2. trait Stream[+A]
  3.  
  4. case object Empty extends Stream[Nothing]
  5. case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
  6.  
  7. object Stream {
  8.  
  9. def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = {
  10. lazy val head = hd
  11. lazy val tail = tl Cons(() => head, () => tail)
  12. }
  13.  
  14. def empty[A]: Stream[A] = Empty
  15.  
  16. def apply[A](as: A*): Stream[A] =
  17. if (as.isEmpty) empty
  18. else cons(as.head, apply(as.tail: _*))
  19.  
  20. def toListFast: List[A] = {
  21. val buf = new collection.mutable.ListBuffer[A]
  22.  
  23. @annotation.tailrec
  24. def go(s: Stream[A]): List[A] = s match {
  25. case Cons(h,t) => buf += h() go(t())
  26. case _ => buf.toList
  27. }
  28. go(this)
  29. }
  30.  
  31. def take(n: Int): Stream[A] =
  32. if (n > 0) this match {
  33. case Cons(h, t) if n == 1 => cons(h(), Stream.empty) // we can say Stream.empty
  34. case Cons(h, t) => cons(h(), t().take(n-1))
  35. case _ => Stream.empty
  36. } else Stream()
  37.  
  38. def constant[A](a: A): Stream[A] = {
  39. lazy val tail: Stream[A] = Cons(() => a, () => tail)
  40. tail
  41. }
  42. }
  43.  
  44. object Main extends App {
  45. println(constant(5).take(3).toList)
  46. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/opt/scala/bin/scalac: line 50: /dev/null: Permission denied
Main.scala:11: error: value Cons is not a member of Stream[A]
    lazy val tail = tl Cons(() => head, () => tail) 
                       ^
Main.scala:12: error: type mismatch;
 found   : Unit
 required: Stream[A]
  } 
  ^
Main.scala:20: error: not found: type A
  def toListFast: List[A] = {
                       ^
Main.scala:21: error: not found: type A
    val buf = new collection.mutable.ListBuffer[A] 
                                                ^
Main.scala:24: error: not found: type A
    def go(s: Stream[A]): List[A] = s match { 
                               ^
Main.scala:24: error: not found: type A
    def go(s: Stream[A]): List[A] = s match { 
                     ^
Main.scala:31: error: not found: type A
  def take(n: Int): Stream[A] = 
                           ^
Main.scala:33: error: constructor cannot be instantiated to expected type;
 found   : Cons[A]
 required: Stream.type
      case Cons(h, t) if n == 1 => cons(h(), Stream.empty) // we can say Stream.empty 
           ^
Main.scala:33: error: not found: value h
      case Cons(h, t) if n == 1 => cons(h(), Stream.empty) // we can say Stream.empty 
                                        ^
Main.scala:34: error: constructor cannot be instantiated to expected type;
 found   : Cons[A]
 required: Stream.type
      case Cons(h, t) => cons(h(), t().take(n-1)) 
           ^
Main.scala:34: error: not found: value h
      case Cons(h, t) => cons(h(), t().take(n-1)) 
                              ^
Main.scala:34: error: not found: value t
      case Cons(h, t) => cons(h(), t().take(n-1)) 
                                   ^
Main.scala:45: error: value take is not a member of Stream[Int]
  println(constant(5).take(3).toList)
                      ^
13 errors found
spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
stdout
Standard output is empty