Stream

fs2.Stream
See theStream companion object
final class Stream[+F[_], +O]

A stream producing output of type O and which may evaluate F effects.

- Purely functional a value of type Stream[F, O] _describes_ an effectful computation. A function that returns a Stream[F, O] builds a _description_ of an effectful computation, but does not perform them. The methods of the Stream class derive new descriptions from others. This is similar to how effect types like cats.effect.IO and monix.Task build descriptions of computations.

- Pull: to evaluate a stream, a consumer pulls its values from it, by repeatedly performing one pull step at a time. Each step is a F-effectful computation that may yield some O values (or none), and a stream from which to continue pulling. The consumer controls the evaluation of the stream, which effectful operations are performed, and when.

- Non-Strict: stream evaluation only pulls from the stream a prefix large enough to compute its results. Thus, although a stream may yield an unbounded number of values or, after successfully yielding several values, either raise an error or hang up and never yield any value, the consumer need not reach those points of failure. For the same reason, in general, no effect in F is evaluated unless and until the consumer needs it.

- Abstract: a stream needs not be a plain finite list of fixed effectful computations in F. It can also represent an input or output connection through which data incrementally arrives. It can represent an effectful computation, such as reading the system's time, that can be re-evaluated as often as the consumer of the stream requires.

Special properties for streams

There are some special properties or cases of streams: - A stream is finite if we can reach the end after a limited number of pull steps, which may yield a finite number of values. It is empty if it terminates and yields no values. - A singleton stream is a stream that ends after yielding one single value. - A pure stream is one in which the F is Pure, which indicates that it evaluates no effects. - A never stream is a stream that never terminates and never yields any value.

Pure Streams and operations

We can sometimes think of streams, naively, as lists of O elements with F-effects. This is particularly true for pure streams, which are instances of Stream which use the Pure effect type. We can convert every pure and finite stream into a List[O] using the .toList method. Also, we can convert pure infinite streams into instances of the Stream[O] class from the Scala standard library.

A method of the Stream class is pure if it can be applied to pure streams. Such methods are identified in that their signature includes no type-class constraint (or implicit parameter) on the F method. Pure methods in Stream[F, O] can be projected naturally to methods in the List class, which means that applying the stream's method and converting the result to a list gets the same result as first converting the stream to a list, and then applying list methods.

Some methods that project directly to list are map, filter, takeWhile, etc. There are other methods, like exists or find, that in the List class they return a value or an Option, but their stream counterparts return an (either empty or singleton) stream. Other methods, like zipWithPrevious, have a more complicated but still pure translation to list methods.

Type-Class instances and laws of the Stream Operations

Laws (using infix syntax):

append forms a monoid in conjunction with empty:

  • empty append s == s and s append empty == s.

  • (s1 append s2) append s3 == s1 append (s2 append s3)

And cons is consistent with using ++ to prepend a single chunk:

  • s.cons(c) == Stream.chunk(c) ++ s

Stream.raiseError propagates until being caught by handleErrorWith:

  • Stream.raiseError(e) handleErrorWith h == h(e)

  • Stream.raiseError(e) ++ s == Stream.raiseError(e)

  • Stream.raiseError(e) flatMap f == Stream.raiseError(e)

Stream forms a monad with emit and flatMap:

  • Stream.emit >=> f == f (left identity)

  • f >=> Stream.emit === f (right identity - note weaker equality notion here)

  • (f >=> g) >=> h == f >=> (g >=> h) (associativity) where Stream.emit(a) is defined as chunk(Chunk.singleton(a)) and f >=> g is defined as a => a flatMap f flatMap g

The monad is the list-style sequencing monad:

  • (a ++ b) flatMap f == (a flatMap f) ++ (b flatMap f)

  • Stream.empty flatMap f == Stream.empty

Technical notes

Note: since the chunk structure of the stream is observable, and s flatMap Stream.emit produces a stream of singleton chunks, the right identity law uses a weaker notion of equality, === which normalizes both sides with respect to chunk structure:

(s1 === s2) = normalize(s1) == normalize(s2) where == is full equality (a == b iff f(a) is identical to f(b) for all f)

normalize(s) can be defined as s.flatMap(Stream.emit), which just produces a singly-chunked stream from any input stream s.

For instance, for a stream s and a function f: A => B, - the result of s.map(f) is a Stream with the same _chunking_ as the s; whereas... - the result of s.flatMap(x => S.emit(f(x))) is a Stream structured as a sequence of singleton chunks. The latter is using the definition of map that is derived from the Monad instance.

This is not unlike equality for maps or sets, which is defined by which elements they contain, not by how these are spread between a tree's branches or a hashtable buckets. However, a Stream structure can be _observed_ through the chunks method, so two streams "_equal_" under that notion may give different results through this method.

Note: For efficiency Stream.map function operates on an entire chunk at a time and preserves chunk structure, which differs from the map derived from the monad (s map f == s flatMap (f andThen Stream.emit)) which would produce singleton chunk. In particular, if f throws errors, the chunked version will fail on the first chunk with an error, while the unchunked version will fail on the first element with an error. Exceptions in pure code like this are strongly discouraged.

Attributes

Companion
object
Source
Stream.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def ++[F2[x], O2 >: O](s2: => Stream[F2, O2]): Stream[F2, O2]

Appends s2 to the end of this stream.

Appends s2 to the end of this stream.

Attributes

Example

scala> (Stream(1,2,3) ++ Stream(4,5,6)).toList
res0: List[Int] = List(1, 2, 3, 4, 5, 6)

If this stream is infinite, then the result is equivalent to this.

Source
Stream.scala
def >>[F2[x], O2](s2: => Stream[F2, O2])(implicit ev: NotGiven[O <:< Nothing]): Stream[F2, O2]

Alias for flatMap(_ => s2).

Alias for flatMap(_ => s2).

Attributes

Source
Stream.scala
def append[F2[x], O2 >: O](s2: => Stream[F2, O2]): Stream[F2, O2]

Appends s2 to the end of this stream.

Appends s2 to the end of this stream. Alias for s1 ++ s2.

Attributes

Source
Stream.scala
def apply[F[_]]: Stream[F, O]
Implicitly added by PureOps

Alias for covary, to be able to write Stream.empty[X].

Alias for covary, to be able to write Stream.empty[X].

Attributes

Source
Stream.scala
def as[O2](o2: O2): Stream[F, O2]

Equivalent to val o2Memoized = o2; _.map(_ => o2Memoized).

Equivalent to val o2Memoized = o2; _.map(_ => o2Memoized).

Attributes

Example

scala> Stream(1,2,3).as(0).toList
res0: List[Int] = List(0, 0, 0)
Source
Stream.scala

Returns a stream of O values wrapped in Right until the first error, which is emitted wrapped in Left.

Returns a stream of O values wrapped in Right until the first error, which is emitted wrapped in Left.

Attributes

Example

scala> import cats.effect.SyncIO
scala> (Stream(1,2,3) ++ Stream.raiseError[SyncIO](new RuntimeException) ++ Stream(4,5,6)).attempt.compile.toList.unsafeRunSync()
res0: List[Either[Throwable,Int]] = List(Right(1), Right(2), Right(3), Left(java.lang.RuntimeException))

rethrow is the inverse of attempt, with the caveat that anything after the first failure is discarded.

Source
Stream.scala

Retries on failure, returning a stream of attempts that can be manipulated with standard stream operations such as take, collectFirst and interruptWhen.

Retries on failure, returning a stream of attempts that can be manipulated with standard stream operations such as take, collectFirst and interruptWhen.

Note: The resulting stream does *not* automatically halt at the first successful attempt. Also see retry.

Attributes

Source
Stream.scala
def broadcastThrough[F2[x] : Concurrent, O2](pipes: (F2, O) => O2*): Stream[F2, O2]

Feeds the values from this stream (source) to all the given pipes, which process them in parallel, and coordinates their progress.

Feeds the values from this stream (source) to all the given pipes, which process them in parallel, and coordinates their progress.

The new stream has one instance of this stream (the source), from which it pulls its outputs. To balance the progress amongst pipes and source, outputs are passed chunk-by-chunk, via a Topic. This creates a one-chunk buffer in front of each pipe. A pipe starts processing a chunk after pulling it from its buffer. The topic enforces some temporal constraints: - No chunk is pushed to the buffer of any pipe until after the previous chunk has been published to all pipes. - No chunk is pushed to a pipe until the pipe pulls the previous chunk. - A chunk may be pushed to some pipes, and pulled by them, before other pipes have pulled the previous chunk.

Thus, in processing source values, a fast pipe may be up to two chunks ahead of a slower one. This keeps a balance of progress, and prevents any pipe from getting too far ahead. On the other hand, this slows down fast pipes until slower ones catch up. To ameliorate this, consider using a prefetch combinators on the slow pipes.

**Error** Any error raised from the input stream, or from any pipe, will stop the pulling from this stream and from any pipe, and the error will be raised by the resulting stream.

**Output**: the result stream collects and emits the outputs emitted from each pipe, mixed in an unknown way, with these guarantees: 1. each output chunk was emitted by one pipe exactly once. 2. chunks from each pipe come out of the resulting stream in the same order as they came out of the pipe, and without skipping any chunk.

Attributes

Source
Stream.scala
def buffer(n: Int): Stream[F, O]

Behaves like the identity function, but requests n elements at a time from the input.

Behaves like the identity function, but requests n elements at a time from the input.

Attributes

Example

scala> import cats.effect.SyncIO
scala> val buf = new scala.collection.mutable.ListBuffer[String]()
scala> Stream.range(0, 100).covary[SyncIO].
    |   evalMap(i => SyncIO { buf += s">$i"; i }).
    |   buffer(4).
    |   evalMap(i => SyncIO { buf += s"<$i"; i }).
    |   take(10).
    |   compile.toVector.unsafeRunSync()
res0: Vector[Int] = Vector(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> buf.toList
res1: List[String] = List(>0, >1, >2, >3, <0, <1, <2, <3, >4, >5, >6, >7, <4, <5, <6, <7, >8, >9, >10, >11, <8, <9)
Source
Stream.scala
def bufferAll: Stream[F, O]

Behaves like the identity stream, but emits no output until the source is exhausted.

Behaves like the identity stream, but emits no output until the source is exhausted.

Attributes

Example

scala> import cats.effect.SyncIO
scala> val buf = new scala.collection.mutable.ListBuffer[String]()
scala> Stream.range(0, 10).covary[SyncIO].
    |   evalMap(i => SyncIO { buf += s">$i"; i }).
    |   bufferAll.
    |   evalMap(i => SyncIO { buf += s"<$i"; i }).
    |   take(4).
    |   compile.toVector.unsafeRunSync()
res0: Vector[Int] = Vector(0, 1, 2, 3)
scala> buf.toList
res1: List[String] = List(>0, >1, >2, >3, >4, >5, >6, >7, >8, >9, <0, <1, <2, <3)
Source
Stream.scala
def bufferBy(f: O => Boolean): Stream[F, O]

Behaves like the identity stream, but requests elements from its input in blocks that end whenever the predicate switches from true to false.

Behaves like the identity stream, but requests elements from its input in blocks that end whenever the predicate switches from true to false.

Attributes

Example

scala> import cats.effect.SyncIO
scala> val buf = new scala.collection.mutable.ListBuffer[String]()
scala> Stream.range(0, 10).covary[SyncIO].
    |   evalMap(i => SyncIO { buf += s">$i"; i }).
    |   bufferBy(_ % 2 == 0).
    |   evalMap(i => SyncIO { buf += s"<$i"; i }).
    |   compile.toVector.unsafeRunSync()
res0: Vector[Int] = Vector(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> buf.toList
res1: List[String] = List(>0, >1, <0, <1, >2, >3, <2, <3, >4, >5, <4, <5, >6, >7, <6, <7, >8, >9, <8, <9)
Source
Stream.scala
def changes[O2 >: O](implicit eq: Eq[O2]): Stream[F, O2]

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.

Attributes

Example

scala> Stream(1,1,2,2,2,3,3).changes.toList
res0: List[Int] = List(1, 2, 3)
Source
Stream.scala
def changesBy[O2](f: O => O2)(implicit eq: Eq[O2]): Stream[F, O]

Emits only elements that are distinct from their immediate predecessors according to f, using natural equality for comparison.

Emits only elements that are distinct from their immediate predecessors according to f, using natural equality for comparison.

Note that f is called for each element in the stream multiple times and hence should be fast (e.g., an accessor). It is not intended to be used for computationally intensive conversions. For such conversions, consider something like: src.map(o => (o, f(o))).changesBy(_._2).map(_._1)

Attributes

Example

scala> Stream(1,1,2,4,6,9).changesBy(_ % 2).toList
res0: List[Int] = List(1, 2, 9)
Source
Stream.scala
def chunkAll: Stream[F, Chunk[O]]

Collects all output chunks in to a single chunk and emits it at the end of the source stream.

Collects all output chunks in to a single chunk and emits it at the end of the source stream. Note: if more than 2(32-1) elements are collected, this operation will fail.

Attributes

Example

scala> (Stream(1) ++ Stream(2, 3) ++ Stream(4, 5, 6)).chunkAll.toList
res0: List[Chunk[Int]] = List(Chunk(1, 2, 3, 4, 5, 6))
Source
Stream.scala
def chunkLimit(n: Int): Stream[F, Chunk[O]]

Outputs chunk with a limited maximum size, splitting as necessary.

Outputs chunk with a limited maximum size, splitting as necessary.

Attributes

Example

scala> (Stream(1) ++ Stream(2, 3) ++ Stream(4, 5, 6)).chunkLimit(2).toList
res0: List[Chunk[Int]] = List(Chunk(1), Chunk(2, 3), Chunk(4, 5), Chunk(6))
Source
Stream.scala

Outputs chunks of size larger than N

Outputs chunks of size larger than N

Chunks from the source stream are split as necessary.

If allowFewerTotal is true, if the stream is smaller than N, should the elements be included

Attributes

Example

scala> (Stream(1,2) ++ Stream(3,4) ++ Stream(5,6,7)).chunkMin(3).toList
res0: List[Chunk[Int]] = List(Chunk(1, 2, 3, 4), Chunk(5, 6, 7))
Source
Stream.scala

Outputs chunks of size n.

Outputs chunks of size n.

Chunks from the source stream are split as necessary. If allowFewer is true, the last chunk that is emitted may have less than n elements.

Note: the emitted chunk may be a composite chunk (i.e., an instance of Chunk.Queue) and hence may not have O(1) lookup by index. Consider calling .map(_.compact) if indexed lookup is important.

Attributes

Example

scala> Stream(1,2,3).repeat.chunkN(2).take(5).toList
res0: List[Chunk[Int]] = List(Chunk(1, 2), Chunk(3, 1), Chunk(2, 3), Chunk(1, 2), Chunk(3, 1))
Source
Stream.scala
def chunks: Stream[F, Chunk[O]]

Outputs all chunks from the source stream.

Outputs all chunks from the source stream.

Attributes

Example

scala> (Stream(1) ++ Stream(2, 3) ++ Stream(4, 5, 6)).chunks.toList
res0: List[Chunk[Int]] = List(Chunk(1), Chunk(2, 3), Chunk(4, 5, 6))
Source
Stream.scala
def collect[O2](pf: PartialFunction[O, O2]): Stream[F, O2]

Filters and maps simultaneously.

Filters and maps simultaneously. Calls collect on each chunk in the stream.

Attributes

Example

scala> Stream(Some(1), Some(2), None, Some(3), None, Some(4)).collect { case Some(i) => i }.toList
res0: List[Int] = List(1, 2, 3, 4)
Source
Stream.scala
def collectFirst[O2](pf: PartialFunction[O, O2]): Stream[F, O2]

Emits the first element of the stream for which the partial function is defined.

Emits the first element of the stream for which the partial function is defined.

Attributes

Example

scala> Stream(None, Some(1), Some(2), None, Some(3)).collectFirst { case Some(i) => i }.toList
res0: List[Int] = List(1)
Source
Stream.scala
def collectWhile[O2](pf: PartialFunction[O, O2]): Stream[F, O2]

Like collect but terminates as soon as the partial function is undefined.

Like collect but terminates as soon as the partial function is undefined.

Attributes

Example

scala> Stream(Some(1), Some(2), Some(3), None, Some(4)).collectWhile { case Some(i) => i }.toList
res0: List[Int] = List(1, 2, 3)
Source
Stream.scala
def compile[F2[x], G[_], O2 >: O](implicit compiler: Compiler[F2, G]): CompileOps[F2, G, O2]

Gets a projection of this stream that allows converting it to an F[..] in a number of ways.

Gets a projection of this stream that allows converting it to an F[..] in a number of ways.

Attributes

Example

scala> import cats.effect.SyncIO
scala> val prg: SyncIO[Vector[Int]] = Stream.eval(SyncIO(1)).append(Stream(2,3,4)).compile.toVector
scala> prg.unsafeRunSync()
res2: Vector[Int] = Vector(1, 2, 3, 4)
Source
Stream.scala
def concurrently[F2[x], O2](that: Stream[F2, O2])(implicit F: Concurrent[F2]): Stream[F2, O]

Runs the supplied stream in the background as elements from this stream are pulled.

Runs the supplied stream in the background as elements from this stream are pulled.

The resulting stream terminates upon termination of this stream. The background stream will be interrupted at that point. Early termination of that does not terminate the resulting stream.

Any errors that occur in either this or that stream result in the overall stream terminating with an error.

Upon finalization, the resulting stream will interrupt the background stream and wait for it to be finalized.

This method is equivalent to this mergeHaltL that.drain, just more efficient for this and that evaluation.

Attributes

Example

scala> import cats.effect.IO, cats.effect.unsafe.implicits.global
scala> val data: Stream[IO,Int] = Stream.range(1, 10).covary[IO]
scala> Stream.eval(fs2.concurrent.SignallingRef[IO,Int](0)).flatMap(s => Stream(s).concurrently(data.evalMap(s.set))).flatMap(_.discrete).takeWhile(_ < 9, true).compile.last.unsafeRunSync()
res0: Option[Int] = Some(9)
Source
Stream.scala
def cons[O2 >: O](c: Chunk[O2]): Stream[F, O2]

Prepends a chunk onto the front of this stream.

Prepends a chunk onto the front of this stream.

Attributes

Example

scala> Stream(1,2,3).cons(Chunk(-1, 0)).toList
res0: List[Int] = List(-1, 0, 1, 2, 3)
Source
Stream.scala
def cons1[O2 >: O](o: O2): Stream[F, O2]

Prepends a single value onto the front of this stream.

Prepends a single value onto the front of this stream.

Attributes

Example

scala> Stream(1,2,3).cons1(0).toList
res0: List[Int] = List(0, 1, 2, 3)
Source
Stream.scala
def consChunk[O2 >: O](c: Chunk[O2]): Stream[F, O2]

Prepends a chunk onto the front of this stream.

Prepends a chunk onto the front of this stream.

Attributes

Example

scala> Stream(1,2,3).consChunk(Chunk.from(Vector(-1, 0))).toList
res0: List[Int] = List(-1, 0, 1, 2, 3)
Source
Stream.scala
def covary[F2[x]]: Stream[F2, O]
Implicitly added by InvariantOps

Lifts this stream to the specified effect type.

Lifts this stream to the specified effect type.

Attributes

Example

scala> import cats.effect.IO
scala> Stream(1, 2, 3).covary[IO]
res0: Stream[IO,Int] = Stream(..)
Source
Stream.scala
def covary[F[_]]: Stream[F, O]
Implicitly added by PureOps

Lifts this stream to the specified effect type.

Lifts this stream to the specified effect type.

Attributes

Source
Stream.scala
def covaryAll[F2[x], O2 >: O]: Stream[F2, O2]

Lifts this stream to the specified effect and output types.

Lifts this stream to the specified effect and output types.

Attributes

Example

scala> import cats.effect.IO
scala> Stream.empty.covaryAll[IO,Int]
res0: Stream[IO,Int] = Stream(..)
Source
Stream.scala
def covaryId[F[_] : Applicative]: Stream[F, O]
Implicitly added by IdOps

Attributes

Source
Stream.scala
def covaryOutput[O2 >: O]: Stream[F, O2]

Lifts this stream to the specified output type.

Lifts this stream to the specified output type.

Attributes

Example

scala> Stream(Some(1), Some(2), Some(3)).covaryOutput[Option[Int]]
res0: Stream[Pure,Option[Int]] = Stream(..)
Source
Stream.scala
def debounce[F2[x]](d: FiniteDuration)(implicit F: Temporal[F2]): Stream[F2, O]

Debounce the stream with a minimum period of d between each element.

Debounce the stream with a minimum period of d between each element.

Use-case: if this is a stream of updates about external state, we may want to refresh (side-effectful) once every 'd' milliseconds, and every time we refresh we only care about the latest update.

Attributes

Returns

A stream whose values is an in-order, not necessarily strict subsequence of this stream, and whose evaluation will force a delay d between emitting each element. The exact subsequence would depend on the chunk structure of this stream, and the timing they arrive.

Example

scala> import scala.concurrent.duration._, cats.effect.IO, cats.effect.unsafe.implicits.global
scala> val s = Stream(1, 2, 3) ++ Stream.sleep_[IO](500.millis) ++ Stream(4, 5) ++ Stream.sleep_[IO](10.millis) ++ Stream(6)
scala> val s2 = s.debounce(100.milliseconds)
scala> s2.compile.toVector.unsafeRunSync()
res0: Vector[Int] = Vector(3, 6)
Source
Stream.scala
def debug[O2 >: O](formatter: O2 => String, logger: String => Unit): Stream[F, O]

Logs the elements of this stream as they are pulled.

Logs the elements of this stream as they are pulled.

By default, toString is called on each element and the result is printed to standard out. To change formatting, supply a value for the formatter param. To change the destination, supply a value for the logger param.

This method does not change the chunk structure of the stream. To debug the chunk structure, see debugChunks.

Logging is not done in F because this operation is intended for debugging, including pure streams.

Attributes

Example

scala> Stream(1, 2).append(Stream(3, 4)).debug(o => s"a: $o").toList
a: 1
a: 2
a: 3
a: 4
res0: List[Int] = List(1, 2, 3, 4)
Source
Stream.scala
def debugChunks[O2 >: O](formatter: Chunk[O2] => String, logger: String => Unit): Stream[F, O]

Like debug but logs chunks as they are pulled instead of individual elements.

Like debug but logs chunks as they are pulled instead of individual elements.

Attributes

Example

scala> Stream(1, 2, 3).append(Stream(4, 5, 6)).debugChunks(c => s"a: $c").buffer(2).debugChunks(c => s"b: $c").toList
a: Chunk(1, 2, 3)
b: Chunk(1, 2)
a: Chunk(4, 5, 6)
b: Chunk(3, 4)
b: Chunk(5, 6)
res0: List[Int] = List(1, 2, 3, 4, 5, 6)
Source
Stream.scala

Returns a stream that when run, sleeps for duration d and then pulls from this stream.

Returns a stream that when run, sleeps for duration d and then pulls from this stream.

Alias for sleep_[F](d) ++ this.

Attributes

Source
Stream.scala
def delete(p: O => Boolean): Stream[F, O]

Skips the first element that matches the predicate.

Skips the first element that matches the predicate.

Attributes

Example

scala> Stream.range(1, 10).delete(_ % 2 == 0).toList
res0: List[Int] = List(1, 3, 4, 5, 6, 7, 8, 9)
Source
Stream.scala
def drain: Stream[F, Nothing]

Removes all output values from this stream.

Removes all output values from this stream.

Often used with merge to run one side of the merge for its effect while getting outputs from the opposite side of the merge.

Attributes

Example

scala> import cats.effect.SyncIO
scala> Stream.eval(SyncIO(println("x"))).drain.compile.toVector.unsafeRunSync()
res0: Vector[Nothing] = Vector()
Source
Stream.scala
def drop(n: Long): Stream[F, O]

Drops n elements of the input, then echoes the rest.

Drops n elements of the input, then echoes the rest.

Attributes

Example

scala> Stream.range(0,10).drop(5).toList
res0: List[Int] = List(5, 6, 7, 8, 9)
Source
Stream.scala
def dropLast: Stream[F, O]

Drops the last element.

Drops the last element.

Attributes

Example

scala> Stream.range(0,10).dropLast.toList
res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8)
Source
Stream.scala
def dropLastIf(p: O => Boolean): Stream[F, O]

Drops the last element if the predicate evaluates to true.

Drops the last element if the predicate evaluates to true.

Attributes

Example

scala> Stream.range(0,10).dropLastIf(_ > 5).toList
res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8)
Source
Stream.scala
def dropRight(n: Int): Stream[F, O]

Outputs all but the last n elements of the input.

Outputs all but the last n elements of the input.

This is a pure stream operation: if s is a finite pure stream, then s.dropRight(n).toList is equal to this.toList.reverse.drop(n).reverse.

Attributes

Example

scala> Stream.range(0,10).dropRight(5).toList
res0: List[Int] = List(0, 1, 2, 3, 4)
Source
Stream.scala
def dropThrough(p: O => Boolean): Stream[F, O]

Like dropWhile, but drops the first value which tests false.

Like dropWhile, but drops the first value which tests false.

Attributes

Example

scala> Stream.range(0,10).dropThrough(_ != 4).toList
res0: List[Int] = List(5, 6, 7, 8, 9)

Pure: if this is a finite pure stream, then this.dropThrough(p).toList is equal to this.toList.dropWhile(p).drop(1)

Source
Stream.scala
def dropWhile(p: O => Boolean): Stream[F, O]

Drops elements from the head of this stream until the supplied predicate returns false.

Drops elements from the head of this stream until the supplied predicate returns false.

Attributes

Example

scala> Stream.range(0,10).dropWhile(_ != 4).toList
res0: List[Int] = List(4, 5, 6, 7, 8, 9)

Pure this operation maps directly to List.dropWhile

Source
Stream.scala
def either[F2[x] : Concurrent, O2](that: Stream[F2, O2]): Stream[F2, Either[O, O2]]

Like merge, but tags each output with the branch it came from.

Like merge, but tags each output with the branch it came from.

Attributes

Example

scala> import scala.concurrent.duration._, cats.effect.IO, cats.effect.unsafe.implicits.global
scala> val s1 = Stream.awakeEvery[IO](1000.millis).scan(0)((acc, _) => acc + 1)
scala> val s = s1.either(Stream.sleep_[IO](500.millis) ++ s1).take(10)
scala> s.take(10).compile.toVector.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))
Source
Stream.scala
def enqueueNoneTerminated[F2[x], O2 >: O](queue: QueueSink[F2, Option[O2]]): Stream[F2, Nothing]

Enqueues the elements of this stream to the supplied queue and enqueues None when this stream terminates.

Enqueues the elements of this stream to the supplied queue and enqueues None when this stream terminates.

Attributes

Source
Stream.scala
def enqueueNoneTerminatedChunks[F2[x], O2 >: O](queue: QueueSink[F2, Option[Chunk[O2]]]): Stream[F2, Nothing]

Enqueues the chunks of this stream to the supplied queue and enqueues None when this stream terminates.

Enqueues the chunks of this stream to the supplied queue and enqueues None when this stream terminates.

Attributes

Source
Stream.scala
def enqueueUnterminated[F2[x], O2 >: O](queue: QueueSink[F2, O2]): Stream[F2, Nothing]

Enqueues the elements of this stream to the supplied queue.

Enqueues the elements of this stream to the supplied queue.

Attributes

Source
Stream.scala
def enqueueUnterminatedChunks[F2[x], O2 >: O](queue: QueueSink[F2, Chunk[O2]]): Stream[F2, Nothing]

Enqueues the chunks of this stream to the supplied queue.

Enqueues the chunks of this stream to the supplied queue.

Attributes

Source
Stream.scala
def evalFilter[F2[x]](f: O => F2[Boolean]): Stream[F2, O]

Like filter, but allows filtering based on an effect.

Like filter, but allows filtering based on an effect.

Note: The result Stream will consist of chunks that are empty or 1-element-long. If you want to operate on chunks after using it, consider buffering, e.g. by using buffer.

Attributes

Source
Stream.scala

Like filter, but allows filtering based on an effect, with up to maxConcurrent concurrently running effects.

Like filter, but allows filtering based on an effect, with up to maxConcurrent concurrently running effects. The ordering of emitted elements is unchanged.

Attributes

Source
Stream.scala
def evalFilterNot[F2[x]](f: O => F2[Boolean]): Stream[F2, O]

Like filterNot, but allows filtering based on an effect.

Like filterNot, but allows filtering based on an effect.

Note: The result Stream will consist of chunks that are empty or 1-element-long. If you want to operate on chunks after using it, consider buffering, e.g. by using buffer.

Attributes

Source
Stream.scala

Like filterNot, but allows filtering based on an effect, with up to maxConcurrent concurrently running effects.

Like filterNot, but allows filtering based on an effect, with up to maxConcurrent concurrently running effects. The ordering of emitted elements is unchanged.

Attributes

Source
Stream.scala
def evalMap[F2[x], O2](f: O => F2[O2]): Stream[F2, O2]

Alias for flatMap(o => Stream.eval(f(o))).

Alias for flatMap(o => Stream.eval(f(o))).

Attributes

Example

scala> import cats.effect.SyncIO
scala> Stream(1,2,3,4).evalMap(i => SyncIO(println(i))).compile.drain.unsafeRunSync()
res0: Unit = ()

Note this operator will de-chunk the stream back into chunks of size 1, which has performance implications. For maximum performance, evalMapChunk is available, however, with caveats.

Source
Stream.scala
def evalMapAccumulate[F2[x], S, O2](s: S)(f: (S, O) => F2[(S, O2)]): Stream[F2, (S, O2)]

Like Stream#mapAccumulate, but accepts a function returning an F[_].

Like Stream#mapAccumulate, but accepts a function returning an F[_].

Attributes

Example

scala> import cats.effect.SyncIO
scala> Stream(1,2,3,4).covary[SyncIO].evalMapAccumulate(0)((acc,i) => SyncIO((i, acc + i))).compile.toVector.unsafeRunSync()
res0: Vector[(Int, Int)] = Vector((1,1), (2,3), (3,5), (4,7))
Source
Stream.scala
def evalMapChunk[F2[x] : Applicative, O2](f: O => F2[O2]): Stream[F2, O2]

Like evalMap, but operates on chunks for performance.

Like evalMap, but operates on chunks for performance. This means this operator is not lazy on every single element, rather on the chunks.

For instance, evalMap would only print twice in the follow example (note the take(2)):

Attributes

Example

scala> import cats.effect.SyncIO
scala> Stream(1,2,3,4).evalMap(i => SyncIO(println(i))).take(2).compile.drain.unsafeRunSync()
res0: Unit = ()

But with evalMapChunk, it will print 4 times:

scala> Stream(1,2,3,4).evalMapChunk(i => SyncIO(println(i))).take(2).compile.drain.unsafeRunSync()
res0: Unit = ()
Source
Stream.scala
def evalMapFilter[F2[x], O2](f: O => F2[Option[O2]]): Stream[F2, O2]

Effectfully maps and filters the elements of the stream depending on the optionality of the result of the application of the effectful function f.

Effectfully maps and filters the elements of the stream depending on the optionality of the result of the application of the effectful function f.

Attributes

Example

scala> import cats.effect.SyncIO, cats.syntax.all._
scala> Stream(1, 2, 3, 4, 5).evalMapFilter(n => SyncIO((n * 2).some.filter(_ % 4 == 0))).compile.toList.unsafeRunSync()
res0: List[Int] = List(4, 8)
Source
Stream.scala
def evalScan[F2[x], O2](z: O2)(f: (O2, O) => F2[O2]): Stream[F2, O2]

Like Stream#scan, but accepts a function returning an F[_].

Like Stream#scan, but accepts a function returning an F[_].

Attributes

Example

scala> import cats.effect.SyncIO
scala> Stream(1,2,3,4).covary[SyncIO].evalScan(0)((acc,i) => SyncIO(acc + i)).compile.toVector.unsafeRunSync()
res0: Vector[Int] = Vector(0, 1, 3, 6, 10)
Source
Stream.scala
def evalTap[F2[x], O2](f: O => F2[O2]): Stream[F2, O]

Like observe but observes with a function O => F[O2] instead of a pipe.

Like observe but observes with a function O => F[O2] instead of a pipe. Not as powerful as observe since not all pipes can be represented by O => F[O2], but much faster. Alias for evalMap(o => f(o).as(o)).

Attributes

Source
Stream.scala
def evalTapChunk[F2[x] : Applicative, O2](f: O => F2[O2]): Stream[F2, O]

Alias for evalMapChunk(o => f(o).as(o)).

Alias for evalMapChunk(o => f(o).as(o)).

Attributes

Source
Stream.scala
def exists(p: O => Boolean): Stream[F, Boolean]

Emits true as soon as a matching element is received, else false if no input matches.

Emits true as soon as a matching element is received, else false if no input matches. Pure: this operation maps to List.exists

Attributes

Returns

Either a singleton stream, or a never stream. - If this is a finite stream, the result is a singleton stream, yielding a single boolean value. - If this is empty, the result is a singleton stream, yielding a true value. - If this is a non-terminating stream which contains a value matching the predicate, the result is a singleton stream containing true. - If this is a non-terminating stream which never contains a value matching the predicate, the result is a never stream.

Example

scala> Stream.range(0,10).exists(_ == 4).toList
res0: List[Boolean] = List(true)
scala> Stream.range(0,10).exists(_ == 10).toList
res1: List[Boolean] = List(false)
Source
Stream.scala
def filter(p: O => Boolean): Stream[F, O]

Emits only inputs which match the supplied predicate.

Emits only inputs which match the supplied predicate.

This is a pure operation, that projects directly into List.filter

Attributes

Example

scala> Stream.range(0,10).filter(_ % 2 == 0).toList
res0: List[Int] = List(0, 2, 4, 6, 8)
Source
Stream.scala
def filterNot(p: O => Boolean): Stream[F, O]

Emits only inputs which do not match the supplied predicate.

Emits only inputs which do not match the supplied predicate.

Attributes

Source
Stream.scala
def filterWithPrevious(f: (O, O) => Boolean): Stream[F, O]

Like filter, but the predicate f depends on the previously emitted and current elements.

Like filter, but the predicate f depends on the previously emitted and current elements.

Attributes

Example

scala> Stream(1, 9, 5, 6, 7, 8, 9, 10).filterWithPrevious((previous, current) => previous < current).toList
res0: List[Int] = List(1, 9, 10)
Source
Stream.scala
def find(f: O => Boolean): Stream[F, O]

Emits the first input (if any) which matches the supplied predicate.

Emits the first input (if any) which matches the supplied predicate.

Attributes

Example

scala> Stream.range(1,10).find(_ % 2 == 0).toList
res0: List[Int] = List(2)

Pure if s is a finite pure stream, s.find(p).toList is equal to s.toList.find(p).toList, where the second toList is to turn Option into List.

Source
Stream.scala
def flatMap[F2[x], O2](f: O => Stream[F2, O2])(implicit ev: NotGiven[O <:< Nothing]): Stream[F2, O2]

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.

Attributes

Example

scala> Stream(1, 2, 3).flatMap { i => Stream.chunk(Chunk.from(List.fill(i)(i))) }.toList
res0: List[Int] = List(1, 2, 2, 3, 3, 3)
Source
Stream.scala
def flatten[F2[x], O2](implicit ev: O <:< Stream[F2, O2]): Stream[F2, O2]

Flattens a stream of streams in to a single stream by concatenating each stream.

Flattens a stream of streams in to a single stream by concatenating each stream. See parJoin and parJoinUnbounded for concurrent flattening of 'n' streams.

Attributes

Source
Stream.scala
def fold[O2](z: O2)(f: (O2, O) => O2): Stream[F, O2]

Folds all inputs using an initial value z and supplied binary operator, and emits a single element stream.

Folds all inputs using an initial value z and supplied binary operator, and emits a single element stream.

Attributes

Example

scala> Stream(1, 2, 3, 4, 5).fold(0)(_ + _).toList
res0: List[Int] = List(15)
Source
Stream.scala
def fold1[O2 >: O](f: (O2, O2) => O2): Stream[F, O2]

Folds all inputs using the supplied binary operator, and emits a single-element stream, or the empty stream if the input is empty, or the never stream if the input is non-terminating.

Folds all inputs using the supplied binary operator, and emits a single-element stream, or the empty stream if the input is empty, or the never stream if the input is non-terminating.

Attributes

Example

scala> Stream(1, 2, 3, 4, 5).fold1(_ + _).toList
res0: List[Int] = List(15)
Source
Stream.scala
def foldMap[O2](f: O => O2)(implicit O2: Monoid[O2]): Stream[F, O2]

Alias for map(f).foldMonoid.

Alias for map(f).foldMonoid.

Attributes

Example

scala> Stream(1, 2, 3, 4, 5).foldMap(_ => 1).toList
res0: List[Int] = List(5)
Source
Stream.scala
def foldMonoid[O2 >: O](implicit O: Monoid[O2]): Stream[F, O2]

Folds this stream with the monoid for O.

Folds this stream with the monoid for O.

Attributes

Returns

Either a singleton stream or a never stream: - If this is a finite stream, the result is a singleton stream. If this is empty, that value is the mempty of the instance of Monoid. - If this is a non-terminating stream, and no matter if it yields any value, then the result is equivalent to the Stream.never: it never terminates nor yields any value.

Example

scala> Stream(1, 2, 3, 4, 5).foldMonoid.toList
res0: List[Int] = List(15)
Source
Stream.scala
def forall(p: O => Boolean): Stream[F, Boolean]

Emits false and halts as soon as a non-matching element is received; or emits a single true value if it reaches the stream end and every input before that matches the predicate; or hangs without emitting values if the input is infinite and all inputs match the predicate.

Emits false and halts as soon as a non-matching element is received; or emits a single true value if it reaches the stream end and every input before that matches the predicate; or hangs without emitting values if the input is infinite and all inputs match the predicate.

Attributes

Returns

Either a singleton or a never stream: - If this yields an element x for which ¬ p(x), then a singleton stream with the value false. Pulling from the resultg performs all the effects needed until reaching the counterexample x. - If this is a finite stream with no counterexamples of p, then a singleton stream with the true value. Pulling from the it will perform all effects of this. - If this is an infinite stream and all its the elements satisfy p, then the result is a never stream. Pulling from that stream will pull all effects from this.

Example

scala> Stream(1, 2, 3, 4, 5).forall(_ < 10).toList
res0: List[Boolean] = List(true)
Source
Stream.scala
def foreach[F2[x]](f: O => F2[Unit]): Stream[F2, Nothing]

Like evalMap but discards the result of evaluation, resulting in a stream with no elements.

Like evalMap but discards the result of evaluation, resulting in a stream with no elements.

Attributes

Example

scala> import cats.effect.SyncIO
scala> Stream(1,2,3,4).foreach(i => SyncIO(println(i))).compile.drain.unsafeRunSync()
res0: Unit = ()
Source
Stream.scala
def groupAdjacentBy[O2](f: O => O2)(implicit eq: Eq[O2]): Stream[F, (O2, Chunk[O])]

Partitions the input into a stream of chunks according to a discriminator function.

Partitions the input into a stream of chunks according to a discriminator function.

Each chunk in the source stream is grouped using the supplied discriminator function and the results of the grouping are emitted each time the discriminator function changes values.

Note: there is no limit to how large a group can become. To limit the group size, use groupAdjacentByLimit.

Attributes

Example

scala> Stream("Hello", "Hi", "Greetings", "Hey").groupAdjacentBy(_.head).toList.map { case (k,vs) => k -> vs.toList }
res0: List[(Char,List[String])] = List((H,List(Hello, Hi)), (G,List(Greetings)), (H,List(Hey)))
Source
Stream.scala
def groupAdjacentByLimit[O2](limit: Int)(f: O => O2)(implicit eq: Eq[O2]): Stream[F, (O2, Chunk[O])]

Like groupAdjacentBy but limits the size of emitted chunks.

Like groupAdjacentBy but limits the size of emitted chunks.

Attributes

Example

scala> Stream.range(0, 12).groupAdjacentByLimit(3)(_ / 4).toList
res0: List[(Int,Chunk[Int])] = List((0,Chunk(0, 1, 2)), (0,Chunk(3)), (1,Chunk(4, 5, 6)), (1,Chunk(7)), (2,Chunk(8, 9, 10)), (2,Chunk(11)))
Source
Stream.scala

Splits this stream into a stream of chunks of elements, such that 1.

Splits this stream into a stream of chunks of elements, such that 1. each chunk in the output has at most outputSize elements, and 2. the concatenation of those chunks, which is obtained by calling unchunks, yields the same element sequence as this stream.

As this stream emits input elements, the result stream them in a waiting buffer, until it has enough elements to emit next chunk.

To avoid holding input elements for too long, this method takes a timeout. This timeout is reset after each output chunk is emitted.

When the timeout expires, if the buffer contains any elements, then all elements in the buffer are emitted in an output chunk, even if there are fewer than chunkSize elements, and the timeout is reset.

However, if the buffer is empty when the timeout expires, then the output stream enters into a "timed out" state. From it, as soon as this stream emits the next chunk of input, the resulting stream will emit its next output chunk and reset timeout again. If that input chunk is shorter than the chunkSize, it is emitted whole. Otherwise, only the first chunkSize elements are emitted, and the rest are put in the buffer.

When the input stream terminates, any accumulated elements are emitted immediately in a chunk, even if timeout has not expired.

Value parameters

chunkSize

the maximum size of chunks emitted by resulting stream.

timeout

maximum time that input elements are held in the buffer before being emitted by the resulting stream.

Attributes

Source
Stream.scala
def handleErrorWith[F2[x], O2 >: O](h: Throwable => Stream[F2, O2]): Stream[F2, O2]

If this terminates with Stream.raiseError(e), invoke h(e).

If this terminates with Stream.raiseError(e), invoke h(e).

Attributes

Example

scala> import cats.effect.SyncIO
scala> Stream(1, 2, 3).append(Stream.raiseError[SyncIO](new RuntimeException)).handleErrorWith(_ => Stream(0)).compile.toList.unsafeRunSync()
res0: List[Int] = List(1, 2, 3, 0)
Source
Stream.scala
def head: Stream[F, O]

Emits the first element of this stream (if non-empty) and then halts.

Emits the first element of this stream (if non-empty) and then halts.

Attributes

Example

scala> Stream(1, 2, 3).head.toList
res0: List[Int] = List(1)
Source
Stream.scala
def hold[F2[x] : Concurrent, O2 >: O](initial: O2): Stream[F2, Signal[F2, O2]]

Converts a discrete stream to a signal.

Converts a discrete stream to a signal. Returns a single-element stream.

Resulting signal is initially initial, and is updated with latest value produced by source. If the source stream is empty, the resulting signal will always be initial.

Attributes

Source
Stream.scala
def hold1[F2[x] : Concurrent, O2 >: O]: Stream[F2, Signal[F2, O2]]

Like hold but does not require an initial value.

Like hold but does not require an initial value. The signal is not emitted until the initial value is emitted from this stream

Attributes

Source
Stream.scala

Like hold1 but returns a Resource rather than a single element stream.

Like hold1 but returns a Resource rather than a single element stream.

Attributes

Source
Stream.scala
def holdOption[F2[x] : Concurrent, O2 >: O]: Stream[F2, Signal[F2, Option[O2]]]

Like hold but does not require an initial value, and hence all output elements are wrapped in Some.

Like hold but does not require an initial value, and hence all output elements are wrapped in Some.

Attributes

Source
Stream.scala

Like holdResource but does not require an initial value, and hence all output elements are wrapped in Some.

Like holdResource but does not require an initial value, and hence all output elements are wrapped in Some.

Attributes

Source
Stream.scala
def holdResource[F2[x] : Concurrent, O2 >: O](initial: O2): Resource[F2, Signal[F2, O2]]

Like hold but returns a Resource rather than a single element stream.

Like hold but returns a Resource rather than a single element stream.

Attributes

Source
Stream.scala
def ifEmpty[F2[x], O2 >: O](fallback: => Stream[F2, O2]): Stream[F2, O2]

Falls back to the supplied stream if this stream finishes without emitting any elements.

Falls back to the supplied stream if this stream finishes without emitting any elements. Note: fallback occurs any time stream evaluation finishes without emitting, even when effects have been evaluated.

Attributes

Example

scala> Stream.empty.ifEmpty(Stream(1, 2, 3)).toList
res0: List[Int] = List(1, 2, 3)
scala> Stream.exec(cats.effect.SyncIO(println("Hello"))).ifEmpty(Stream(1, 2, 3)).compile.toList.unsafeRunSync()
res1: List[Int] = List(1, 2, 3)
Source
Stream.scala
def ifEmptyEmit[O2 >: O](o: => O2): Stream[F, O2]

Emits the supplied value if this stream finishes without emitting any elements.

Emits the supplied value if this stream finishes without emitting any elements. Note: fallback occurs any time stream evaluation finishes without emitting, even when effects have been evaluated.

Attributes

Example

scala> Stream.empty.ifEmptyEmit(0).toList
res0: List[Int] = List(0)
Source
Stream.scala
def interleave[F2[x], O2 >: O](that: Stream[F2, O2]): Stream[F2, O2]

Deterministically interleaves elements, starting on the left, terminating when the end of either branch is reached naturally.

Deterministically interleaves elements, starting on the left, terminating when the end of either branch is reached naturally.

Attributes

Example

scala> Stream(1, 2, 3).interleave(Stream(4, 5, 6, 7)).toList
res0: List[Int] = List(1, 4, 2, 5, 3, 6)
Source
Stream.scala
def interleaveAll[F2[x], O2 >: O](that: Stream[F2, O2]): Stream[F2, O2]

Deterministically interleaves elements, starting on the left, terminating when the ends of both branches are reached naturally.

Deterministically interleaves elements, starting on the left, terminating when the ends of both branches are reached naturally.

Attributes

Example

scala> Stream(1, 2, 3).interleaveAll(Stream(4, 5, 6, 7)).toList
res0: List[Int] = List(1, 4, 2, 5, 3, 6, 7)
Source
Stream.scala
def interleaveOrdered[F2[x], O2 >: O : Order](that: Stream[F2, O2]): Stream[F2, O2]

Given two sorted streams emits a single sorted stream, like in merge-sort.

Given two sorted streams emits a single sorted stream, like in merge-sort. For entries that are considered equal by the Order, left stream element is emitted first. Note: both this and another streams MUST BE ORDERED already

Attributes

Example

scala> Stream(1, 2, 5, 6).interleaveOrdered(Stream(0, 2, 3, 4)).toList
res0: List[Int] = List(0, 1, 2, 2, 3, 4, 5, 6)
Source
Stream.scala

Interrupts this stream after the specified duration has passed.

Interrupts this stream after the specified duration has passed.

Attributes

Source
Stream.scala
def interruptScope: Stream[F, O]

Creates a scope that may be interrupted by calling scope#interrupt.

Creates a scope that may be interrupted by calling scope#interrupt.

Attributes

Source
Stream.scala
def interruptWhen[F2[x]](haltWhenTrue: Stream[F2, Boolean])(implicit F: Concurrent[F2]): Stream[F2, O]

Ties this stream to the given haltWhenTrue stream.

Ties this stream to the given haltWhenTrue stream. The resulting stream performs all the effects and emits all the outputs from this stream (the fore), until the moment that the haltWhenTrue stream ends, be it by emitting true, error, or cancellation.

The haltWhenTrue stream is compiled and drained, asynchronously in the background, until the moment it emits a value true or raises an error. This halts as soon as either branch halts.

If the haltWhenTrue stream ends by raising an error, the resulting stream rethrows that same error. If the haltWhenTrue stream is cancelled, then the resulting stream is interrupted (without cancellation).

Consider using the overload that takes a Signal, Deferred or F[Either[Throwable, Unit]].

Attributes

Source
Stream.scala

Alias for interruptWhen(haltWhenTrue.get).

Alias for interruptWhen(haltWhenTrue.get).

Attributes

Source
Stream.scala

Alias for interruptWhen(haltWhenTrue.discrete).

Alias for interruptWhen(haltWhenTrue.discrete).

Attributes

Source
Stream.scala

Interrupts the stream, when haltOnSignal finishes its evaluation.

Interrupts the stream, when haltOnSignal finishes its evaluation.

Attributes

Source
Stream.scala
def intersperse[O2 >: O](separator: O2): Stream[F, O2]

Emits the specified separator between every pair of elements in the source stream.

Emits the specified separator between every pair of elements in the source stream.

Attributes

Example

scala> Stream(1, 2, 3, 4, 5).intersperse(0).toList
res0: List[Int] = List(1, 0, 2, 0, 3, 0, 4, 0, 5)

This method preserves the Chunking structure of this stream.

Source
Stream.scala
def last: Stream[F, Option[O]]

Returns the last element of this stream, if non-empty.

Returns the last element of this stream, if non-empty.

Attributes

Example

scala> Stream(1, 2, 3).last.toList
res0: List[Option[Int]] = List(Some(3))
Source
Stream.scala
def lastOr[O2 >: O](fallback: => O2): Stream[F, O2]

Returns the last element of this stream, if non-empty, otherwise the supplied fallback value.

Returns the last element of this stream, if non-empty, otherwise the supplied fallback value.

Attributes

Example

scala> Stream(1, 2, 3).lastOr(0).toList
res0: List[Int] = List(3)
scala> Stream.empty.lastOr(0).toList
res1: List[Int] = List(0)
Source
Stream.scala
def lift[F[_]](implicit F: ApplicativeError[F, Throwable]): Stream[F, O]
Implicitly added by FallibleOps

Lifts this stream to the specified effect type.

Lifts this stream to the specified effect type.

Attributes

Source
Stream.scala
def limit[F2[x]](n: Long)(implicit rt: RaiseThrowable[F2]): Stream[F2, O]

Emits the first n elements of this stream, raising an IllegalStateException if there are more elements.

Emits the first n elements of this stream, raising an IllegalStateException if there are more elements.

Attributes

Source
Stream.scala
def map[O2](f: O => O2): Stream[F, O2]

Applies the specified pure function to each input and emits the result.

Applies the specified pure function to each input and emits the result.

Attributes

Example

scala> Stream("Hello", "World!").map(_.size).toList
res0: List[Int] = List(5, 6)
Source
Stream.scala
def mapAccumulate[S, O2](init: S)(f: (S, O) => (S, O2)): Stream[F, (S, O2)]

Maps a running total according to S and the input with the function f.

Maps a running total according to S and the input with the function f.

Attributes

Example

scala> Stream("Hello", "World").mapAccumulate(0)((l, s) => (l + s.length, s.head)).toVector
res0: Vector[(Int, Char)] = Vector((5,H), (10,W))
Source
Stream.scala
def mapAsync[F2[x] : Concurrent, O2](maxConcurrent: Int)(f: O => F2[O2]): Stream[F2, O2]

Alias for parEvalMap.

Alias for parEvalMap.

Attributes

Source
Stream.scala
def mapAsyncUnordered[F2[x] : Concurrent, O2](maxConcurrent: Int)(f: O => F2[O2]): Stream[F2, O2]

Alias for parEvalMapUnordered.

Alias for parEvalMapUnordered.

Attributes

Source
Stream.scala
def mapChunks[O2](f: Chunk[O] => Chunk[O2]): Stream[F, O2]

Applies the specified pure function to each chunk in this stream.

Applies the specified pure function to each chunk in this stream.

Attributes

Example

scala> Stream(1, 2, 3).append(Stream(4, 5, 6)).mapChunks { c => val ints = c.toArraySlice; for (i <- 0 until ints.values.size) ints.values(i) = 0; ints }.toList
res0: List[Int] = List(0, 0, 0, 0, 0, 0)
Source
Stream.scala
def mask: Stream[F, O]

Behaves like the identity function but halts the stream on an error and does not return the error.

Behaves like the identity function but halts the stream on an error and does not return the error.

Attributes

Example

scala> import cats.effect.SyncIO
scala> (Stream(1,2,3) ++ Stream.raiseError[SyncIO](new RuntimeException) ++ Stream(4, 5, 6)).mask.compile.toList.unsafeRunSync()
res0: List[Int] = List(1, 2, 3)
Source
Stream.scala
def merge[F2[x], O2 >: O](that: Stream[F2, O2])(implicit F: Concurrent[F2]): Stream[F2, O2]

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(raiseError(e), s) will eventually terminate with raiseError(e), possibly after emitting some elements of s first.

The implementation always tries to pull one chunk from each side before waiting for it to be consumed by resulting stream. As such, there may be up to two chunks (one from each stream) waiting to be processed while the resulting stream is processing elements.

Also note that if either side produces empty chunk, the processing on that side continues, w/o downstream requiring to consume result.

If either side does not emit anything (i.e. as result of drain) that side will continue to run even when the resulting stream did not ask for more data.

Note that even when this is equivalent to Stream(this, that).parJoinUnbounded, this implementation is little more efficient

Attributes

Example

scala> import scala.concurrent.duration._, cats.effect.IO, cats.effect.unsafe.implicits.global
scala> val s1 = Stream.awakeEvery[IO](500.millis).scan(0)((acc, _) => acc + 1)
scala> val s = s1.merge(Stream.sleep_[IO](250.millis) ++ s1)
scala> s.take(6).compile.toVector.unsafeRunSync()
res0: Vector[Int] = Vector(0, 0, 1, 1, 2, 2)
Source
Stream.scala
def mergeHaltBoth[F2[x] : Concurrent, O2 >: O](that: Stream[F2, O2]): Stream[F2, O2]

Like merge, but halts as soon as _either_ branch halts.

Like merge, but halts as soon as _either_ branch halts.

Attributes

Source
Stream.scala
def mergeHaltL[F2[x] : Concurrent, O2 >: O](that: Stream[F2, O2]): Stream[F2, O2]

Like merge, but halts as soon as the s1 branch halts.

Like merge, but halts as soon as the s1 branch halts.

Note: it is *not* guaranteed that the last element of the stream will come from s1.

Attributes

Source
Stream.scala
def mergeHaltR[F2[x] : Concurrent, O2 >: O](that: Stream[F2, O2]): Stream[F2, O2]

Like merge, but halts as soon as the s2 branch halts.

Like merge, but halts as soon as the s2 branch halts.

Note: it is *not* guaranteed that the last element of the stream will come from s2.

Attributes

Source
Stream.scala

Throttles the stream to the specified rate.

Throttles the stream to the specified rate. Unlike debounce, metered doesn't drop elements.

Provided rate should be viewed as maximum rate: resulting rate can't exceed the output rate of this stream.

Attributes

Source
Stream.scala

Provides the same functionality as metered but begins immediately instead of waiting for rate

Provides the same functionality as metered but begins immediately instead of waiting for rate

Attributes

Source
Stream.scala

Emits each output wrapped in a Some and emits a None at the end of the stream.

Emits each output wrapped in a Some and emits a None at the end of the stream.

s.noneTerminate.unNoneTerminate == s

Attributes

Example

scala> Stream(1,2,3).noneTerminate.toList
res0: List[Option[Int]] = List(Some(1), Some(2), Some(3), None)
Source
Stream.scala
def observe(p: (F, O) => Nothing)(implicit F: Concurrent[F]): Stream[F, O]
Implicitly added by InvariantOps

Synchronously sends values through p.

Synchronously sends values through p.

If p fails, then resulting stream will fail. If p halts the evaluation will halt too.

Note that observe will only output full chunks of O that are known to be successfully processed by p. So if p terminates/fails in the middle of chunk processing, the chunk will not be available in resulting stream.

Note that if your pipe can be represented by an O => F[Unit], evalTap will provide much greater performance.

Attributes

Example

scala> import cats.effect.IO, cats.effect.unsafe.implicits.global
scala> Stream(1, 2, 3).covary[IO].observe(_.printlns).map(_ + 1).compile.toVector.unsafeRunSync()
res0: Vector[Int] = Vector(2, 3, 4)
Source
Stream.scala
def observeAsync(maxQueued: Int)(pipe: (F, O) => Nothing)(implicit F: Concurrent[F]): Stream[F, O]
Implicitly added by InvariantOps

Attaches to this stream an observer pipe, that pre-inspects the outputs from this stream (the source) before the result stream emits them.

Attaches to this stream an observer pipe, that pre-inspects the outputs from this stream (the source) before the result stream emits them.

Outputs from the source are fed to the observer pipe, to build a stream that is run in the background. However, unlike the background method, the observe method binds the mainstream: if the observation stream reaches a stream end, the resulting stream is cut short.

The resulting stream emits the same outputs as the source (this) stream, in the same order and chunk structure. However, no chunk is emitted by the resulting stream until _after_ the observer pipe is done processing it.

Any errors raised either from the evaluation of the source stream (this) or from the observer pipe (when applied to source chunks) will cause the termination of the resulting stream, and will be raised from this.

Attributes

Returns

A stream that may emit the same outputs as this stream (source), in the same order and chunks, and performs the same effects as the source; but in which every chunk is processed by the pipe.

Source
Stream.scala
def observeEither[L, R](left: (F, L) => Nothing, right: (F, R) => Nothing)(implicit F: Concurrent[F], ev: O <:< Either[L, R]): Stream[F, Either[L, R]]
Implicitly added by InvariantOps

Observes this stream of Either[L, R] values with two pipes, one that observes left values and another that observes right values.

Observes this stream of Either[L, R] values with two pipes, one that observes left values and another that observes right values.

If either of left or right fails, then resulting stream will fail. If either halts the evaluation will halt too.

Attributes

Source
Stream.scala
def onComplete[F2[x], O2 >: O](s2: => Stream[F2, O2]): Stream[F2, O2]

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.

Attributes

Example

scala> Stream(1, 2, 3).onComplete(Stream(4, 5)).toList
res0: List[Int] = List(1, 2, 3, 4, 5)
Source
Stream.scala
def onFinalize[F2[x]](f: F2[Unit])(implicit F2: Applicative[F2]): Stream[F2, O]

Runs the supplied effectful action at the end of this stream, regardless of how the stream terminates.

Runs the supplied effectful action at the end of this stream, regardless of how the stream terminates.

Attributes

Source
Stream.scala
def onFinalizeCase[F2[x]](f: ExitCase => F2[Unit])(implicit F2: Applicative[F2]): Stream[F2, O]

Like onFinalize but provides the reason for finalization as an ExitCase[Throwable].

Like onFinalize but provides the reason for finalization as an ExitCase[Throwable].

Attributes

Source
Stream.scala
def onFinalizeCaseWeak[F2[x]](f: ExitCase => F2[Unit])(implicit F2: Applicative[F2]): Stream[F2, O]

Like onFinalizeCase but does not introduce a scope, allowing finalization to occur after subsequent appends or other scope-preserving transformations.

Like onFinalizeCase but does not introduce a scope, allowing finalization to occur after subsequent appends or other scope-preserving transformations.

Scopes can be manually introduced via scope if desired.

See onFinalizeWeak for more details on semantics.

Attributes

Source
Stream.scala
def onFinalizeWeak[F2[x]](f: F2[Unit])(implicit F2: Applicative[F2]): Stream[F2, O]

Like onFinalize but does not introduce a scope, allowing finalization to occur after subsequent appends or other scope-preserving transformations.

Like onFinalize but does not introduce a scope, allowing finalization to occur after subsequent appends or other scope-preserving transformations.

Scopes can be manually introduced via scope if desired.

Example use case: a.concurrently(b).onFinalizeWeak(f).compile.resource.use(g) In this example, use of onFinalize would result in b shutting down before g is run, because onFinalize creates a scope, whose lifetime is extended over the compiled resource. By using onFinalizeWeak instead, f is attached to the scope governing concurrently.

Attributes

Source
Stream.scala
def parEvalMap[F2[x], O2](maxConcurrent: Int)(f: O => F2[O2])(implicit F: Concurrent[F2]): Stream[F2, O2]

Like Stream#evalMap, but will evaluate effects in parallel, emitting the results downstream in the same order as the input stream.

Like Stream#evalMap, but will evaluate effects in parallel, emitting the results downstream in the same order as the input stream. The number of concurrent effects is limited by the maxConcurrent parameter.

See Stream#parEvalMapUnordered if there is no requirement to retain the order of the original stream.

Attributes

Example

scala> import cats.effect.IO, cats.effect.unsafe.implicits.global
scala> Stream(1,2,3,4).covary[IO].parEvalMap(2)(i => IO(println(i))).compile.drain.unsafeRunSync()
res0: Unit = ()
Source
Stream.scala
def parEvalMapUnbounded[F2[x], O2](f: O => F2[O2])(implicit F: Concurrent[F2]): Stream[F2, O2]

Like parEvalMap but with unbounded concurrency.

Like parEvalMap but with unbounded concurrency.

Attributes

Source
Stream.scala
def parEvalMapUnordered[F2[x], O2](maxConcurrent: Int)(f: O => F2[O2])(implicit F: Concurrent[F2]): Stream[F2, O2]

Like Stream#evalMap, but will evaluate effects in parallel, emitting the results downstream.

Like Stream#evalMap, but will evaluate effects in parallel, emitting the results downstream. The number of concurrent effects is limited by the maxConcurrent parameter.

See Stream#parEvalMap if retaining the original order of the stream is required.

Attributes

Example

scala> import cats.effect.IO, cats.effect.unsafe.implicits.global
scala> Stream(1,2,3,4).covary[IO].parEvalMapUnordered(2)(i => IO(println(i))).compile.drain.unsafeRunSync()
res0: Unit = ()
Source
Stream.scala
def parEvalMapUnorderedUnbounded[F2[x], O2](f: O => F2[O2])(implicit F: Concurrent[F2]): Stream[F2, O2]

Like parEvalMapUnordered but with unbounded concurrency.

Like parEvalMapUnordered but with unbounded concurrency.

Attributes

Source
Stream.scala
def parJoin(maxOpen: Int)(implicit F: Concurrent[F]): Stream[F, O]
Implicitly added by NestedStreamOps

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 but not before all inner streams terminate -- hence finalizers on the outer stream will run AFTER the LAST finalizer on the very last inner stream.

Finalizers on the returned stream are run after the outer stream has finished and all open inner streams have finished.

Value parameters

maxOpen

Maximum number of open inner streams at any time. Must be > 0.

Attributes

Source
Stream.scala
def parJoinUnbounded(implicit F: Concurrent[F]): Stream[F, O]
Implicitly added by NestedStreamOps

Like parJoin but races all inner streams simultaneously.

Like parJoin but races all inner streams simultaneously.

Attributes

Source
Stream.scala
def parZip[F2[x] : Concurrent, O2](that: Stream[F2, O2]): Stream[F2, (O, O2)]

Concurrent zip.

Concurrent zip.

It combines elements pairwise and in order like zip, but instead of pulling from the left stream and then from the right stream, it evaluates both pulls concurrently. The resulting stream terminates when either stream terminates.

The concurrency is bounded following a model of successive races: both sides start evaluation of a single element concurrently, and whichever finishes first waits for the other to catch up and the resulting pair to be emitted, at which point the process repeats. This means that no branch is allowed to get ahead by more than one element.

Notes: - Effects within each stream are executed in order, they are only concurrent with respect to each other. - The output of parZip is guaranteed to be the same as zip, although the order in which effects are executed differs.

Attributes

Source
Stream.scala
def parZipWith[F2[x] : Concurrent, O2 >: O, O3, O4](that: Stream[F2, O3])(f: (O2, O3) => O4): Stream[F2, O4]

Like parZip, but combines elements pairwise with a function instead of tupling them.

Like parZip, but combines elements pairwise with a function instead of tupling them.

Attributes

Source
Stream.scala

Pause this stream when pauseWhenTrue emits true, resuming when false is emitted.

Pause this stream when pauseWhenTrue emits true, resuming when false is emitted.

Attributes

Source
Stream.scala

Pause this stream when pauseWhenTrue is true, resume when it's false.

Pause this stream when pauseWhenTrue is true, resume when it's false.

Attributes

Source
Stream.scala
def prefetch[F2[x] : Concurrent]: Stream[F2, O]

Alias for prefetchN(1).

Alias for prefetchN(1).

Attributes

Source
Stream.scala
def prefetchN[F2[x] : Concurrent](n: Int): Stream[F2, O]

Behaves like identity, but starts fetches up to n chunks in parallel with downstream consumption, enabling processing on either side of the prefetchN to run in parallel.

Behaves like identity, but starts fetches up to n chunks in parallel with downstream consumption, enabling processing on either side of the prefetchN to run in parallel.

Attributes

Source
Stream.scala
def printlns[F2[x], O2 >: O](implicit F: Console[F2], showO: Show[O2]): Stream[F2, Nothing]

Prints each element of this stream to standard out, converting each element to a String via Show.

Prints each element of this stream to standard out, converting each element to a String via Show.

Attributes

Source
Stream.scala
def pull: ToPull[F, O]
Implicitly added by InvariantOps

Gets a projection of this stream that allows converting it to a Pull in a number of ways.

Gets a projection of this stream that allows converting it to a Pull in a number of ways.

Attributes

Source
Stream.scala

Rechunks the stream such that output chunks are within [inputChunk.size * minFactor, inputChunk.size * maxFactor].

Rechunks the stream such that output chunks are within [inputChunk.size * minFactor, inputChunk.size * maxFactor].

Attributes

Source
Stream.scala

Rechunks the stream such that output chunks are within [inputChunk.size * minFactor, inputChunk.size * maxFactor].

Rechunks the stream such that output chunks are within [inputChunk.size * minFactor, inputChunk.size * maxFactor]. The pseudo random generator is deterministic based on the supplied seed.

Attributes

Source
Stream.scala
def reduce[O2 >: O](f: (O2, O2) => O2): Stream[F, O2]

Alias for fold1.

Alias for fold1.

Attributes

Source
Stream.scala
def reduceSemigroup[O2 >: O](implicit S: Semigroup[O2]): Stream[F, O2]

Reduces this stream with the Semigroup for O.

Reduces this stream with the Semigroup for O.

Attributes

Example

scala> Stream("The", "quick", "brown", "fox").intersperse(" ").reduceSemigroup.toList
res0: List[String] = List(The quick brown fox)
Source
Stream.scala
def repartition[O2 >: O](f: O2 => Chunk[O2])(implicit S: Semigroup[O2]): Stream[F, O2]

Repartitions the input with the function f.

Repartitions the input with the function f. On each step f is applied to the input and all elements but the last of the resulting sequence are emitted. The last element is then appended to the next input using the Semigroup S.

Attributes

Example

scala> Stream("Hel", "l", "o Wor", "ld").repartition(s => Chunk.array(s.split(" "))).toList
res0: List[String] = List(Hello, World)
Source
Stream.scala
def repeat: Stream[F, O]

Repeat this stream an infinite number of times.

Repeat this stream an infinite number of times.

s.repeat == s ++ s ++ s ++ ...

Attributes

Example

scala> Stream(1,2,3).repeat.take(8).toList
res0: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2)
Source
Stream.scala
def repeatN(n: Long): Stream[F, O]

Repeat this stream a given number of times.

Repeat this stream a given number of times.

s.repeatN(n) == s ++ s ++ s ++ ... (n times)

Attributes

Example

scala> Stream(1,2,3).repeatN(3).take(100).toList
res0: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2, 3)
Source
Stream.scala
def repeatPull[O2](f: ToPull[F, O] => Pull[F, O2, Option[Stream[F, O]]]): Stream[F, O2]
Implicitly added by InvariantOps

Repeatedly invokes using, running the resultant Pull each time, halting when a pull returns None instead of Some(nextStream).

Repeatedly invokes using, running the resultant Pull each time, halting when a pull returns None instead of Some(nextStream).

Attributes

Source
Stream.scala
def rethrow[F2[x], O2](implicit ev: O <:< Either[Throwable, O2], rt: RaiseThrowable[F2]): Stream[F2, O2]

Converts a Stream[F,Either[Throwable,O]] to a Stream[F,O], which emits right values and fails upon the first Left(t).

Converts a Stream[F,Either[Throwable,O]] to a Stream[F,O], which emits right values and fails upon the first Left(t). Preserves chunkiness.

Attributes

Example

scala> import cats.effect.SyncIO
scala> Stream(Right(1), Right(2), Left(new RuntimeException), Right(3)).rethrow[SyncIO, Int].handleErrorWith(_ => Stream(-1)).compile.toList.unsafeRunSync()
res0: List[Int] = List(1, 2, -1)
Source
Stream.scala
def scan[O2](z: O2)(f: (O2, O) => O2): Stream[F, O2]

Left fold which outputs all intermediate results.

Left fold which outputs all intermediate results.

Attributes

Example

scala> Stream(1,2,3,4).scan(0)(_ + _).toList
res0: List[Int] = List(0, 1, 3, 6, 10)

More generally: Stream().scan(z)(f) == Stream(z) Stream(x1).scan(z)(f) == Stream(z, f(z,x1)) Stream(x1,x2).scan(z)(f) == Stream(z, f(z,x1), f(f(z,x1),x2)) etc

Source
Stream.scala
def scan1[O2 >: O](f: (O2, O2) => O2): Stream[F, O2]

Like scan, but uses the first element of the stream as the seed.

Like scan, but uses the first element of the stream as the seed.

Attributes

Example

scala> Stream(1,2,3,4).scan1(_ + _).toList
res0: List[Int] = List(1, 3, 6, 10)
Source
Stream.scala
def scanChunks[S, O2 >: O, O3](init: S)(f: (S, Chunk[O2]) => (S, Chunk[O3])): Stream[F, O3]

Like scan but f is applied to each chunk of the source stream.

Like scan but f is applied to each chunk of the source stream. The resulting chunk is emitted while the resulting state is used in the next invocation of f.

Many stateful pipes can be implemented efficiently (i.e., supporting fusion) with this method.

Attributes

Source
Stream.scala
def scanChunksOpt[S, O2 >: O, O3](init: S)(f: S => Option[Chunk[O2] => (S, Chunk[O3])]): Stream[F, O3]

More general version of scanChunks where the current state (i.e., S) can be inspected to determine if another chunk should be pulled or if the stream should terminate.

More general version of scanChunks where the current state (i.e., S) can be inspected to determine if another chunk should be pulled or if the stream should terminate. Termination is signaled by returning None from f. Otherwise, a function which consumes the next chunk is returned wrapped in Some.

Attributes

Example

scala> def take[F[_],O](s: Stream[F,O], n: Int): Stream[F,O] =
    |   s.scanChunksOpt(n) { n => if (n <= 0) None else Some((c: Chunk[O]) => if (c.size < n) (n - c.size, c) else (0, c.take(n))) }
scala> take(Stream.range(0,100), 5).toList
res0: List[Int] = List(0, 1, 2, 3, 4)
Source
Stream.scala
def scanMap[O2](f: O => O2)(implicit O2: Monoid[O2]): Stream[F, O2]

Alias for map(f).scanMonoid.

Alias for map(f).scanMonoid.

Attributes

Example

scala> Stream("a", "aa", "aaa", "aaaa").scanMap(_.length).toList
res0: List[Int] = List(0, 1, 3, 6, 10)
Source
Stream.scala
def scanMonoid[O2 >: O](implicit O: Monoid[O2]): Stream[F, O2]

Folds this stream with the monoid for O while emitting all intermediate results.

Folds this stream with the monoid for O while emitting all intermediate results.

Attributes

Example

scala> Stream(1, 2, 3, 4).scanMonoid.toList
res0: List[Int] = List(0, 1, 3, 6, 10)
Source
Stream.scala
def scope: Stream[F, O]

Introduces an explicit scope.

Introduces an explicit scope.

Scopes are normally introduced automatically, when using bracket or similar operations that acquire resources and run finalizers. Manual scope introduction is useful when using onFinalizeWeak/onFinalizeCaseWeak, where no scope is introduced.

Attributes

Source
Stream.scala
def sliding(n: Int): Stream[F, Chunk[O]]

Groups inputs in fixed size chunks by passing a "sliding window" of size n over them.

Groups inputs in fixed size chunks by passing a "sliding window" of size n over them. If the input contains less than or equal to n elements, only one chunk of this size will be emitted.

Attributes

Throws

scala.IllegalArgumentException if n <= 0

Example

scala> Stream(1, 2, 3, 4).sliding(2).toList
res0: List[fs2.Chunk[Int]] = List(Chunk(1, 2), Chunk(2, 3), Chunk(3, 4))
Source
Stream.scala
def sliding(size: Int, step: Int): Stream[F, Chunk[O]]

Groups inputs in fixed size chunks by passing a "sliding window" of size with step over them.

Groups inputs in fixed size chunks by passing a "sliding window" of size with step over them. If the input contains less than or equal to size elements, only one chunk of this size will be emitted.

Attributes

Throws

scala.IllegalArgumentException if size <= 0 | step <= 0

Example

scala> Stream(1, 2, 3, 4, 5).sliding(2, 3).toList
res0: List[fs2.Chunk[Int]] = List(Chunk(1, 2), Chunk(4, 5))
scala> Stream(1, 2, 3, 4, 5).sliding(3, 2).toList
res1: List[fs2.Chunk[Int]] = List(Chunk(1, 2, 3), Chunk(3, 4, 5))
Source
Stream.scala

Waits the specified delay between each event.

Waits the specified delay between each event.

The resulting stream emits the same elements from this stream, but split into singleton chunks. Between each chunk (element) it adds a pause of a fixed delay duration.

This method differs in the timing of elements from metered. The metered combinator takes a "schedule" for elements to be released, and before each element introduces just the necessary delay to hit that time. To do so, it deducts from the pause any delay caused by other effects in the stream, or the pauses the stream consumer takes while pulling. This method, instead, simply introduced a fixed sleep time between elements, irrespective of other pauses in the stream or the consumer.

Starts immediately, same as meteredStartImmediately unless parameter startImmediately is set to false.

Attributes

Source
Stream.scala
def spawn[F2[x]](implicit F: Concurrent[F2]): Stream[F2, Fiber[F2, Throwable, Unit]]

Starts this stream in the background and cancels it as finalization of the returned stream.

Starts this stream in the background and cancels it as finalization of the returned stream.

Any errors that occur in the background stream results in the foreground stream terminating with an error.

Attributes

Source
Stream.scala
def split(f: O => Boolean): Stream[F, Chunk[O]]

Breaks the input into chunks where the delimiter matches the predicate.

Breaks the input into chunks where the delimiter matches the predicate. The delimiter does not appear in the output. Two adjacent delimiters in the input result in an empty chunk in the output.

Attributes

Example

scala> Stream.range(0, 10).split(_ % 4 == 0).toList
res0: List[Chunk[Int]] = List(Chunk(), Chunk(1, 2, 3), Chunk(5, 6, 7), Chunk(9))
Source
Stream.scala
def switchMap[F2[x], O2](f: O => Stream[F2, O2])(implicit F: Concurrent[F2]): Stream[F2, O2]

Like Stream.flatMap but interrupts the inner stream when new elements arrive in the outer stream.

Like Stream.flatMap but interrupts the inner stream when new elements arrive in the outer stream.

The implementation will try to preserve chunks like Stream.merge.

Finializers of each inner stream are guaranteed to run before the next inner stream starts.

When the outer stream stops gracefully, the currently running inner stream will continue to run.

When an inner stream terminates/interrupts, nothing happens until the next element arrives in the outer stream(i.e the outer stream holds the stream open during this time or else the stream terminates)

When either the inner or outer stream fails, the entire stream fails and the finalizer of the inner stream runs before the outer one.

Attributes

Source
Stream.scala
def tail: Stream[F, O]

Emits all elements of the input except the first one.

Emits all elements of the input except the first one.

Attributes

Example

scala> Stream(1,2,3).tail.toList
res0: List[Int] = List(2, 3)
Source
Stream.scala
def take(n: Long): Stream[F, O]

Emits the first n elements of this stream.

Emits the first n elements of this stream.

Attributes

Example

scala> Stream.range(0,1000).take(5).toList
res0: List[Int] = List(0, 1, 2, 3, 4)
Source
Stream.scala
def takeRight(n: Int): Stream[F, O]

Emits the last n elements of the input.

Emits the last n elements of the input.

Attributes

Example

scala> Stream.range(0,1000).takeRight(5).toList
res0: List[Int] = List(995, 996, 997, 998, 999)
Source
Stream.scala
def takeThrough(p: O => Boolean): Stream[F, O]

Like takeWhile, but emits the first value which tests false.

Like takeWhile, but emits the first value which tests false.

Attributes

Example

scala> Stream.range(0,1000).takeThrough(_ != 5).toList
res0: List[Int] = List(0, 1, 2, 3, 4, 5)
Source
Stream.scala

Emits the longest prefix of the input for which all elements test true according to f.

Emits the longest prefix of the input for which all elements test true according to f.

Attributes

Example

scala> Stream.range(0,1000).takeWhile(_ != 5).toList
res0: List[Int] = List(0, 1, 2, 3, 4)
Source
Stream.scala
def through[F2[x], O2](f: Stream[F, O] => Stream[F2, O2]): Stream[F2, O2]

Transforms this stream using the given Pipe.

Transforms this stream using the given Pipe.

Attributes

Example

scala> Stream("Hello", "world").through(text.utf8.encode).toVector.toArray
res0: Array[Byte] = Array(72, 101, 108, 108, 111, 119, 111, 114, 108, 100)
Source
Stream.scala
def through2[F2[x], O2, O3](s2: Stream[F2, O2])(f: (Stream[F, O], Stream[F2, O2]) => Stream[F2, O3]): Stream[F2, O3]

Transforms this stream and s2 using the given Pipe2.

Transforms this stream and s2 using the given Pipe2.

Attributes

Source
Stream.scala

Fails this stream with a TimeoutException if it does not complete within given timeout.

Fails this stream with a TimeoutException if it does not complete within given timeout.

Attributes

Source
Stream.scala
Implicitly added by FallibleOps

Runs this fallible stream and returns the emitted elements in a collection of the specified type.

Runs this fallible stream and returns the emitted elements in a collection of the specified type. Note: this method is only available on fallible streams.

Attributes

Source
Stream.scala
def to(c: Collector[O]): Out
Implicitly added by PureOps

Runs this pure stream and returns the emitted elements in a collection of the specified type.

Runs this pure stream and returns the emitted elements in a collection of the specified type. Note: this method is only available on pure streams.

Attributes

Source
Stream.scala
Implicitly added by FallibleOps

Runs this fallible stream and returns the emitted elements in a list.

Runs this fallible stream and returns the emitted elements in a list. Note: this method is only available on fallible streams.

Attributes

Source
Stream.scala
def toList: List[O]
Implicitly added by PureOps

Runs this pure stream and returns the emitted elements in a list.

Runs this pure stream and returns the emitted elements in a list. Note: this method is only available on pure streams.

Attributes

Source
Stream.scala
override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Definition Classes
Any
Source
Stream.scala
Implicitly added by FallibleOps

Runs this fallible stream and returns the emitted elements in a vector.

Runs this fallible stream and returns the emitted elements in a vector. Note: this method is only available on fallible streams.

Attributes

Source
Stream.scala
def toVector: Vector[O]
Implicitly added by PureOps

Runs this pure stream and returns the emitted elements in a vector.

Runs this pure stream and returns the emitted elements in a vector. Note: this method is only available on pure streams.

Attributes

Source
Stream.scala
def translate[F2[x], G[_]](u: FunctionK[F2, G]): Stream[G, O]

Translates effect type from F to G using the supplied FunctionK.

Translates effect type from F to G using the supplied FunctionK.

Attributes

Source
Stream.scala
def unNone: Stream[F, O]
Implicitly added by OptionStreamOps

Filters any 'None'.

Filters any 'None'.

Attributes

Example

scala> Stream(Some(1), Some(2), None, Some(3), None).unNone.toList
res0: List[Int] = List(1, 2, 3)
Source
Stream.scala
def unNoneTerminate: Stream[F, O]
Implicitly added by OptionStreamOps

Halts the input stream at the first None.

Halts the input stream at the first None.

Attributes

Example

scala> Stream(Some(1), Some(2), None, Some(3), None).unNoneTerminate.toList
res0: List[Int] = List(1, 2)
Source
Stream.scala
def unchunks[O2](implicit ev: O <:< Chunk[O2]): Stream[F, O2]

Flattens a stream of chunks.

Flattens a stream of chunks. Inverse of chunks.

Attributes

Source
Stream.scala
def unitary: Stream[F, Unit]
Implicitly added by NothingStreamOps

Converts a Stream[F, Nothing] to a Stream[F, Unit] which emits a single () after this stream completes.

Converts a Stream[F, Nothing] to a Stream[F, Unit] which emits a single () after this stream completes.

Attributes

Source
Stream.scala
def withFilter(f: O => Boolean): Stream[F, O]

Alias for filter Implemented to enable filtering in for comprehensions

Alias for filter Implemented to enable filtering in for comprehensions

Attributes

Source
Stream.scala
def zip[F2[x], O2](that: Stream[F2, O2]): Stream[F2, (O, O2)]

Deterministically zips elements, terminating when the end of either branch is reached naturally.

Deterministically zips elements, terminating when the end of either branch is reached naturally.

Attributes

Example

scala> Stream(1, 2, 3).zip(Stream(4, 5, 6, 7)).toList
res0: List[(Int,Int)] = List((1,4), (2,5), (3,6))
Source
Stream.scala
def zipAll[F2[x], O2 >: O, O3](that: Stream[F2, O3])(pad1: O2, pad2: O3): Stream[F2, (O2, O3)]

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.

Attributes

Example

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))
Source
Stream.scala
def zipAllWith[F2[x], O2 >: O, O3, O4](that: Stream[F2, O3])(pad1: O2, pad2: O3)(f: (O2, O3) => O4): Stream[F2, O4]

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.

Attributes

Example

scala> Stream(1,2,3).zipAllWith(Stream(4,5,6,7))(0, 0)(_ + _).toList
res0: List[Int] = List(5, 7, 9, 7)
Source
Stream.scala
def zipLeft[F2[x], O2](that: Stream[F2, O2]): Stream[F2, O]

Like zip, but selects the left values only.

Like zip, but selects the left values only. Useful with timed streams, the example below will emit a number every 100 milliseconds.

Attributes

Example

scala> import scala.concurrent.duration._, cats.effect.IO, cats.effect.unsafe.implicits.global
scala> val s = Stream.range(0, 5) zipLeft Stream.fixedDelay[IO](100.millis)
scala> s.compile.toVector.unsafeRunSync()
res0: Vector[Int] = Vector(0, 1, 2, 3, 4)
Source
Stream.scala
def zipRight[F2[x], O2](that: Stream[F2, O2]): Stream[F2, O2]

Like zip, but selects the right values only.

Like zip, but selects the right values only. Useful with timed streams, the example below will emit a number every 100 milliseconds.

Attributes

Example

scala> import scala.concurrent.duration._, cats.effect.IO, cats.effect.unsafe.implicits.global
scala> val s = Stream.fixedDelay[IO](100.millis) zipRight Stream.range(0, 5)
scala> s.compile.toVector.unsafeRunSync()
res0: Vector[Int] = Vector(0, 1, 2, 3, 4)
Source
Stream.scala
def zipWith[F2[x], O2 >: O, O3, O4](that: Stream[F2, O3])(f: (O2, O3) => O4): Stream[F2, O4]

Deterministically zips elements using the specified function, terminating when the end of either branch is reached naturally.

Deterministically zips elements using the specified function, terminating when the end of either branch is reached naturally.

Attributes

Example

scala> Stream(1, 2, 3).zipWith(Stream(4, 5, 6, 7))(_ + _).toList
res0: List[Int] = List(5, 7, 9)
Source
Stream.scala
def zipWithIndex: Stream[F, (O, Long)]

Zips the elements of the input stream with its indices, and returns the new stream.

Zips the elements of the input stream with its indices, and returns the new stream.

Attributes

Example

scala> Stream("The", "quick", "brown", "fox").zipWithIndex.toList
res0: List[(String,Long)] = List((The,0), (quick,1), (brown,2), (fox,3))
Source
Stream.scala
def zipWithNext: Stream[F, (O, Option[O])]

Zips each element of this stream with the next element wrapped into Some.

Zips each element of this stream with the next element wrapped into Some. The last element is zipped with None.

Attributes

Example

scala> Stream("The", "quick", "brown", "fox").zipWithNext.toList
res0: List[(String,Option[String])] = List((The,Some(quick)), (quick,Some(brown)), (brown,Some(fox)), (fox,None))
Source
Stream.scala
def zipWithPrevious: Stream[F, (Option[O], O)]

Zips each element of this stream with the previous element wrapped into Some.

Zips each element of this stream with the previous element wrapped into Some. The first element is zipped with None.

Attributes

Example

scala> Stream("The", "quick", "brown", "fox").zipWithPrevious.toList
res0: List[(Option[String],String)] = List((None,The), (Some(The),quick), (Some(quick),brown), (Some(brown),fox))
Source
Stream.scala

Zips each element of this stream with its previous and next element wrapped into Some.

Zips each element of this stream with its previous and next element wrapped into Some. The first element is zipped with None as the previous element, the last element is zipped with None as the next element.

Attributes

Example

scala> Stream("The", "quick", "brown", "fox").zipWithPreviousAndNext.toList
res0: List[(Option[String],String,Option[String])] = List((None,The,Some(quick)), (Some(The),quick,Some(brown)), (Some(quick),brown,Some(fox)), (Some(brown),fox,None))
Source
Stream.scala
def zipWithScan[O2](z: O2)(f: (O2, O) => O2): Stream[F, (O, O2)]

Zips the input with a running total according to S, up to but not including the current element.

Zips the input with a running total according to S, up to but not including the current element. Thus the initial z value is the first emitted to the output:

Attributes

See also
Example

scala> Stream("uno", "dos", "tres", "cuatro").zipWithScan(0)(_ + _.length).toList
res0: List[(String,Int)] = List((uno,0), (dos,3), (tres,6), (cuatro,10))
Source
Stream.scala
def zipWithScan1[O2](z: O2)(f: (O2, O) => O2): Stream[F, (O, O2)]

Zips the input with a running total according to S, including the current element.

Zips the input with a running total according to S, including the current element. Thus the initial z value is the first emitted to the output:

Attributes

See also
Example

scala> Stream("uno", "dos", "tres", "cuatro").zipWithScan1(0)(_ + _.length).toList
res0: List[(String, Int)] = List((uno,3), (dos,6), (tres,10), (cuatro,16))
Source
Stream.scala

Deprecated methods

def translateInterruptible[F2[x], G[_]](u: FunctionK[F2, G]): Stream[G, O]

Translates effect type from F to G using the supplied FunctionK.

Translates effect type from F to G using the supplied FunctionK.

Attributes

Deprecated
true
Source
Stream.scala
def unchunk: Stream[F, O]

Converts the input to a stream of 1-element chunks.

Converts the input to a stream of 1-element chunks.

Attributes

Deprecated
true
Source
Stream.scala