Class/Object

cats.data

Streaming

Related Docs: object Streaming | package data

Permalink

sealed abstract class Streaming[A] extends AnyRef

Streaming[A] represents a stream of values. A stream can be thought of as a collection, with two key differences:

  1. It may be infinite; it does not necessarily have a finite length. For this reason, there is no .length method.

2. It may be lazy. In other words, the entire stream may not be in memory. In this case, each "step" of the stream has instructions for producing the next step.

Streams are not necessarily lazy: they use Eval[Streaming[A]] to represent a tail that may (or may not be) lazy. If Now[A] is used for each tail, then Streaming[A] will behave similarly to List[A]. If Later[A] is used for each tail, then Streaming[A] will behave similarly to scala.Stream[A] (i.e. it will lazily-compute the tail, and will memoize the result to improve the performance of repeated traversals). If Always[A] is used for each tail, the result will be a lazy stream which does not memoize results (saving space at the cost of potentially-repeated calculations).

Since Streaming[A] has been compared to scala.Stream[A] it is worth noting some key differences between the two types:

  1. When the entire stream is known ahead of time, Streaming[A] can represent it more efficiencly, using Now[A], rather than allocating a list of closures.

2. Streaming[A] does not memoize by default. This protects against cases where a reference to head will prevent the entire stream from being garbage collected, and is a better default. A stream can be memoized later using the .memoize method.

3. Streaming[A] does not inherit from the standard collections, meaning a wide variety of methods which are dangerous on streams (.length, .apply, etc.) are not present.

4. scala.Stream[A] requires an immediate value for .head. This means that operations like .filter will block until a matching value is found, or the stream is exhausted (which could be never in the case of an infinite stream). By contrast, Streaming[A] values can be totally lazy (and can be lazily-constructed using Streaming.defer()), so methods like .filter are completely lazy.

5. The use of Eval[Streaming[A]] to represent the "tail" of the stream means that streams can be lazily (and safely) constructed with Foldable#foldRight, and that .map and .flatMap operations over the tail will be safely trampolined.

Self Type
Streaming[A]
Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Streaming
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. def compact: Streaming[A]

    Permalink

    Compact removes "pauses" in the stream (represented as Wait(_) nodes).

    Compact removes "pauses" in the stream (represented as Wait(_) nodes).

    Normally, Wait(_) values are used to defer tail computation in cases where it is convenient to return a stream value where neither the head or tail are computed yet.

    In some cases (particularly if the stream is to be memoized) it may be desirable to ensure that these values are not retained.

  7. def concat(rhs: Eval[Streaming[A]]): Streaming[A]

    Permalink

    Lazily concatenate two streams.

    Lazily concatenate two streams.

    In this case the evaluation of the second stream may be deferred.

  8. def concat(rhs: Streaming[A]): Streaming[A]

    Permalink

    Lazily concatenate two streams.

  9. def drop(n: Int): Streaming[A]

    Permalink

    Return a stream consisting of all but the first n elements of this stream.

    Return a stream consisting of all but the first n elements of this stream.

    If the current stream has n or fewer elements, an empty stream will be returned.

  10. def dropWhile(f: (A) ⇒ Boolean): Streaming[A]

    Permalink

    From the beginning of this stream, create a new stream which removes all elements that fulfill the predicate f.

    From the beginning of this stream, create a new stream which removes all elements that fulfill the predicate f. Once an element is found which does not fulfill the predicate, that element and all subsequent elements will be returned.

    If all elements satisfy f, an empty stream will be returned. If no elements satisfy f, the current stream will be returned.

    For example:

    Streaming(1, 2, 3, 4, 5, 6, 7).takeWhile(n => n != 4)

    Will result in: Streaming(4, 5, 6, 7)

  11. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  12. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  13. def exists(f: (A) ⇒ Boolean): Boolean

    Permalink

    Return true if some element of the stream satisfies the predicate, false otherwise.

  14. def filter(f: (A) ⇒ Boolean): Streaming[A]

    Permalink

    Lazily filter the stream given the predicate f.

  15. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  16. def find(f: (A) ⇒ Boolean): Option[A]

    Permalink

    Eagerly search the stream from the left.

    Eagerly search the stream from the left. The search ends when f returns true for some a, or the stream is exhausted. Some(a) signals a match, None means no matching items were found.

  17. def flatMap[B](f: (A) ⇒ Streaming[B]): Streaming[B]

    Permalink

    Lazily transform the stream given a function f.

  18. def fold[B](b: Eval[B], f: (A, Eval[Streaming[A]]) ⇒ B): B

    Permalink

    The stream's catamorphism.

    The stream's catamorphism.

    This method allows the stream to be transformed into an arbitrary value by handling two cases:

    1. empty stream: return b 2. non-empty stream: apply the function to the head and tail

    This method can be more convenient than pattern-matching, since it includes support for handling deferred streams (i.e. Wait(_)), these nodes will be evaluated until an empty or non-empty stream is found (i.e. until Empty() or Cons() is found).

  19. def foldLeft[B](b: B)(f: (B, A) ⇒ B): B

    Permalink

    Eagerly fold the stream to a single value from the left.

  20. def foldRight[B](b: Eval[B])(f: (A, Eval[B]) ⇒ Eval[B]): Eval[B]

    Permalink

    Lazily fold the stream to a single value from the right.

  21. def foldStreaming[B](bs: ⇒ Streaming[B], f: (A, Eval[Streaming[A]]) ⇒ Streaming[B]): Streaming[B]

    Permalink

    A variant of fold, used for constructing streams.

    A variant of fold, used for constructing streams.

    The only difference is that foldStream will preserve deferred streams. This makes it more appropriate to use in situations where the stream's laziness must be preserved.

  22. def forall(f: (A) ⇒ Boolean): Boolean

    Permalink

    Return true if every element of the stream satisfies the predicate, false otherwise.

  23. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  24. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  25. def interleave(rhs: Streaming[A]): Streaming[A]

    Permalink

    Interleave the elements of two streams.

    Interleave the elements of two streams.

    Given x = [x0, x1, x2, ...] and y = [y0, y1, y2, ...] this method will return the stream [x0, y0, x1, y1, x2, ...]

    If one stream is longer than the other, the rest of its elements will appear after the other stream is exhausted.

  26. def isEmpty: Boolean

    Permalink

    Return true if the stream is empty, false otherwise.

    Return true if the stream is empty, false otherwise.

    In this case of deferred streams this will force the first element to be calculated.

  27. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  28. def iterator: Iterator[A]

    Permalink

    Provide an iterator over the elements in the stream.

  29. def izip[B](rhs: Streaming[B]): Streaming[Ior[A, B]]

    Permalink

    Lazily zip two streams together using Ior.

    Lazily zip two streams together using Ior.

    Unlike zip, the length of the result will be the longer of the two arguments.

  30. def izipMap[B, C](rhs: Streaming[B])(f: (A, B) ⇒ C, g: (A) ⇒ C, h: (B) ⇒ C): Streaming[C]

    Permalink

    Zip two streams together, using the given function f to produce the output values.

    Zip two streams together, using the given function f to produce the output values.

    Unlike zipMap, the length of the result will be the *longer* of the two input streams. The functions g and h will be used in this case to produce valid C values.

    The expression:

    (lhs izipMap rhs)(f, g, h)

    is equivalent to (but more efficient than):

    (lhs izip rhs).map { case Ior.Both(a, b) => f(a, b) case Ior.Left(a) => g(a) case Ior.Right(b) => h(b) }

  31. def map[B](f: (A) ⇒ B): Streaming[B]

    Permalink

    Lazily transform the stream given a function f.

  32. def memoize: Streaming[A]

    Permalink

    Ensure that repeated traversals of the stream will not cause repeated tail computations.

    Ensure that repeated traversals of the stream will not cause repeated tail computations.

    By default stream does not memoize to avoid memory leaks when the head of the stream is retained.

  33. def merge(rhs: Streaming[A])(implicit ev: Order[A]): Streaming[A]

    Permalink

    Merge two sorted streams into a new stream.

    Merge two sorted streams into a new stream.

    The streams are assumed to already be sorted. If they are not, the resulting order is not defined.

  34. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  35. def nonEmpty: Boolean

    Permalink

    Return true if the stream is non-empty, false otherwise.

    Return true if the stream is non-empty, false otherwise.

    In this case of deferred streams this will force the first element to be calculated.

  36. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  37. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  38. def peekEmpty: Option[Boolean]

    Permalink

    Peek at the start of the stream to see whether we know if it is empty.

    Peek at the start of the stream to see whether we know if it is empty.

    Unlike .isEmpty/.nonEmpty, this method will not force the calculationg of a deferred stream. Instead, None will be returned.

  39. def product[B](rhs: Streaming[B]): Streaming[(A, B)]

    Permalink

    Produce the Cartestian product of two streams.

    Produce the Cartestian product of two streams.

    Given x = [x0, x1, x2, ...] and y = [y0, y1, y2, ...] this method will return the stream:

    [(x0, y0), (x0, y1), (x1, y0), (x0, y2), (x1, y1), (x2, y0), ...]

    This is the diagonalized product of both streams. Every possible combination will (eventually) be reached.

    This is true even for infinite streams, at least in theory -- time and space limitations of evaluating an infinite stream may make it impossible to reach very distant elements.

    This method lazily evaluates the input streams, but due to the diagonalization method may read ahead more than is strictly necessary.

  40. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  41. def tails: Streaming[Streaming[A]]

    Permalink

    Provide a stream of all the tails of a stream (including itself).

    Provide a stream of all the tails of a stream (including itself).

    For example, Streaming(1, 2).tails is equivalent to:

    Streaming(Streaming(1, 2), Streaming(1), Streaming.empty)

  42. def take(n: Int): Streaming[A]

    Permalink

    Return a stream consisting only of the first n elements of this stream.

    Return a stream consisting only of the first n elements of this stream.

    If the current stream has n or fewer elements, the entire stream will be returned.

  43. def takeWhile(f: (A) ⇒ Boolean): Streaming[A]

    Permalink

    From the beginning of this stream, create a new stream which takes only those elements that fulfill the predicate f.

    From the beginning of this stream, create a new stream which takes only those elements that fulfill the predicate f. Once an element is found which does not fulfill the predicate, no further elements will be returned.

    If all elements satisfy f, the current stream will be returned. If no elements satisfy f, an empty stream will be returned.

    For example:

    Streaming(1, 2, 3, 4, 5, 6, 7).takeWhile(n => n != 4)

    Will result in: Streaming(1, 2, 3)

  44. def toArray(implicit ct: ClassTag[A]): Array[A]

    Permalink

    Provide an array of elements in the stream.

    Provide an array of elements in the stream.

    This will evaluate the stream immediately, and will hang in the case of infinite streams.

  45. def toList: List[A]

    Permalink

    Provide a list of elements in the stream.

    Provide a list of elements in the stream.

    This will evaluate the stream immediately, and will hang in the case of infinite streams.

  46. def toString(limit: Int = 10): String

    Permalink

    String representation of the first n elements of a stream.

  47. def toString(): String

    Permalink

    Basic string representation of a stream.

    Basic string representation of a stream.

    This method will not force evaluation of any lazy part of a stream. As a result, you will see at most one element (the first one).

    Use .toString(n) to see the first n elements of the stream.

    Definition Classes
    Streaming → AnyRef → Any
  48. def uncons: Option[(A, Eval[Streaming[A]])]

    Permalink

    Deconstruct a stream into a head and tail (if available).

    Deconstruct a stream into a head and tail (if available).

    This method will evaluate the stream until it finds a head and tail, or until the stream is exhausted. The head will be evaluated, whereas the tail will remain (potentially) lazy within Eval.

  49. def unzip[B, C](implicit ev: =:=[A, (B, C)]): (Streaming[B], Streaming[C])

    Permalink

    Unzip this stream of tuples into two distinct streams.

  50. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  51. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  52. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  53. def zip[B](rhs: Streaming[B]): Streaming[(A, B)]

    Permalink

    Lazily zip two streams together.

    Lazily zip two streams together.

    The length of the result will be the shorter of the two arguments.

  54. def zipMap[B, C](rhs: Streaming[B])(f: (A, B) ⇒ C): Streaming[C]

    Permalink

    Lazily zip two streams together, using the given function f to produce output values.

    Lazily zip two streams together, using the given function f to produce output values.

    The length of the result will be the shorter of the two arguments.

    The expression:

    (lhs zipMap rhs)(f)

    is equivalent to (but more efficient than):

    (lhs zip rhs).map { case (a, b) => f(a, b) }

  55. def zipWithIndex: Streaming[(A, Int)]

    Permalink

    Zip the items of the stream with their position.

    Zip the items of the stream with their position.

    Indices start at 0, so

    Streaming('x, 'y, 'z).zipWithIndex

    lazily produces:

    Streaming(('x, 0), ('y, 1), ('z, 2))

Inherited from AnyRef

Inherited from Any

Ungrouped