Guest User

Untitled

a guest
Feb 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.60 KB | None | 0 0
  1. object Stream extends SeqFactory[Stream] {
  2.  
  3.   /** The factory for streams.
  4.    *  @note Methods such as map/flatMap will not invoke the Builder factory,
  5.    *        but will return a new stream directly, to preserve laziness.
  6.    *        The new stream is then cast to the factory's result type.
  7.    *        This means that every CanBuildFrom that takes a
  8.    *        Stream as its From type parameter must yield a stream as its result parameter.
  9.    *        If that assumption is broken, cast errors might result.
  10.    */
  11.   class StreamCanBuildFrom[A] extends GenericCanBuildFrom[A]
  12.  
  13.   implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, Stream[A]] = new StreamCanBuildFrom[A]
  14.  
  15.   /** Creates a new builder for a stream */
  16.   def newBuilder[A]: Builder[A, Stream[A]] = new StreamBuilder[A]
  17.  
  18.   import scala.collection.{Iterable, Seq, IndexedSeq}
  19.  
  20.   /** A builder for streams
  21.    *  @note This builder is lazy only in the sense that it does not go downs the spine
  22.    *        of traversables that are added as a whole. If more laziness can be achieved,
  23.    *        this builder should be bypassed.
  24.    */
  25.   class StreamBuilder[A] extends scala.collection.mutable.LazyBuilder[A, Stream[A]] {
  26.     def result: Stream[A] = parts.toStream flatMap (_.toStream)
  27.   }
  28.  
  29.   object Empty extends Stream[Nothing] {
  30.     override def isEmpty = true
  31.     override def head = throw new NoSuchElementException("head of empty stream")
  32.     override def tail = throw new UnsupportedOperationException("tail of empty stream")
  33.     def tailDefined = false
  34.   }
  35.  
  36.   /** The empty stream */
  37.   override def empty[A]: Stream[A] = Empty
  38. }
Add Comment
Please, Sign In to add comment