final class CompileOps[F[_], G[_], O] extends AnyRef
Projection of a Stream
providing various ways to compile a Stream[F,O]
to a G[...]
.
- Source
- Stream.scala
- Alphabetic
- By Inheritance
- CompileOps
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native() @IntrinsicCandidate()
-
def
count: G[Long]
Compiles this stream to a count of the elements in the target effect type
G
. -
def
drain: G[Unit]
Compiles this stream in to a value of the target effect type
G
and discards any output values of the stream. -
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
fold[B](init: B)(f: (B, O) ⇒ B): G[B]
Compiles this stream in to a value of the target effect type
G
by folding the output values together, starting with the providedinit
and combining the current value with each output value. -
def
foldChunks[B](init: B)(f: (B, Chunk[O]) ⇒ B): G[B]
Compiles this stream in to a value of the target effect type
G
by folding the output chunks together, starting with the providedinit
and combining the current value with each output chunk.Compiles this stream in to a value of the target effect type
G
by folding the output chunks together, starting with the providedinit
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.
-
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.scala> import cats.effect.SyncIO scala> Stream(1, 2, 3, 4, 5).covary[SyncIO].compile.foldMonoid.unsafeRunSync() res0: Int = 15
Example: -
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.scala> import cats.effect.SyncIO scala> Stream(1, 2, 3, 4, 5).covary[SyncIO].compile.foldSemigroup.unsafeRunSync() res0: Option[Int] = Some(15) scala> Stream.empty.covaryAll[SyncIO, Int].compile.foldSemigroup.unsafeRunSync() res1: Option[Int] = None
Example: -
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @IntrinsicCandidate()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @IntrinsicCandidate()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
last: G[Option[O]]
Compiles this stream in to a value of the target effect type
G
, returningNone
if the stream emitted no values and returning the last value emitted wrapped inSome
if values were emitted.Compiles this stream in to a value of the target effect type
G
, returningNone
if the stream emitted no values and returning the last value emitted wrapped inSome
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.
scala> import cats.effect.SyncIO scala> Stream.range(0,100).take(5).covary[SyncIO].compile.last.unsafeRunSync() res0: Option[Int] = Some(4)
Example: -
def
lastOrError(implicit G: MonadError[G, Throwable]): G[O]
Compiles this stream in to a value of the target effect type
G
, raising aNoSuchElementException
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
G
, raising aNoSuchElementException
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.
scala> import cats.effect.SyncIO scala> Stream.range(0,100).take(5).covary[SyncIO].compile.lastOrError.unsafeRunSync() res0: Int = 4 scala> Stream.empty.covaryAll[SyncIO, Int].compile.lastOrError.attempt.unsafeRunSync() res1: Either[Throwable, Int] = Left(java.util.NoSuchElementException)
Example: -
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @IntrinsicCandidate()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @IntrinsicCandidate()
-
def
onlyOrError(implicit G: MonadError[G, Throwable]): G[O]
Compiles this stream in to a value of the target effect type
G
, raising aNoSuchElementException
if the stream emitted no values,IllegalStateException
if the stream emits more than one value and returning the only value emitted otherwise.Compiles this stream in to a value of the target effect type
G
, raising aNoSuchElementException
if the stream emitted no values,IllegalStateException
if the stream emits more than one value and returning the only value emitted otherwise. Note that the stream execution will not short-circuit once it emits more than one element, it will still be evaluated to its end before raising the exception.When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.
scala> import cats.effect.SyncIO scala> Stream(1).covary[SyncIO].compile.onlyOrError.unsafeRunSync() res0: Int = 1 scala> Stream.empty.covaryAll[SyncIO, Int].compile.onlyOrError.attempt.unsafeRunSync() res1: Either[Throwable, Int] = Left(java.util.NoSuchElementException) scala> Stream.range(0,10).covary[SyncIO].compile.onlyOrError.attempt.unsafeRunSync() res2: Either[Throwable, Int] = Left(java.lang.IllegalStateException: Expected singleton stream)
Example: -
def
resource(implicit compiler: Compiler[F, [β$8$]Resource[G, β$8$]]): CompileOps[F, [β$9$]Resource[G, β$9$], O]
Gives access to the whole compilation api, where the result is expressed as a
cats.effect.Resource
, instead of bareG
.Gives access to the whole compilation api, where the result is expressed as a
cats.effect.Resource
, instead of bareG
.import fs2._ import cats.effect._ 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.kernel.Ref import scala.concurrent.duration._ trait StopWatch[F[_]] { def elapsedSeconds: F[Int] } object StopWatch { def create[F[_]](implicit F: Temporal[F]): Stream[F, StopWatch[F]] = Stream.eval(F.ref(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 aStream[F, StopWatch[F]]
, even though there is only one instance being emitted: this is less than ideal, so we might think about returning anF[StopWatch[F]]
with the following codeStopWatch.create[F].compile.lastOrError
but it does not work: the returned
F
terminates the lifetime of the stream, which causesconcurrently
to stop theprocess
stream. As a result,elapsedSeconds
never gets updated.Alternatively, we could implement
StopWatch
inF
only usingFiber.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[_]: Temporal]: Resource[F, StopWatch[F]] = create.compile.resource.lastOrError }
This works for every other
compile.
method, although it's a very natural fit withlastOrError
. -
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 aStringBuilder
internally, avoiding intermediate string creation.scala> Stream("Hello ", "world!").compile.string res0: String = Hello world!
Example: -
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
to(collector: Collector[O]): G[Out]
Compiles this stream into a value of the target effect type
G
by collecting all of the output values in a collection.Compiles this stream into a value of the target effect type
G
by collecting all of the output values in a collection.Collection building is done via an explicitly passed
Collector
. Standard library collections have collector instances, allowing syntax like:s.compile.to(List)
ors.compile.to(Array)
ors.compile.to(Map)
.A collector is provided for
scodec.bits.ByteVector
, providing efficient byte vector construction from a stream of bytes:s.compile.to(ByteVector)
.When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.
scala> import cats.effect.SyncIO scala> val s = Stream.range(0,100).take(5).covary[SyncIO] scala> s.compile.to(List).unsafeRunSync() res0: List[Int] = List(0, 1, 2, 3, 4) scala> s.compile.to(Chunk).unsafeRunSync() res1: Chunk[Int] = Chunk(0, 1, 2, 3, 4) scala> s.map(i => (i % 2, i)).compile.to(Map).unsafeRunSync() res2: Map[Int, Int] = Map(0 -> 4, 1 -> 3) scala> s.map(_.toByte).compile.to(scodec.bits.ByteVector).unsafeRunSync() res3: scodec.bits.ByteVector = ByteVector(5 bytes, 0x0001020304)
Example: -
def
toList: G[List[O]]
Compiles this stream in to a value of the target effect type
G
by logging the output values to aList
.Compiles this stream in to a value of the target effect type
G
by logging the output values to aList
. Equivalent toto[List]
.When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.
scala> import cats.effect.SyncIO scala> Stream.range(0,100).take(5).covary[SyncIO].compile.toList.unsafeRunSync() res0: List[Int] = List(0, 1, 2, 3, 4)
Example: -
def
toString(): String
- Definition Classes
- AnyRef → Any
-
def
toVector: G[Vector[O]]
Compiles this stream in to a value of the target effect type
G
by logging the output values to aVector
.Compiles this stream in to a value of the target effect type
G
by logging the output values to aVector
. Equivalent toto[Vector]
.When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.
scala> import cats.effect.SyncIO scala> Stream.range(0,100).take(5).covary[SyncIO].compile.toVector.unsafeRunSync() res0: Vector[Int] = Vector(0, 1, 2, 3, 4)
Example: -
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )