Trait/Object

com.twitter.concurrent

Spool

Related Docs: object Spool | package concurrent

Permalink

sealed trait Spool[+A] extends AnyRef

Note: Spool is no longer the recommended asynchronous stream abstraction. We encourage you to use AsyncStream instead.

A spool is an asynchronous stream. It more or less mimics the scala {{Stream}} collection, but with cons cells that have either eager or deferred tails.

Construction of eager Spools is done with either Spool.cons or the {{**::}} operator. To construct a lazy/deferred Spool which materializes its tail on demand, use the {{*::}} operator. In order to use these operators for deconstruction, they must be imported explicitly (ie: {{import Spool.{*::, **:: }}} )

def fill(rest: Promise[Spool[Int]]) {
  asyncProcess foreach { result =>
    if (result.last) {
      rest() = Return(result **:: Spool.empty)
    } else {
      val next = new Promise[Spool[Int]]
      rest() = Return(result *:: next)
      fill(next)
    }
  }
}
val rest = new Promise[Spool[Int]]
fill(rest)
firstElem *:: rest

Note: There is a Java-friendly API for this trait: com.twitter.concurrent.AbstractSpool.

explicitly (ie: {{import Spool.{*::, **:: }}}

def fill(rest: Promise[Spool[Int]]) {
  asyncProcess foreach { result =>
    if (result.last) {
      rest() = Return(result **:: Spool.empty)
    } else {
      val next = new Promise[Spool[Int]]
      rest() = Return(result *:: next)
      fill(next)
    }
  }
}
val rest = new Promise[Spool[Int]]
fill(rest)
firstElem *:: rest

Note: There is a Java-friendly API for this trait: com.twitter.concurrent.AbstractSpool.

Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Spool
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def head: A

    Permalink

    The first element of the spool.

    The first element of the spool. Invalid for empty spools.

  2. abstract def isEmpty: Boolean

    Permalink
  3. abstract def tail: Future[Spool[A]]

    Permalink

    The (deferred) tail of the spool.

    The (deferred) tail of the spool. Invalid for empty spools.

Concrete Value Members

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

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

    Permalink
    Definition Classes
    AnyRef → Any
  3. def ++[B >: A](that: ⇒ Future[Spool[B]]): Future[Spool[B]]

    Permalink

    Concatenates two spools.

  4. def ++[B >: A](that: ⇒ Spool[B]): Spool[B]

    Permalink

    Concatenates two spools.

  5. final def ==(arg0: Any): Boolean

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

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

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. def collect[B](f: PartialFunction[A, B]): Future[Spool[B]]

    Permalink

    The standard Scala collect, in order to implement map & filter.

    The standard Scala collect, in order to implement map & filter.

    It may seem unnatural to return a Future[…] here, but we cannot know whether the first element exists until we have applied its filter.

  9. def concat[B >: A](that: Future[Spool[B]]): Future[Spool[B]]

    Permalink

    See also

    operator ++

  10. def concat[B >: A](that: Spool[B]): Spool[B]

    Permalink

    See also

    operator ++

  11. def distinctBy[B](fn: (A) ⇒ B): Spool[A]

    Permalink

    Builds a new Spool from this one by filtering out duplicate elements, elements for which fn returns the same value.

    Builds a new Spool from this one by filtering out duplicate elements, elements for which fn returns the same value.

    NB: this has space consumption O(N) of the number of distinct items

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

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

    Permalink
    Definition Classes
    AnyRef → Any
  14. def filter(f: (A) ⇒ Boolean): Future[Spool[A]]

    Permalink
  15. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  16. def flatMap[B](f: (A) ⇒ Future[Spool[B]]): Future[Spool[B]]

    Permalink

    Applies a function that generates a spool for each element in this spool, flattening the result into a single spool.

  17. def foldLeft[B](z: B)(f: (B, A) ⇒ B): Future[B]

    Permalink
  18. def force: Future[Unit]

    Permalink

    Eagerly executes all computation represented by this Spool (presumably for side-effects), and returns a Future representing its completion.

  19. def foreach[B](f: (A) ⇒ B): Future[Unit]

    Permalink

    Apply {{f}} for each item in the spool, until the end.

    Apply {{f}} for each item in the spool, until the end. {{f}} is applied as the items become available.

  20. def foreachElem[B](f: (Option[A]) ⇒ B): Future[Unit]

    Permalink

    A version of {{foreach}} that wraps each element in an {{Option}}, terminating the stream (EOF) with {{None}}.

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

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

    Permalink
    Definition Classes
    AnyRef → Any
  23. def headOption: Option[A]

    Permalink

    The first element of the spool if it is non-empty.

  24. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  25. def map[B](f: (A) ⇒ B): Spool[B]

    Permalink
  26. def mapFuture[B](f: (A) ⇒ Future[B]): Future[Spool[B]]

    Permalink

    Applies a function that generates a Future[B] for each element of this spool.

    Applies a function that generates a Future[B] for each element of this spool. The returned future is satisfied when the head of the resulting spool is available.

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

    Permalink
    Definition Classes
    AnyRef
  28. final def notify(): Unit

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

    Permalink
    Definition Classes
    AnyRef
  30. def reduceLeft[B >: A](f: (B, A) ⇒ B): Future[B]

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

    Permalink
    Definition Classes
    AnyRef
  32. def take(n: Int): Spool[A]

    Permalink

    Take the first n elements of the Spool as another Spool (adapted from Stream.take)

  33. def takeWhile(f: (A) ⇒ Boolean): Spool[A]

    Permalink

    Take elements from the head of the Spool (lazily), while the given condition is true.

  34. def toSeq: Future[Seq[A]]

    Permalink

    Fully buffer the spool to a {{Seq}}.

    Fully buffer the spool to a {{Seq}}. The returned future is satisfied when the entire result is ready.

  35. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  36. final def wait(): Unit

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

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  39. def zip[B](that: Spool[B]): Spool[(A, B)]

    Permalink

    Zips two Spools returning a Spool of Tuple2s.

    Zips two Spools returning a Spool of Tuple2s.

    If one Spool is shorter, excess elements of the longer Spool are discarded.

    c.f. scala.collection.immutable.Stream#zip

Inherited from AnyRef

Inherited from Any

Ungrouped