Alias for _.map(_ => o2)
.
Alias for _.map(_ => o2)
.
scala> Stream(1,2,3).as(0).toList res0: List[Int] = List(0, 0, 0)
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
.
scala> (Stream(1,2,3) ++ Stream.raiseError(new RuntimeException) ++ Stream(4,5,6)).attempt.toList 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.
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.
scala> import cats.effect.IO scala> val buf = new scala.collection.mutable.ListBuffer[String]() scala> Stream.range(0, 100).covary[IO]. | evalMap(i => IO { buf += s">$i"; i }). | buffer(4). | evalMap(i => IO { 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)
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.
scala> import cats.effect.IO scala> val buf = new scala.collection.mutable.ListBuffer[String]() scala> Stream.range(0, 10).covary[IO]. | evalMap(i => IO { buf += s">$i"; i }). | bufferAll. | evalMap(i => IO { 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)
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.
scala> import cats.effect.IO scala> val buf = new scala.collection.mutable.ListBuffer[String]() scala> Stream.range(0, 10).covary[IO]. | evalMap(i => IO { buf += s">$i"; i }). | bufferBy(_ % 2 == 0). | evalMap(i => IO { 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)
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)
scala> import cats.implicits._ scala> Stream(1,1,2,4,6,9).changesBy(_ % 2).toList res0: List[Int] = List(1, 2, 9)
Outputs chunk with a limited maximum size, splitting as necessary.
Outputs chunk with a limited maximum size, splitting as necessary.
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))
Outputs all chunks from the source stream.
Outputs all chunks from the source stream.
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))
Filters and maps simultaneously.
Filters and maps simultaneously. Calls collect
on each segment in the stream.
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)
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.
scala> Stream(None, Some(1), Some(2), None, Some(3)).collectFirst { case Some(i) => i }.toList res0: List[Int] = List(1)
Prepends a segment onto the front of this stream.
Prepends a segment onto the front of this stream.
scala> Stream(1,2,3).cons(Segment.vector(Vector(-1, 0))).toList res0: List[Int] = List(-1, 0, 1, 2, 3)
Prepends a single value onto the front of this stream.
Prepends a single value onto the front of this stream.
scala> Stream(1,2,3).cons1(0).toList res0: List[Int] = List(0, 1, 2, 3)
Prepends a chunk onto the front of this stream.
Prepends a chunk onto the front of this stream.
scala> Stream(1,2,3).consChunk(Chunk.vector(Vector(-1, 0))).toList res0: List[Int] = List(-1, 0, 1, 2, 3)
Lifts this stream to the specified output type.
Lifts this stream to the specified output type.
scala> Stream(Some(1), Some(2), Some(3)).covaryOutput[Option[Int]] res0: Stream[Pure,Option[Int]] = Stream(..)
Skips the first element that matches the predicate.
Skips the first element that matches the predicate.
scala> Stream.range(1, 10).delete(_ % 2 == 0).toList res0: List[Int] = List(1, 3, 4, 5, 6, 7, 8, 9)
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.
scala> import cats.effect.IO scala> Stream.eval(IO(println("x"))).drain.compile.toVector.unsafeRunSync res0: Vector[Nothing] = Vector()
Drops n
elements of the input, then echoes the rest.
Drops n
elements of the input, then echoes the rest.
scala> Stream.range(0,10).drop(5).toList res0: List[Int] = List(5, 6, 7, 8, 9)
Drops the last element.
Drops the last element.
scala> Stream.range(0,10).dropLast.toList res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8)
Drops the last element if the predicate evaluates to true.
Drops the last element if the predicate evaluates to true.
scala> Stream.range(0,10).dropLastIf(_ > 5).toList res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8)
Outputs all but the last n
elements of the input.
Outputs all but the last n
elements of the input.
scala> Stream.range(0,10).dropRight(5).toList res0: List[Int] = List(0, 1, 2, 3, 4)
Like dropWhile, but drops the first value which tests false.
Like dropWhile, but drops the first value which tests false.
scala> Stream.range(0,10).dropThrough(_ != 4).toList res0: List[Int] = List(5, 6, 7, 8, 9)
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.
scala> Stream.range(0,10).dropWhile(_ != 4).toList res0: List[Int] = List(4, 5, 6, 7, 8, 9)
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.
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)
Emits only inputs which match the supplied predicate.
Emits only inputs which match the supplied predicate.
scala> Stream.range(0,10).filter(_ % 2 == 0).toList res0: List[Int] = List(0, 2, 4, 6, 8)
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.
scala> Stream(1, -1, 2, -2, 3, -3, 4, -4).filterWithPrevious((previous, current) => previous < current).toList res0: List[Int] = List(1, 2, 3, 4)
Emits the first input (if any) which matches the supplied predicate.
Emits the first input (if any) which matches the supplied predicate.
scala> Stream.range(1,10).find(_ % 2 == 0).toList res0: List[Int] = List(2)
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.
scala> Stream(1, 2, 3, 4, 5).fold(0)(_ + _).toList res0: List[Int] = List(15)
Folds all inputs using the supplied binary operator, and emits a single-element stream, or the empty stream if the input is empty.
Folds all inputs using the supplied binary operator, and emits a single-element stream, or the empty stream if the input is empty.
scala> Stream(1, 2, 3, 4, 5).fold1(_ + _).toList res0: List[Int] = List(15)
Alias for map(f).foldMonoid
.
Alias for map(f).foldMonoid
.
scala> import cats.implicits._ scala> Stream(1, 2, 3, 4, 5).foldMap(_ => 1).toList res0: List[Int] = List(5)
Emits a single true
value if all input matches the predicate.
Emits a single true
value if all input matches the predicate.
Halts with false
as soon as a non-matching element is received.
scala> Stream(1, 2, 3, 4, 5).forall(_ < 10).toList res0: List[Boolean] = List(true)
Partitions the input into a stream of segments according to a discriminator function.
Partitions the input into a stream of segments 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.
scala> import cats.implicits._ scala> Stream("Hello", "Hi", "Greetings", "Hey").groupAdjacentBy(_.head).toList.map { case (k,vs) => k -> vs.force.toList } res0: List[(Char,List[String])] = List((H,List(Hello, Hi)), (G,List(Greetings)), (H,List(Hey)))
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.
scala> Stream(1, 2, 3).head.toList res0: List[Int] = List(1)
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.
scala> Stream(1, 2, 3, 4, 5).intersperse(0).toList res0: List[Int] = List(1, 0, 2, 0, 3, 0, 4, 0, 5)
Returns the last element of this stream, if non-empty.
Returns the last element of this stream, if non-empty.
scala> Stream(1, 2, 3).last.toList res0: List[Option[Int]] = List(Some(3))
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.
scala> Stream(1, 2, 3).lastOr(0).toList res0: List[Int] = List(3) scala> Stream.empty.lastOr(0).toList res1: List[Int] = List(0)
Applies the specified pure function to each input and emits the result.
Applies the specified pure function to each input and emits the result.
scala> Stream("Hello", "World!").map(_.size).toList res0: List[Int] = List(5, 6)
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
.
scala> Stream("Hello", "World").mapAccumulate(0)((l, s) => (l + s.length, s.head)).toVector res0: Vector[(Int, Char)] = Vector((5,H), (10,W))
Applies the specified pure function to each chunk in this stream.
Applies the specified pure function to each chunk in this stream.
scala> Stream(1, 2, 3).append(Stream(4, 5, 6)).mapChunks { c => val ints = c.toInts; for (i <- 0 until ints.values.size) ints.values(i) = 0; ints.toSegment }.toList res0: List[Int] = List(0, 0, 0, 0, 0, 0)
Applies the specified pure function to each segment in this stream.
Applies the specified pure function to each segment in this stream.
scala> (Stream.range(1,5) ++ Stream.range(5,10)).mapSegments(s => s.scan(0)(_ + _).voidResult).toList res0: List[Int] = List(0, 1, 3, 6, 10, 0, 5, 11, 18, 26, 35)
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.
scala> (Stream(1,2,3) ++ Stream.raiseError(new RuntimeException) ++ Stream(4, 5, 6)).mask.toList res0: List[Int] = List(1, 2, 3)
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
scala> Stream(1,2,3).noneTerminate.toList res0: List[Option[Int]] = List(Some(1), Some(2), Some(3), None)
Alias for fold1.
Repeat this stream an infinite number of times.
Repeat this stream an infinite number of times.
s.repeat == s ++ s ++ s ++ ...
scala> Stream(1,2,3).repeat.take(8).toList res0: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2)
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.
scala> Stream(Right(1), Right(2), Left(new RuntimeException), Right(3)).rethrow.handleErrorWith(t => Stream(-1)).toList res0: List[Int] = List(-1)
Left fold which outputs all intermediate results.
Left fold which outputs all intermediate results.
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
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.
scala> Stream(1,2,3,4).scan1(_ + _).toList res0: List[Int] = List(1, 3, 6, 10)
Tracks any resources acquired during this stream and releases them when the stream completes.
Tracks any resources acquired during this stream and releases them when the stream completes.
Scopes are sometimes inserted automatically, (e.g., as a result of calling handleErrorWith
).
This method allows a scope to be explicitly demarcated, so that resources can be freed earlier
than when using automatically inserted scopes.
Outputs the segments of this stream as output values, ensuring each segment has maximum size n
, splitting segments as necessary.
Outputs the segments of this stream as output values, ensuring each segment has maximum size n
, splitting segments as necessary.
scala> Stream(1,2,3).repeat.segmentLimit(2).take(5).toList res0: List[Segment[Int,Unit]] = List(Chunk(1, 2), Chunk(3), Chunk(1, 2), Chunk(3), Chunk(1, 2))
Outputs segments of size n
.
Outputs segments of size n
.
Segments from the source stream are split as necessary.
If allowFewer
is true, the last segment that is emitted may have less than n
elements.
scala> Stream(1,2,3).repeat.segmentN(2).take(5).toList res0: List[Segment[Int,Unit]] = List(Chunk(1, 2), catenated(Chunk(3), Chunk(1)), Chunk(2, 3), Chunk(1, 2), catenated(Chunk(3), Chunk(1)))
Outputs all segments from the source stream.
Outputs all segments from the source stream.
scala> Stream(1,2,3).repeat.segments.take(5).toList res0: List[Segment[Int,Unit]] = List(Chunk(1, 2, 3), Chunk(1, 2, 3), Chunk(1, 2, 3), Chunk(1, 2, 3), Chunk(1, 2, 3))
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.
scala> Stream(1, 2, 3, 4).sliding(2).toList res0: List[scala.collection.immutable.Queue[Int]] = List(Queue(1, 2), Queue(2, 3), Queue(3, 4))
scala.IllegalArgumentException
if n
<= 0
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.
scala> Stream.range(0, 10).split(_ % 4 == 0).toList res0: List[Segment[Int,Unit]] = List(empty, catenated(Chunk(1), Chunk(2), Chunk(3), empty), catenated(Chunk(5), Chunk(6), Chunk(7), empty), Chunk(9))
Emits all elements of the input except the first one.
Emits all elements of the input except the first one.
scala> Stream(1,2,3).tail.toList res0: List[Int] = List(2, 3)
Emits the first n
elements of this stream.
Emits the first n
elements of this stream.
scala> Stream.range(0,1000).take(5).toList res0: List[Int] = List(0, 1, 2, 3, 4)
Emits the last n
elements of the input.
Emits the last n
elements of the input.
scala> Stream.range(0,1000).takeRight(5).toList res0: List[Int] = List(995, 996, 997, 998, 999)
Like takeWhile, but emits the first value which tests false.
Like takeWhile, but emits the first value which tests false.
scala> Stream.range(0,1000).takeThrough(_ != 5).toList res0: List[Int] = List(0, 1, 2, 3, 4, 5)
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
.
scala> Stream.range(0,1000).takeWhile(_ != 5).toList res0: List[Int] = List(0, 1, 2, 3, 4)
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.
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.
Filters any 'None'.
Filters any 'None'.
scala> Stream(Some(1), Some(2), None, Some(3), None).unNone.toList res0: List[Int] = List(1, 2, 3)
Halts the input stream at the first None
.
Halts the input stream at the first None
.
scala> Stream(Some(1), Some(2), None, Some(3), None).unNoneTerminate.toList res0: List[Int] = List(1, 2)
Converts the input to a stream of 1-element chunks.
Converts the input to a stream of 1-element chunks.
scala> (Stream(1,2,3) ++ Stream(4,5,6)).unchunk.segments.toList res0: List[Segment[Int,Unit]] = List(Chunk(1), Chunk(2), Chunk(3), Chunk(4), Chunk(5), Chunk(6))
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.
scala> Stream("The", "quick", "brown", "fox").zipWithIndex.toList res0: List[(String,Long)] = List((The,0), (quick,1), (brown,2), (fox,3))
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
.
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))
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
.
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))
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.
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))
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:
scala> Stream("uno", "dos", "tres", "cuatro").zipWithScan(0)(_ + _.length).toList res0: List[(String,Int)] = List((uno,0), (dos,3), (tres,6), (cuatro,10))
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:
scala> Stream("uno", "dos", "tres", "cuatro").zipWithScan1(0)(_ + _.length).toList res0: List[(String, Int)] = List((uno,3), (dos,6), (tres,10), (cuatro,16))
Alias for _.map(_ => o2)
.
Alias for _.map(_ => o2)
.
(stream: Stream[Nothing, O]).as(o2)
scala> Stream(1,2,3).as(0).toList res0: List[Int] = List(0, 0, 0)
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
.
(stream: Stream[Nothing, O]).attempt
scala> (Stream(1,2,3) ++ Stream.raiseError(new RuntimeException) ++ Stream(4,5,6)).attempt.toList 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.
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.
(stream: Stream[Nothing, O]).buffer(n)
scala> import cats.effect.IO scala> val buf = new scala.collection.mutable.ListBuffer[String]() scala> Stream.range(0, 100).covary[IO]. | evalMap(i => IO { buf += s">$i"; i }). | buffer(4). | evalMap(i => IO { 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)
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.
(stream: Stream[Nothing, O]).bufferAll
scala> import cats.effect.IO scala> val buf = new scala.collection.mutable.ListBuffer[String]() scala> Stream.range(0, 10).covary[IO]. | evalMap(i => IO { buf += s">$i"; i }). | bufferAll. | evalMap(i => IO { 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)
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.
(stream: Stream[Nothing, O]).bufferBy(f)
scala> import cats.effect.IO scala> val buf = new scala.collection.mutable.ListBuffer[String]() scala> Stream.range(0, 10).covary[IO]. | evalMap(i => IO { buf += s">$i"; i }). | bufferBy(_ % 2 == 0). | evalMap(i => IO { 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)
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)
(stream: Stream[Nothing, O]).changesBy(f)(eq)
scala> import cats.implicits._ scala> Stream(1,1,2,4,6,9).changesBy(_ % 2).toList res0: List[Int] = List(1, 2, 9)
Outputs chunk with a limited maximum size, splitting as necessary.
Outputs chunk with a limited maximum size, splitting as necessary.
(stream: Stream[Nothing, O]).chunkLimit(n)
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))
Outputs all chunks from the source stream.
Outputs all chunks from the source stream.
(stream: Stream[Nothing, O]).chunks
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))
Filters and maps simultaneously.
Filters and maps simultaneously. Calls collect
on each segment in the stream.
(stream: Stream[Nothing, O]).collect(pf)
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)
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.
(stream: Stream[Nothing, O]).collectFirst(pf)
scala> Stream(None, Some(1), Some(2), None, Some(3)).collectFirst { case Some(i) => i }.toList res0: List[Int] = List(1)
Prepends a segment onto the front of this stream.
Prepends a segment onto the front of this stream.
(stream: Stream[Nothing, O]).cons(s)
scala> Stream(1,2,3).cons(Segment.vector(Vector(-1, 0))).toList res0: List[Int] = List(-1, 0, 1, 2, 3)
Prepends a single value onto the front of this stream.
Prepends a single value onto the front of this stream.
(stream: Stream[Nothing, O]).cons1(o)
scala> Stream(1,2,3).cons1(0).toList res0: List[Int] = List(0, 1, 2, 3)
Prepends a chunk onto the front of this stream.
Prepends a chunk onto the front of this stream.
(stream: Stream[Nothing, O]).consChunk(c)
scala> Stream(1,2,3).consChunk(Chunk.vector(Vector(-1, 0))).toList res0: List[Int] = List(-1, 0, 1, 2, 3)
(stream: PureOps[O]).covary
Lifts this stream to the specified effect type.
Lifts this stream to the specified effect type.
(stream: EmptyOps).covary
(stream: PureOps[O]).covaryAll
Lifts this stream to the specified effect and output types.
Lifts this stream to the specified effect and output types.
(stream: EmptyOps).covaryAll
Lifts this stream to the specified output type.
Lifts this stream to the specified output type.
(stream: Stream[Nothing, O]).covaryOutput
scala> Stream(Some(1), Some(2), Some(3)).covaryOutput[Option[Int]] res0: Stream[Pure,Option[Int]] = Stream(..)
Skips the first element that matches the predicate.
Skips the first element that matches the predicate.
(stream: Stream[Nothing, O]).delete(p)
scala> Stream.range(1, 10).delete(_ % 2 == 0).toList res0: List[Int] = List(1, 3, 4, 5, 6, 7, 8, 9)
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.
(stream: Stream[Nothing, O]).drain
scala> import cats.effect.IO scala> Stream.eval(IO(println("x"))).drain.compile.toVector.unsafeRunSync res0: Vector[Nothing] = Vector()
Drops n
elements of the input, then echoes the rest.
Drops n
elements of the input, then echoes the rest.
(stream: Stream[Nothing, O]).drop(n)
scala> Stream.range(0,10).drop(5).toList res0: List[Int] = List(5, 6, 7, 8, 9)
Drops the last element.
Drops the last element.
(stream: Stream[Nothing, O]).dropLast
scala> Stream.range(0,10).dropLast.toList res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8)
Drops the last element if the predicate evaluates to true.
Drops the last element if the predicate evaluates to true.
(stream: Stream[Nothing, O]).dropLastIf(p)
scala> Stream.range(0,10).dropLastIf(_ > 5).toList res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8)
Outputs all but the last n
elements of the input.
Outputs all but the last n
elements of the input.
(stream: Stream[Nothing, O]).dropRight(n)
scala> Stream.range(0,10).dropRight(5).toList res0: List[Int] = List(0, 1, 2, 3, 4)
Like dropWhile, but drops the first value which tests false.
Like dropWhile, but drops the first value which tests false.
(stream: Stream[Nothing, O]).dropThrough(p)
scala> Stream.range(0,10).dropThrough(_ != 4).toList res0: List[Int] = List(5, 6, 7, 8, 9)
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.
(stream: Stream[Nothing, O]).dropWhile(p)
scala> Stream.range(0,10).dropWhile(_ != 4).toList res0: List[Int] = List(4, 5, 6, 7, 8, 9)
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.
(stream: Stream[Nothing, O]).exists(p)
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)
Emits only inputs which match the supplied predicate.
Emits only inputs which match the supplied predicate.
(stream: Stream[Nothing, O]).filter(p)
scala> Stream.range(0,10).filter(_ % 2 == 0).toList res0: List[Int] = List(0, 2, 4, 6, 8)
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.
(stream: Stream[Nothing, O]).filterWithPrevious(f)
scala> Stream(1, -1, 2, -2, 3, -3, 4, -4).filterWithPrevious((previous, current) => previous < current).toList res0: List[Int] = List(1, 2, 3, 4)
Emits the first input (if any) which matches the supplied predicate.
Emits the first input (if any) which matches the supplied predicate.
(stream: Stream[Nothing, O]).find(f)
scala> Stream.range(1,10).find(_ % 2 == 0).toList res0: List[Int] = List(2)
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.
(stream: Stream[Nothing, O]).fold(z)(f)
scala> Stream(1, 2, 3, 4, 5).fold(0)(_ + _).toList res0: List[Int] = List(15)
Folds all inputs using the supplied binary operator, and emits a single-element stream, or the empty stream if the input is empty.
Folds all inputs using the supplied binary operator, and emits a single-element stream, or the empty stream if the input is empty.
(stream: Stream[Nothing, O]).fold1(f)
scala> Stream(1, 2, 3, 4, 5).fold1(_ + _).toList res0: List[Int] = List(15)
Alias for map(f).foldMonoid
.
Alias for map(f).foldMonoid
.
(stream: Stream[Nothing, O]).foldMap(f)(O2)
scala> import cats.implicits._ scala> Stream(1, 2, 3, 4, 5).foldMap(_ => 1).toList res0: List[Int] = List(5)
Emits a single true
value if all input matches the predicate.
Emits a single true
value if all input matches the predicate.
Halts with false
as soon as a non-matching element is received.
(stream: Stream[Nothing, O]).forall(p)
scala> Stream(1, 2, 3, 4, 5).forall(_ < 10).toList res0: List[Boolean] = List(true)
Partitions the input into a stream of segments according to a discriminator function.
Partitions the input into a stream of segments 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.
(stream: Stream[Nothing, O]).groupAdjacentBy(f)(eq)
scala> import cats.implicits._ scala> Stream("Hello", "Hi", "Greetings", "Hey").groupAdjacentBy(_.head).toList.map { case (k,vs) => k -> vs.force.toList } res0: List[(Char,List[String])] = List((H,List(Hello, Hi)), (G,List(Greetings)), (H,List(Hey)))
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.
(stream: Stream[Nothing, O]).head
scala> Stream(1, 2, 3).head.toList res0: List[Int] = List(1)
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.
(stream: Stream[Nothing, O]).intersperse(separator)
scala> Stream(1, 2, 3, 4, 5).intersperse(0).toList res0: List[Int] = List(1, 0, 2, 0, 3, 0, 4, 0, 5)
Returns the last element of this stream, if non-empty.
Returns the last element of this stream, if non-empty.
(stream: Stream[Nothing, O]).last
scala> Stream(1, 2, 3).last.toList res0: List[Option[Int]] = List(Some(3))
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.
(stream: Stream[Nothing, O]).lastOr(fallback)
scala> Stream(1, 2, 3).lastOr(0).toList res0: List[Int] = List(3) scala> Stream.empty.lastOr(0).toList res1: List[Int] = List(0)
Applies the specified pure function to each input and emits the result.
Applies the specified pure function to each input and emits the result.
(stream: Stream[Nothing, O]).map(f)
scala> Stream("Hello", "World!").map(_.size).toList res0: List[Int] = List(5, 6)
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
.
(stream: Stream[Nothing, O]).mapAccumulate(init)(f)
scala> Stream("Hello", "World").mapAccumulate(0)((l, s) => (l + s.length, s.head)).toVector res0: Vector[(Int, Char)] = Vector((5,H), (10,W))
Applies the specified pure function to each chunk in this stream.
Applies the specified pure function to each chunk in this stream.
(stream: Stream[Nothing, O]).mapChunks(f)
scala> Stream(1, 2, 3).append(Stream(4, 5, 6)).mapChunks { c => val ints = c.toInts; for (i <- 0 until ints.values.size) ints.values(i) = 0; ints.toSegment }.toList res0: List[Int] = List(0, 0, 0, 0, 0, 0)
Applies the specified pure function to each segment in this stream.
Applies the specified pure function to each segment in this stream.
(stream: Stream[Nothing, O]).mapSegments(f)
scala> (Stream.range(1,5) ++ Stream.range(5,10)).mapSegments(s => s.scan(0)(_ + _).voidResult).toList res0: List[Int] = List(0, 1, 3, 6, 10, 0, 5, 11, 18, 26, 35)
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.
(stream: Stream[Nothing, O]).mask
scala> (Stream(1,2,3) ++ Stream.raiseError(new RuntimeException) ++ Stream(4, 5, 6)).mask.toList res0: List[Int] = List(1, 2, 3)
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
(stream: Stream[Nothing, O]).noneTerminate
scala> Stream(1,2,3).noneTerminate.toList res0: List[Option[Int]] = List(Some(1), Some(2), Some(3), None)
Alias for fold1.
Alias for fold1.
(stream: Stream[Nothing, O]).reduce(f)
Repeat this stream an infinite number of times.
Repeat this stream an infinite number of times.
s.repeat == s ++ s ++ s ++ ...
(stream: Stream[Nothing, O]).repeat
scala> Stream(1,2,3).repeat.take(8).toList res0: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2)
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.
(stream: Stream[Nothing, O]).rethrow(ev)
scala> Stream(Right(1), Right(2), Left(new RuntimeException), Right(3)).rethrow.handleErrorWith(t => Stream(-1)).toList res0: List[Int] = List(-1)
Left fold which outputs all intermediate results.
Left fold which outputs all intermediate results.
(stream: Stream[Nothing, O]).scan(z)(f)
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
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.
(stream: Stream[Nothing, O]).scan1(f)
scala> Stream(1,2,3,4).scan1(_ + _).toList res0: List[Int] = List(1, 3, 6, 10)
Tracks any resources acquired during this stream and releases them when the stream completes.
Tracks any resources acquired during this stream and releases them when the stream completes.
Scopes are sometimes inserted automatically, (e.g., as a result of calling handleErrorWith
).
This method allows a scope to be explicitly demarcated, so that resources can be freed earlier
than when using automatically inserted scopes.
(stream: Stream[Nothing, O]).scope
Outputs the segments of this stream as output values, ensuring each segment has maximum size n
, splitting segments as necessary.
Outputs the segments of this stream as output values, ensuring each segment has maximum size n
, splitting segments as necessary.
(stream: Stream[Nothing, O]).segmentLimit(n)
scala> Stream(1,2,3).repeat.segmentLimit(2).take(5).toList res0: List[Segment[Int,Unit]] = List(Chunk(1, 2), Chunk(3), Chunk(1, 2), Chunk(3), Chunk(1, 2))
Outputs segments of size n
.
Outputs segments of size n
.
Segments from the source stream are split as necessary.
If allowFewer
is true, the last segment that is emitted may have less than n
elements.
(stream: Stream[Nothing, O]).segmentN(n, allowFewer)
scala> Stream(1,2,3).repeat.segmentN(2).take(5).toList res0: List[Segment[Int,Unit]] = List(Chunk(1, 2), catenated(Chunk(3), Chunk(1)), Chunk(2, 3), Chunk(1, 2), catenated(Chunk(3), Chunk(1)))
Outputs all segments from the source stream.
Outputs all segments from the source stream.
(stream: Stream[Nothing, O]).segments
scala> Stream(1,2,3).repeat.segments.take(5).toList res0: List[Segment[Int,Unit]] = List(Chunk(1, 2, 3), Chunk(1, 2, 3), Chunk(1, 2, 3), Chunk(1, 2, 3), Chunk(1, 2, 3))
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.
(stream: Stream[Nothing, O]).sliding(n)
scala> Stream(1, 2, 3, 4).sliding(2).toList res0: List[scala.collection.immutable.Queue[Int]] = List(Queue(1, 2), Queue(2, 3), Queue(3, 4))
scala.IllegalArgumentException
if n
<= 0
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.
(stream: Stream[Nothing, O]).split(f)
scala> Stream.range(0, 10).split(_ % 4 == 0).toList res0: List[Segment[Int,Unit]] = List(empty, catenated(Chunk(1), Chunk(2), Chunk(3), empty), catenated(Chunk(5), Chunk(6), Chunk(7), empty), Chunk(9))
Emits all elements of the input except the first one.
Emits all elements of the input except the first one.
(stream: Stream[Nothing, O]).tail
scala> Stream(1,2,3).tail.toList res0: List[Int] = List(2, 3)
Emits the first n
elements of this stream.
Emits the first n
elements of this stream.
(stream: Stream[Nothing, O]).take(n)
scala> Stream.range(0,1000).take(5).toList res0: List[Int] = List(0, 1, 2, 3, 4)
Emits the last n
elements of the input.
Emits the last n
elements of the input.
(stream: Stream[Nothing, O]).takeRight(n)
scala> Stream.range(0,1000).takeRight(5).toList res0: List[Int] = List(995, 996, 997, 998, 999)
Like takeWhile, but emits the first value which tests false.
Like takeWhile, but emits the first value which tests false.
(stream: Stream[Nothing, O]).takeThrough(p)
scala> Stream.range(0,1000).takeThrough(_ != 5).toList res0: List[Int] = List(0, 1, 2, 3, 4, 5)
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
.
(stream: Stream[Nothing, O]).takeWhile(p, takeFailure)
scala> Stream.range(0,1000).takeWhile(_ != 5).toList res0: List[Int] = List(0, 1, 2, 3, 4)
(stream: Stream[Nothing, O]).toString()
Filters any 'None'.
Filters any 'None'.
(stream: Stream[Nothing, O]).unNone(ev)
scala> Stream(Some(1), Some(2), None, Some(3), None).unNone.toList res0: List[Int] = List(1, 2, 3)
Halts the input stream at the first None
.
Halts the input stream at the first None
.
(stream: Stream[Nothing, O]).unNoneTerminate(ev)
scala> Stream(Some(1), Some(2), None, Some(3), None).unNoneTerminate.toList res0: List[Int] = List(1, 2)
Converts the input to a stream of 1-element chunks.
Converts the input to a stream of 1-element chunks.
(stream: Stream[Nothing, O]).unchunk
scala> (Stream(1,2,3) ++ Stream(4,5,6)).unchunk.segments.toList res0: List[Segment[Int,Unit]] = List(Chunk(1), Chunk(2), Chunk(3), Chunk(4), Chunk(5), Chunk(6))
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.
(stream: Stream[Nothing, O]).zipWithIndex
scala> Stream("The", "quick", "brown", "fox").zipWithIndex.toList res0: List[(String,Long)] = List((The,0), (quick,1), (brown,2), (fox,3))
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
.
(stream: Stream[Nothing, O]).zipWithNext
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))
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
.
(stream: Stream[Nothing, O]).zipWithPrevious
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))
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.
(stream: Stream[Nothing, O]).zipWithPreviousAndNext
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))
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:
(stream: Stream[Nothing, O]).zipWithScan(z)(f)
scala> Stream("uno", "dos", "tres", "cuatro").zipWithScan(0)(_ + _.length).toList res0: List[(String,Int)] = List((uno,0), (dos,3), (tres,6), (cuatro,10))
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:
(stream: Stream[Nothing, O]).zipWithScan1(z)(f)
scala> Stream("uno", "dos", "tres", "cuatro").zipWithScan1(0)(_ + _.length).toList res0: List[(String, Int)] = List((uno,3), (dos,6), (tres,10), (cuatro,16))
A stream producing output of type
O
and which may evaluateF
effects. IfF
is Pure, the stream evaluates no effects.Much of the API of
Stream
is defined in Stream.InvariantOps.Laws (using infix syntax):
append
forms a monoid in conjunction withempty
:empty append s == s
ands append empty == s
.(s1 append s2) append s3 == s1 append (s2 append s3)
And
cons
is consistent with using++
to prepend a single segment:s.cons(seg) == Stream.segment(seg) ++ s
Stream.raiseError
propagates until being caught byhandleErrorWith
: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 withemit
andflatMap
:Stream.emit >=> f == f
(left identity)f >=> Stream.emit === f
(right identity - note weaker equality notion here)(f >=> g) >=> h == f >=> (g >=> h)
(associativity) whereStream.emit(a)
is defined assegment(Segment.singleton(a)) and
f >=> gis defined as
a => a flatMap f flatMap gThe 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 segment structure of the stream is observable, and
s flatMap Stream.emit
produces a stream of singleton segments, the right identity law uses a weaker notion of equality,===
which normalizes both sides with respect to segment structure:(s1 === s2) = normalize(s1) == normalize(s2)
where==
is full equality (a == b
ifff(a)
is identical tof(b)
for allf
)normalize(s)
can be defined ass.flatMap(Stream.emit)
, which just produces a singly-chunked stream from any input streams
.Note: For efficiency
Stream.map
function operates on an entire segment at a time and preserves segment structure, which differs from themap
derived from the monad (s map f == s flatMap (f andThen Stream.emit)
) which would produce singleton segments. In particular, iff
throws errors, the segmented version will fail on the first segment with an error, while the unsegmented version will fail on the first element with an error. Exceptions in pure code like this are strongly discouraged.