Provides syntax for pure empty pipes.
Provides syntax for streams that are invariant in F
and O
.
Provides operations on effectful pipes for syntactic convenience.
Provides syntax for pure pipes.
Provides operations on pure pipes for syntactic convenience.
Provides operations on pure pipes for syntactic convenience.
Projection of a Stream
providing various ways to compile a Stream[F,O]
to an F[...]
.
Projection of a Stream
providing various ways to get a Pull
from the Stream
.
Provides syntax for pure empty pipes.
Provides syntax for streams that are invariant in F
and O
.
Provides syntax for pure pipes.
Creates a pure stream that emits the supplied values.
Creates a pure stream that emits the supplied values. To convert to an effectful stream, use covary.
Creates a single element stream that gets its value by evaluating the supplied effect.
Creates a single element stream that gets its value by evaluating the supplied effect. If the effect fails, a Left
is emitted. Otherwise, a Right
is emitted.
Use eval instead if a failure while evaluating the effect should fail the stream.
scala> import cats.effect.IO scala> Stream.attemptEval(IO(10)).compile.toVector.unsafeRunSync res0: Vector[Either[Throwable,Int]] = Vector(Right(10)) scala> Stream.attemptEval(IO(throw new RuntimeException)).compile.toVector.unsafeRunSync res1: Vector[Either[Throwable,Nothing]] = Vector(Left(java.lang.RuntimeException))
Creates a stream that depends on a resource allocated by an effect, ensuring the resource is released regardless of how the stream is used.
Creates a stream that depends on a resource allocated by an effect, ensuring the resource is released regardless of how the stream is used.
resource to acquire at start of stream
function which uses the acquired resource to generate a stream of effectful outputs
function which returns an effect that releases the resource
A typical use case for bracket is working with files or network sockets. The resource effect
opens a file and returns a reference to it. The use
function reads bytes and transforms them
in to some stream of elements (e.g., bytes, strings, lines, etc.). The release
action closes
the file.
Creates a pure stream that emits the elements of the supplied chunk.
Creates a pure stream that emits the elements of the supplied chunk.
scala> Stream.chunk(Chunk(1,2,3)).toList res0: List[Int] = List(1, 2, 3)
Creates an infinite pure stream that always returns the supplied value.
Creates an infinite pure stream that always returns the supplied value.
Elements are emitted in finite segments with segmentSize
number of elements.
scala> Stream.constant(0).take(5).toList res0: List[Int] = List(0, 0, 0, 0, 0)
Implicitly covaries a stream.
Implicitly covaries a pipe.
Implicitly covaries a Pipe2
.
A continuous stream of the elapsed time, computed using System.nanoTime
.
A continuous stream of the elapsed time, computed using System.nanoTime
.
Note that the actual granularity of these elapsed times depends on the OS, for instance
the OS may only update the current time every ten milliseconds or so.
Creates a singleton pure stream that emits the supplied value.
Creates a singleton pure stream that emits the supplied value.
scala> Stream.emit(0).toList res0: List[Int] = List(0)
Creates a pure stream that emits the supplied values.
Creates a pure stream that emits the supplied values.
scala> Stream.emits(List(1, 2, 3)).toList res0: List[Int] = List(1, 2, 3)
Empty pure stream.
Creates a single element stream that gets its value by evaluating the supplied effect.
Creates a single element stream that gets its value by evaluating the supplied effect. If the effect fails, the returned stream fails.
Use attemptEval instead if a failure while evaluating the effect should be emitted as a value.
scala> import cats.effect.IO scala> Stream.eval(IO(10)).compile.toVector.unsafeRunSync res0: Vector[Int] = Vector(10) scala> Stream.eval(IO(throw new RuntimeException)).compile.toVector.attempt.unsafeRunSync res1: Either[Throwable,Vector[Nothing]] = Left(java.lang.RuntimeException)
Creates a stream that evaluates the supplied fa
for its effect, discarding the output value.
Creates a stream that evaluates the supplied fa
for its effect, discarding the output value.
As a result, the returned stream emits no elements and hence has output type Nothing
.
Alias for eval(fa).drain
.
scala> import cats.effect.IO scala> Stream.eval_(IO(println("Ran"))).compile.toVector.unsafeRunSync res0: Vector[Nothing] = Vector()
A continuous stream which is true after d, 2d, 3d...
elapsed duration,
and false otherwise.
A continuous stream which is true after d, 2d, 3d...
elapsed duration,
and false otherwise.
If you'd like a 'discrete' stream that will actually block until d
has elapsed,
use awakeEvery
on Scheduler instead.
Lifts an effect that generates a stream in to a stream.
Lifts an effect that generates a stream in to a stream. Alias for eval(f).flatMap(_)
.
scala> import cats.effect.IO scala> Stream.force(IO(Stream(1,2,3).covary[IO])).compile.toVector.unsafeRunSync res0: Vector[Int] = Vector(1, 2, 3)
Lifts an iterator into a Stream
Gets the current scope, allowing manual leasing or interruption.
Gets the current scope, allowing manual leasing or interruption. This is a low-level method and generally should not be used by user code.
An infinite Stream
that repeatedly applies a given function
to a start value.
An infinite Stream
that repeatedly applies a given function
to a start value. start
is the first value emitted, followed
by f(start)
, then f(f(start))
, and so on.
scala> Stream.iterate(0)(_ + 1).take(10).toList res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Like iterate, but takes an effectful function for producing the next state.
Like iterate, but takes an effectful function for producing
the next state. start
is the first value emitted.
scala> import cats.effect.IO scala> Stream.iterateEval(0)(i => IO(i + 1)).take(10).compile.toVector.unsafeRunSync res0: Vector[Int] = Vector(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Monoid
instance for Stream
.
Creates a stream that, when run, fails with the supplied exception.
Creates a stream that, when run, fails with the supplied exception.
scala> import scala.util.Try scala> Try(Stream.raiseError(new RuntimeException).toList) res0: Try[List[Nothing]] = Failure(java.lang.RuntimeException) scala> import cats.effect.IO scala> Stream.raiseError(new RuntimeException).covary[IO].compile.drain.attempt.unsafeRunSync res0: Either[Throwable,Unit] = Left(java.lang.RuntimeException)
Lazily produce the range [start, stopExclusive)
.
Lazily produce the range [start, stopExclusive)
. If you want to produce
the sequence in one chunk, instead of lazily, use
emits(start until stopExclusive)
.
scala> Stream.range(10, 20, 2).toList res0: List[Int] = List(10, 12, 14, 16, 18)
Lazily produce a sequence of nonoverlapping ranges, where each range
contains size
integers, assuming the upper bound is exclusive.
Lazily produce a sequence of nonoverlapping ranges, where each range
contains size
integers, assuming the upper bound is exclusive.
Example: ranges(0, 1000, 10)
results in the pairs
(0, 10), (10, 20), (20, 30) ... (990, 1000)
Note: The last emitted range may be truncated at stopExclusive
. For
instance, ranges(0,5,4)
results in (0,4), (4,5)
.
scala> Stream.ranges(0, 20, 5).toList res0: List[(Int,Int)] = List((0,5), (5,10), (10,15), (15,20))
IllegalArgumentException
if size
<= 0
Alias for eval(fo).repeat
.
Creates a pure stream that emits the values of the supplied segment.
Creates a pure stream that emits the values of the supplied segment.
scala> Stream.segment(Segment.from(0)).take(5).toList res0: List[Long] = List(0, 1, 2, 3, 4)
Returns a stream that evaluates the supplied by-name each time the stream is used, allowing use of a mutable value in stream computations.
Returns a stream that evaluates the supplied by-name each time the stream is used, allowing use of a mutable value in stream computations.
Note: it's generally easier to reason about such computations using effectful
values. That is, allocate the mutable value in an effect and then use
Stream.eval(fa).flatMap { a => ??? }
.
scala> Stream.suspend { | val digest = java.security.MessageDigest.getInstance("SHA-256") | val bytes: Stream[Pure,Byte] = ??? | bytes.chunks.fold(digest) { (d,c) => d.update(c.toBytes.values); d } | }
Sync
instance for Stream
.
Sync
instance for Stream
.
scala> import cats.implicits._ scala> import cats.effect.Sync scala> implicit def si: Sync[Stream[Pure, ?]] = Stream.syncInstance[Pure] scala> Stream(1, -2, 3).fproduct(_.abs).toList res0: List[(Int, Int)] = List((1,1), (-2,2), (3,3))
Creates a stream by successively applying f
until a None
is returned, emitting
each output O
and using each output S
as input to the next invocation of f
.
Creates a stream by successively applying f
until a None
is returned, emitting
each output O
and using each output S
as input to the next invocation of f
.
scala> Stream.unfold(0)(i => if (i < 5) Some(i -> (i+1)) else None).toList res0: List[Int] = List(0, 1, 2, 3, 4)
Alias for unfoldSegmentEval with slightly better type inference when f
returns a Chunk
.
Like unfold, but takes an effectful function.
Like unfold but each invocation of f
provides a chunk of output.
Like unfold but each invocation of f
provides a chunk of output.
scala> Stream.unfoldSegment(0)(i => if (i < 5) Some(Segment.seq(List.fill(i)(i)) -> (i+1)) else None).toList res0: List[Int] = List(1, 2, 2, 3, 3, 3, 4, 4, 4, 4)
Like unfoldSegment, but takes an effectful function.