Packages

c

fs2.Stream

CompileOps

final class CompileOps[F[_], G[_], O] extends AnyRef

Projection of a Stream providing various ways to compile a Stream[F,O] to an F[...].

Source
Stream.scala
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. CompileOps
  2. AnyRef
  3. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. def +(other: String): String
    Implicit
    This member is added by an implicit conversion from CompileOps[F, G, O] to any2stringadd[CompileOps[F, G, O]] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (CompileOps[F, G, O], B)
    Implicit
    This member is added by an implicit conversion from CompileOps[F, G, O] to ArrowAssoc[CompileOps[F, G, O]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  5. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  8. def drain: G[Unit]

    Compiles this stream in to a value of the target effect type F and discards any output values of the stream.

    Compiles this stream in to a value of the target effect type F and discards any output values of the stream.

    To access the output values of the stream, use one of the other compilation methods -- e.g., fold, toVector, etc.

  9. def ensuring(cond: (CompileOps[F, G, O]) ⇒ Boolean, msg: ⇒ Any): CompileOps[F, G, O]
    Implicit
    This member is added by an implicit conversion from CompileOps[F, G, O] to Ensuring[CompileOps[F, G, O]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  10. def ensuring(cond: (CompileOps[F, G, O]) ⇒ Boolean): CompileOps[F, G, O]
    Implicit
    This member is added by an implicit conversion from CompileOps[F, G, O] to Ensuring[CompileOps[F, G, O]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  11. def ensuring(cond: Boolean, msg: ⇒ Any): CompileOps[F, G, O]
    Implicit
    This member is added by an implicit conversion from CompileOps[F, G, O] to Ensuring[CompileOps[F, G, O]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  12. def ensuring(cond: Boolean): CompileOps[F, G, O]
    Implicit
    This member is added by an implicit conversion from CompileOps[F, G, O] to Ensuring[CompileOps[F, G, O]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  13. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  14. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  15. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  16. def fold[B](init: B)(f: (B, O) ⇒ B): G[B]

    Compiles this stream in to a value of the target effect type F by folding the output values together, starting with the provided init and combining the current value with each output value.

  17. def foldChunks[B](init: B)(f: (B, Chunk[O]) ⇒ B): G[B]

    Compiles this stream in to a value of the target effect type F by folding the output chunks together, starting with the provided init and combining the current value with each output chunk.

    Compiles this stream in to a value of the target effect type F by folding the output chunks together, starting with the provided init and combining the current value with each output chunk.

    When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.

  18. def foldMonoid(implicit O: Monoid[O]): G[O]

    Like fold but uses the implicitly available Monoid[O] to combine elements.

    Like fold but uses the implicitly available Monoid[O] to combine elements.

    Example:
    1. scala> import cats.implicits._, cats.effect.IO
      scala> Stream(1, 2, 3, 4, 5).covary[IO].compile.foldMonoid.unsafeRunSync
      res0: Int = 15
  19. def foldSemigroup(implicit O: Semigroup[O]): G[Option[O]]

    Like fold but uses the implicitly available Semigroup[O] to combine elements.

    Like fold but uses the implicitly available Semigroup[O] to combine elements. If the stream emits no elements, None is returned.

    Example:
    1. scala> import cats.implicits._, cats.effect.IO
      scala> Stream(1, 2, 3, 4, 5).covary[IO].compile.foldSemigroup.unsafeRunSync
      res0: Option[Int] = Some(15)
      scala> Stream.empty.covaryAll[IO, Int].compile.foldSemigroup.unsafeRunSync
      res1: Option[Int] = None
  20. def formatted(fmtstr: String): String
    Implicit
    This member is added by an implicit conversion from CompileOps[F, G, O] to StringFormat[CompileOps[F, G, O]] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  21. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  22. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  23. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  24. def last: G[Option[O]]

    Compiles this stream in to a value of the target effect type F, returning None if the stream emitted no values and returning the last value emitted wrapped in Some if values were emitted.

    Compiles this stream in to a value of the target effect type F, returning None if the stream emitted no values and returning the last value emitted wrapped in Some if values were emitted.

    When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.

    Example:
    1. scala> import cats.effect.IO
      scala> Stream.range(0,100).take(5).covary[IO].compile.last.unsafeRunSync
      res0: Option[Int] = Some(4)
  25. def lastOrError(implicit G: MonadError[G, Throwable]): G[O]

    Compiles this stream in to a value of the target effect type F, raising a NoSuchElementException if the stream emitted no values and returning the last value emitted otherwise.

    Compiles this stream in to a value of the target effect type F, raising a NoSuchElementException if the stream emitted no values and returning the last value emitted otherwise.

    When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.

    Example:
    1. scala> import cats.effect.IO
      scala> Stream.range(0,100).take(5).covary[IO].compile.lastOrError.unsafeRunSync
      res0: Int = 4
      scala> Stream.empty.covaryAll[IO, Int].compile.lastOrError.attempt.unsafeRunSync
      res1: Either[Throwable, Int] = Left(java.util.NoSuchElementException)
  26. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  27. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  28. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  29. def resource(implicit compiler: Compiler[G, [β$28$]Resource[G, β$28$]]): CompileOps[G, [β$29$]Resource[G, β$29$], O]

    Gives access to the whole compilation api, where the result is expressed as a cats.effect.Resource, instead of bare F.

    Gives access to the whole compilation api, where the result is expressed as a cats.effect.Resource, instead of bare F.

    import fs2._
    import cats.effect._
    import cats.implicits._
    
    val stream = Stream.iterate(0)(_ + 1).take(5).covary[IO]
    
    val s1: Resource[IO, List[Int]] = stream.compile.resource.toList
    val s2: Resource[IO, Int] = stream.compile.resource.foldMonoid
    val s3: Resource[IO, Option[Int]] = stream.compile.resource.last

    And so on for every other method in compile.

    The main use case is interacting with Stream methods whose behaviour depends on the Stream lifetime, in cases where you only want to ultimately return a single element.

    A typical example of this is concurrent combinators, here is an example with concurrently:

    import fs2._
    import cats.effect._
    import cats.effect.concurrent.Ref
    import scala.concurrent.duration._
    
    trait StopWatch[F[_]] {
      def elapsedSeconds: F[Int]
    }
    object StopWatch {
      def create[F[_]: Concurrent: Timer]: Stream[F, StopWatch[F]] =
        Stream.eval(Ref[F].of(0)).flatMap { c =>
          val api = new StopWatch[F] {
            def elapsedSeconds: F[Int] = c.get
          }
    
          val process = Stream.fixedRate(1.second).evalMap(_ => c.update(_ + 1))
    
          Stream.emit(api).concurrently(process)
      }
    }

    This creates a simple abstraction that can be queried by multiple consumers to find out how much time has passed, with a concurrent stream to update it every second.

    Note that create returns a Stream[F, StopWatch[F]], even though there is only one instance being emitted: this is less than ideal, so we might think about returning an F[StopWatch[F]] with the following code

    StopWatch.create[F].compile.lastOrError

    but it does not work: the returned F terminates the lifetime of the stream, which causes concurrently to stop the process stream. As a result, elapsedSeconds never gets updated.

    Alternatively, we could implement StopWatch in F only using Fiber.start, but this is not ideal either: concurrently already handles errors, interruption and stopping the producer stream once the consumer lifetime is over, and we don't want to reimplement the machinery for that.

    So basically what we need is a type that expresses the concept of lifetime, while only ever emitting a single element, which is exactly what cats.effect.Resource does.

    What compile.resource provides is the ability to do this:

    object StopWatch {
      // ... def create as before ...
    
      def betterCreate[F[_]: Concurrent: Timer]: Resource[F, StopWatch[F]] =
        create.compile.resource.lastOrError
    }

    This works for every other compile. method, although it's a very natural fit with lastOrError.

  30. def string(implicit ev: <:<[O, String]): G[String]

    Compiles this stream of strings in to a single string.

    Compiles this stream of strings in to a single string. This is more efficient than foldMonoid because it uses a StringBuilder internally, minimizing string creation.

    Example:
    1. scala> Stream("Hello ", "world!").compile.string
      res0: String = Hello world!
  31. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  32. def to[C[_]](implicit f: scala.collection.compat.Factory[O, C[O]]): G[C[O]]

    Compiles this stream into a value of the target effect type F by logging the output values to a C, given a Factory.

    Compiles this stream into a value of the target effect type F by logging the output values to a C, given a Factory.

    When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.

    Example:
    1. scala> import cats.effect.IO
      scala> Stream.range(0,100).take(5).covary[IO].compile.toList.unsafeRunSync
      res0: List[Int] = List(0, 1, 2, 3, 4)
  33. def toChunk: G[Chunk[O]]

    Compiles this stream in to a value of the target effect type F by logging the output values to a Chunk.

    Compiles this stream in to a value of the target effect type F by logging the output values to a Chunk.

    When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.

    Example:
    1. scala> import cats.effect.IO
      scala> Stream.range(0,100).take(5).covary[IO].compile.toChunk.unsafeRunSync
      res0: Chunk[Int] = Chunk(0, 1, 2, 3, 4)
  34. def toList: G[List[O]]

    Compiles this stream in to a value of the target effect type F by logging the output values to a List.

    Compiles this stream in to a value of the target effect type F by logging the output values to a List. Equivalent to to[List].

    When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.

    Example:
    1. scala> import cats.effect.IO
      scala> Stream.range(0,100).take(5).covary[IO].compile.toList.unsafeRunSync
      res0: List[Int] = List(0, 1, 2, 3, 4)
  35. def toString(): String
    Definition Classes
    AnyRef → Any
  36. def toVector: G[Vector[O]]

    Compiles this stream in to a value of the target effect type F by logging the output values to a Vector.

    Compiles this stream in to a value of the target effect type F by logging the output values to a Vector. Equivalent to to[Vector].

    When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.

    Example:
    1. scala> import cats.effect.IO
      scala> Stream.range(0,100).take(5).covary[IO].compile.toVector.unsafeRunSync
      res0: Vector[Int] = Vector(0, 1, 2, 3, 4)
  37. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  38. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  39. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  40. def [B](y: B): (CompileOps[F, G, O], B)
    Implicit
    This member is added by an implicit conversion from CompileOps[F, G, O] to ArrowAssoc[CompileOps[F, G, O]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion any2stringadd from CompileOps[F, G, O] to any2stringadd[CompileOps[F, G, O]]

Inherited by implicit conversion StringFormat from CompileOps[F, G, O] to StringFormat[CompileOps[F, G, O]]

Inherited by implicit conversion Ensuring from CompileOps[F, G, O] to Ensuring[CompileOps[F, G, O]]

Inherited by implicit conversion ArrowAssoc from CompileOps[F, G, O] to ArrowAssoc[CompileOps[F, G, O]]

Ungrouped