Appends s2
to the end of this stream.
Alias for flatMap(_ => s2)
.
Appends s2
to the end of this stream.
Appends s2
to the end of this stream. Alias for s1 ++ s2
.
Emits only elements that are distinct from their immediate predecessors, using natural equality for comparison.
Emits only elements that are distinct from their immediate predecessors, using natural equality for comparison.
scala> import cats.implicits._ scala> Stream(1,1,2,2,2,3,3).changes.toList res0: List[Int] = List(1, 2, 3)
Lifts this stream to the specified effect type.
Lifts this stream to the specified effect type.
scala> import cats.effect.IO scala> Stream(1, 2, 3).covary[IO] res0: Stream[IO,Int] = Stream(..)
Lifts this stream to the specified effect and output types.
Lifts this stream to the specified effect and output types.
scala> import cats.effect.IO scala> Stream.empty.covaryAll[IO,Int] res0: Stream[IO,Int] = Stream(..)
Pass elements of s
through both f
and g
, then combine the two resulting streams.
Pass elements of s
through both f
and g
, then combine the two resulting streams.
Implemented by enqueueing elements as they are seen by f
onto a Queue
used by the g
branch.
USE EXTREME CARE WHEN USING THIS FUNCTION. Deadlocks are possible if combine
pulls from the g
branch synchronously before the queue has been populated by the f
branch.
The combine
function receives an F[Int]
effect which evaluates to the current size of the
g
-branch's queue.
When possible, use one of the safe combinators like observe
, which are built using this function,
in preference to using this function directly.
Like merge
, but tags each output with the branch it came from.
Like merge
, but tags each output with the branch it came from.
scala> import scala.concurrent.duration._, scala.concurrent.ExecutionContext.Implicits.global, cats.effect.IO scala> val s = Scheduler[IO](1).flatMap { scheduler => | val s1 = scheduler.awakeEvery[IO](500.millis).scan(0)((acc, i) => acc + 1) | s1.either(scheduler.sleep_[IO](250.millis) ++ s1).take(10) | } scala> s.take(10).runLog.unsafeRunSync res0: Vector[Either[Int,Int]] = Vector(Left(0), Right(0), Left(1), Right(1), Left(2), Right(2), Left(3), Right(3), Left(4), Right(4))
Alias for flatMap(o => Stream.eval(f(o)))
.
Alias for flatMap(o => Stream.eval(f(o)))
.
scala> import cats.effect.IO scala> Stream(1,2,3,4).evalMap(i => IO(println(i))).run.unsafeRunSync res0: Unit = ()
Like Stream#scan
, but accepts a function returning an F[_]
.
Like Stream#scan
, but accepts a function returning an F[_]
.
scala> import cats.effect.IO scala> Stream(1,2,3,4).evalScan(0)((acc,i) => IO(acc + i)).runLog.unsafeRunSync res0: Vector[Int] = Vector(0, 1, 3, 6, 10)
Creates a stream whose elements are generated by applying f
to each output of
the source stream and concatenated all of the results.
Creates a stream whose elements are generated by applying f
to each output of
the source stream and concatenated all of the results.
scala> Stream(1, 2, 3).flatMap { i => Stream.segment(Segment.seq(List.fill(i)(i))) }.toList res0: List[Int] = List(1, 2, 2, 3, 3, 3)
Folds this stream with the monoid for O
.
Folds this stream with the monoid for O
.
scala> import cats.implicits._ scala> Stream(1, 2, 3, 4, 5).foldMonoid.toList res0: List[Int] = List(15)
Determinsitically interleaves elements, starting on the left, terminating when the end of either branch is reached naturally.
Determinsitically interleaves elements, starting on the left, terminating when the end of either branch is reached naturally.
scala> Stream(1, 2, 3).interleave(Stream(4, 5, 6, 7)).toList res0: List[Int] = List(1, 4, 2, 5, 3, 6)
Determinsitically interleaves elements, starting on the left, terminating when the ends of both branches are reached naturally.
Determinsitically interleaves elements, starting on the left, terminating when the ends of both branches are reached naturally.
scala> Stream(1, 2, 3).interleaveAll(Stream(4, 5, 6, 7)).toList res0: List[Int] = List(1, 4, 2, 5, 3, 6, 7)
Alias for interruptWhen(haltWhenTrue.discrete)
.
Let through the s2
branch as long as the s1
branch is false
,
listening asynchronously for the left branch to become true
.
Let through the s2
branch as long as the s1
branch is false
,
listening asynchronously for the left branch to become true
.
This halts as soon as either branch halts.
Consider using the overload that takes a Signal
.
Nondeterministically merges a stream of streams (outer
) in to a single stream,
opening at most maxOpen
streams at any point in time.
Nondeterministically merges a stream of streams (outer
) in to a single stream,
opening at most maxOpen
streams at any point in time.
The outer stream is evaluated and each resulting inner stream is run concurrently,
up to maxOpen
stream. Once this limit is reached, evaluation of the outer stream
is paused until one or more inner streams finish evaluating.
When the outer stream stops gracefully, all inner streams continue to run, resulting in a stream that will stop when all inner streams finish their evaluation.
When the outer stream fails, evaluation of all inner streams is interrupted and the resulting stream will fail with same failure.
When any of the inner streams fail, then the outer stream and all other inner streams are interrupted, resulting in stream that fails with the error of the stream that caused initial failure.
Finalizers on each inner stream are run at the end of the inner stream, concurrently with other stream computations.
Finalizers on the outer stream are run after all inner streams have been pulled from the outer stream -- hence, finalizers on the outer stream will likely run BEFORE the LAST finalizer on the last inner stream.
Finalizers on the returned stream are run after the outer stream has finished and all open inner streams have finished.
Maximum number of open inner streams at any time. Must be > 0.
Like join but races all inner streams simultaneously.
Interleaves the two inputs nondeterministically.
Interleaves the two inputs nondeterministically. The output stream
halts after BOTH s1
and s2
terminate normally, or in the event
of an uncaught failure on either s1
or s2
. Has the property that
merge(Stream.empty, s) == s
and merge(fail(e), s)
will
eventually terminate with fail(e)
, possibly after emitting some
elements of s
first.
scala> import scala.concurrent.duration._, scala.concurrent.ExecutionContext.Implicits.global, cats.effect.IO scala> val s = Scheduler[IO](1).flatMap { scheduler => | val s1 = scheduler.awakeEvery[IO](500.millis).scan(0)((acc, i) => acc + 1) | s1.merge(scheduler.sleep_[IO](250.millis) ++ s1) | } scala> s.take(6).runLog.unsafeRunSync res0: Vector[Int] = Vector(0, 0, 1, 1, 2, 2)
Defined as self.drain merge that
.
Defined as self.drain merge that
. Runs self
and that
concurrently, ignoring
any output of that
.
Defined as self merge that.drain
.
Defined as self merge that.drain
. Runs self
and that
concurrently, ignoring
any output of that
.
Like merge
, but halts as soon as _either_ branch halts.
Like merge
, but halts as soon as the s1
branch halts.
Like merge
, but halts as soon as the s2
branch halts.
Synchronously sends values through sink
.
Synchronously sends values through sink
.
scala> import scala.concurrent.ExecutionContext.Implicits.global, cats.effect.IO, cats.implicits._ scala> Stream(1, 2, 3).covary[IO].observe(Sink.showLinesStdOut).map(_ + 1).runLog.unsafeRunSync res0: Vector[Int] = Vector(2, 3, 4)
Send chunks through sink
, allowing up to maxQueued
pending _chunks_ before blocking s
.
Run s2
after this
, regardless of errors during this
, then reraise any errors encountered during this
.
Run s2
after this
, regardless of errors during this
, then reraise any errors encountered during this
.
Note: this should *not* be used for resource cleanup! Use bracket
or onFinalize
instead.
scala> Stream(1, 2, 3).onComplete(Stream(4, 5)).toList res0: List[Int] = List(1, 2, 3, 4, 5)
If this
terminates with Stream.fail(e)
, invoke h(e)
.
If this
terminates with Stream.fail(e)
, invoke h(e)
.
scala> Stream(1, 2, 3).append(Stream.fail(new RuntimeException)).onError(t => Stream(0)).toList res0: List[Int] = List(1, 2, 3, 0)
Run the supplied effectful action at the end of this stream, regardless of how the stream terminates.
Alias for pauseWhen(pauseWhenTrue.discrete)
.
Like interrupt
but resumes the stream when left branch goes to true.
Behaves like identity
, but starts fetching the next segment before emitting the current,
enabling processing on either side of the prefetch
to run in parallel.
Gets a projection of this stream that allows converting it to a Pull
in a number of ways.
Reduces this stream with the Semigroup for O
.
Reduces this stream with the Semigroup for O
.
scala> import cats.implicits._ scala> Stream("The", "quick", "brown", "fox").intersperse(" ").reduceSemigroup.toList res0: List[String] = List(The quick brown fox)
Repeatedly invokes using
, running the resultant Pull
each time, halting when a pull
returns None
instead of Some(nextStream)
.
Interprets this stream in to a value of the target effect type F
and
discards any output values of the stream.
Interprets 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 methods that start with run
--
e.g., runFold, runLog, etc.
When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.
To call this method, a Sync[F]
instance must be implicitly available.
Interprets 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.
Interprets 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.
When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.
To call this method, a Sync[F]
instance must be implicitly available.
Like runFold but uses the implicitly available Monoid[O]
to combine elements.
Like runFold but uses the implicitly available Monoid[O]
to combine elements.
scala> import cats.implicits._, cats.effect.IO scala> Stream(1, 2, 3, 4, 5).covary[IO].runFoldMonoid.unsafeRunSync res0: Int = 15
Like runFold but uses the implicitly available Semigroup[O]
to combine elements.
Like runFold but uses the implicitly available Semigroup[O]
to combine elements.
If the stream emits no elements, None
is returned.
scala> import cats.implicits._, cats.effect.IO scala> Stream(1, 2, 3, 4, 5).covary[IO].runFoldSemigroup.unsafeRunSync res0: Option[Int] = Some(15) scala> Stream.empty.covaryAll[IO,Int].runFoldSemigroup.unsafeRunSync res1: Option[Int] = None
Interprets 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.
Interprets 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.
To call this method, a Sync[F]
instance must be implicitly available.
scala> import cats.effect.IO scala> Stream.range(0,100).take(5).covary[IO].runLog.unsafeRunSync res0: Vector[Int] = Vector(0, 1, 2, 3, 4)
Interprets this stream in to a value of the target effect type F
by logging
the output values to a Vector
.
Interprets this stream in to a value of the target effect type F
by logging
the output values to a Vector
.
When this method has returned, the stream has not begun execution -- this method simply compiles the stream down to the target effect type.
To call this method, a Sync[F]
instance must be implicitly available.
scala> import cats.effect.IO scala> Stream.range(0,100).take(5).covary[IO].runLog.unsafeRunSync res0: Vector[Int] = Vector(0, 1, 2, 3, 4)
Like scan
but f
is applied to each segment of the source stream.
Like scan
but f
is applied to each segment of the source stream.
The resulting segment is emitted and the result of the segment is used in the
next invocation of f
.
Many stateful pipes can be implemented efficiently (i.e., supporting fusion) with this method.
More general version of scanSegments
where the current state (i.e., S
) can be inspected
to determine if another segment should be pulled or if the stream should terminate.
More general version of scanSegments
where the current state (i.e., S
) can be inspected
to determine if another segment should be pulled or if the stream should terminate.
Termination is signaled by returning None
from f
. Otherwise, a function which consumes
the next segment is returned wrapped in Some
.
scala> def take[F[_],O](s: Stream[F,O], n: Long): Stream[F,O] = | s.scanSegmentsOpt(n) { n => if (n <= 0) None else Some(_.take(n).mapResult(_.fold(_._2, _ => 0))) } scala> take(Stream.range(0,100), 5).toList res0: List[Int] = List(0, 1, 2, 3, 4)
Transforms this stream using the given Pipe
.
Transforms this stream using the given Pipe
.
scala> Stream("Hello", "world").through(text.utf8Encode).toVector.toArray res0: Array[Byte] = Array(72, 101, 108, 108, 111, 119, 111, 114, 108, 100)
Transforms this stream and s2
using the given Pipe2
.
Transforms this stream and s2
using the given pure Pipe2
.
Transforms this stream and s2
using the given pure Pipe2
.
Sometimes this has better type inference than through2
(e.g., when F
is Nothing
).
Transforms this stream using the given pure Pipe
.
Transforms this stream using the given pure Pipe
.
Sometimes this has better type inference than through
(e.g., when F
is Nothing
).
Applies the given sink to this stream.
Applies the given sink to this stream.
scala> import cats.effect.IO, cats.implicits._ scala> Stream(1,2,3).covary[IO].to(Sink.showLinesStdOut).run.unsafeRunSync res0: Unit = ()
Translates effect type from F
to G
using the supplied FunctionK
and using
the supplied Effect[G]
for any unconsAsync
steps encountered.
Translates effect type from F
to G
using the supplied FunctionK
.
Translates effect type from F
to G
using the supplied FunctionK
.
If any unconsAsync
steps are encountered, an error is raised in the resulting
stream. To translate such streams successfully, use translate instead and
provide an Effect[G]
instance.
Determinsitically zips elements, terminating when the end of either branch is reached naturally.
Determinsitically zips elements, terminating when the end of either branch is reached naturally.
scala> Stream(1, 2, 3).zip(Stream(4, 5, 6, 7)).toList res0: List[(Int,Int)] = List((1,4), (2,5), (3,6))
Determinsitically zips elements, terminating when the ends of both branches
are reached naturally, padding the left branch with pad1
and padding the right branch
with pad2
as necessary.
Determinsitically zips elements, terminating when the ends of both branches
are reached naturally, padding the left branch with pad1
and padding the right branch
with pad2
as necessary.
scala> Stream(1,2,3).zipAll(Stream(4,5,6,7))(0,0).toList res0: List[(Int,Int)] = List((1,4), (2,5), (3,6), (0,7))
Determinsitically zips elements with the specified function, terminating
when the ends of both branches are reached naturally, padding the left
branch with pad1
and padding the right branch with pad2
as necessary.
Determinsitically zips elements with the specified function, terminating
when the ends of both branches are reached naturally, padding the left
branch with pad1
and padding the right branch with pad2
as necessary.
scala> Stream(1,2,3).zipAllWith(Stream(4,5,6,7))(0, 0)(_ + _).toList res0: List[Int] = List(5, 7, 9, 7)
Determinsitically zips elements using the specified function, terminating when the end of either branch is reached naturally.
Determinsitically zips elements using the specified function, terminating when the end of either branch is reached naturally.
scala> Stream(1, 2, 3).zipWith(Stream(4, 5, 6, 7))(_ + _).toList res0: List[Int] = List(5, 7, 9)
Provides syntax for streams that are invariant in
F
andO
.