Eagerly drops n
elements from the head of this segment, returning either the result and the
number of elements remaining to drop, if the end of the segment was reached, or a new segment,
if the end of the segment was not reached.
Eagerly drops n
elements from the head of this segment, returning either the result and the
number of elements remaining to drop, if the end of the segment was reached, or a new segment,
if the end of the segment was not reached.
scala> Segment(1,2,3,4,5).force.drop(3).toOption.get.force.toVector res0: Vector[Int] = Vector(4, 5) scala> Segment(1,2,3,4,5).force.drop(7) res1: Either[(Unit, Long),Segment[Int,Unit]] = Left(((),2))
Eagerly drops elements from the head of this segment until the supplied predicate returns false, returning either the result, if the end of the segment was reached without the predicate failing, or the remaining segment.
Eagerly drops elements from the head of this segment until the supplied predicate returns false, returning either the result, if the end of the segment was reached without the predicate failing, or the remaining segment.
If dropFailure
is true, the first element that failed the predicate will be dropped. If false,
the first element that failed the predicate will be the first element of the remainder.
scala> Segment(1,2,3,4,5).force.dropWhile(_ < 3).map(_.force.toVector) res0: Either[Unit,Vector[Int]] = Right(Vector(3, 4, 5)) scala> Segment(1,2,3,4,5).force.dropWhile(_ < 10) res1: Either[Unit,Segment[Int,Unit]] = Left(())
Invokes f
on each output of this segment.
Invokes f
on each output of this segment.
scala> val buf = collection.mutable.ListBuffer[Int]() scala> Segment(1,2,3).cons(0).force.foreach(i => buf += i) res0: Unit = () scala> buf.toList res1: List[Int] = List(0, 1, 2, 3)
Invokes f
on each chunk of this segment.
Invokes f
on each chunk of this segment.
scala> val buf = collection.mutable.ListBuffer[Chunk[Int]]() scala> Segment(1,2,3).cons(0).force.foreachChunk(c => buf += c) res0: Unit = () scala> buf.toList res1: List[Chunk[Int]] = List(Chunk(0), Chunk(1, 2, 3))
Computes the result of this segment.
Computes the result of this segment. May only be called when O
is Nothing
, to prevent accidentally ignoring
output values. To intentionally ignore outputs, call s.drain.force.run
.
scala> Segment(1, 2, 3).withSize.drain.force.run res0: (Unit,Long) = ((),3)
Splits this segment at the specified index by simultaneously taking and dropping.
Splits this segment at the specified index by simultaneously taking and dropping.
If the segment has less than n
elements, a left is returned, providing the result of the segment,
all sub-segments taken, and the remaining number of elements (i.e., size - n).
If the segment has more than n
elements, a right is returned, providing the sub-segments up to
the n
-th element and a remainder segment.
The prefix is computed eagerly while the suffix is computed lazily.
The maxSteps
parameter provides a notion of fairness. If specified, steps through the staged machine
are counted while executing and if the limit is reached, execution completes, returning a Right
consisting
of whatever elements were seen in the first maxSteps
steps. This provides fairness but yielding the
computation back to the caller but with less than n
accumulated values.
scala> Segment(1, 2, 3, 4, 5).force.splitAt(2) res0: Either[(Unit,Catenable[Chunk[Int]],Long),(Catenable[Chunk[Int]],Segment[Int,Unit])] = Right((Catenable(Chunk(1, 2)),Chunk(3, 4, 5))) scala> Segment(1, 2, 3, 4, 5).force.splitAt(7) res0: Either[(Unit,Catenable[Chunk[Int]],Long),(Catenable[Chunk[Int]],Segment[Int,Unit])] = Left(((),Catenable(Chunk(1, 2, 3, 4, 5)),2))
Splits this segment at the first element where the supplied predicate returns false.
Splits this segment at the first element where the supplied predicate returns false.
Analagous to siumultaneously running takeWhile
and dropWhile
.
If emitFailure
is false, the first element which fails the predicate is returned in the suffix segment. If true,
it is returned as the last element in the prefix segment.
If the end of the segment is reached and the predicate has not failed, a left is returned, providing the segment result and the catenated sub-segments. Otherwise, a right is returned, providing the prefix sub-segments and the suffix remainder.
scala> Segment(1, 2, 3, 4, 5).force.splitWhile(_ != 3) res0: Either[(Unit,Catenable[Chunk[Int]]),(Catenable[Chunk[Int]],Segment[Int,Unit])] = Right((Catenable(Chunk(1, 2)),Chunk(3, 4, 5))) scala> Segment(1, 2, 3, 4, 5).force.splitWhile(_ != 7) res0: Either[(Unit,Catenable[Chunk[Int]]),(Catenable[Chunk[Int]],Segment[Int,Unit])] = Left(((),Catenable(Chunk(1, 2, 3, 4, 5))))
Converts this segment to an array, discarding the result.
Converts this segment to an array, discarding the result.
Caution: calling toArray
on an infinite sequence will not terminate.
scala> Segment(1, 2, 3).cons(0).cons(-1).force.toArray res0: Array[Int] = Array(-1, 0, 1, 2, 3)
Converts this segment to a catenable of output values, discarding the result.
Converts this segment to a catenable of output values, discarding the result.
Caution: calling toCatenable
on an infinite sequence will not terminate.
scala> Segment(1, 2, 3).cons(0).cons(-1).force.toCatenable.toList res0: List[Int] = List(-1, 0, 1, 2, 3)
Converts this segment to a single chunk, discarding the result.
Converts this segment to a single chunk, discarding the result.
Caution: calling toChunk
on an infinite sequence will not terminate.
scala> Segment(1, 2, 3).cons(0).cons(-1).force.toChunk res0: Chunk[Int] = Chunk(-1, 0, 1, 2, 3)
Converts this segment to a sequence of chunks, discarding the result.
Converts this segment to a sequence of chunks, discarding the result.
Caution: calling toChunks
on an infinite sequence will not terminate.
scala> Segment(1, 2, 3).cons(0).cons(-1).force.toChunks.toList res0: List[Chunk[Int]] = List(Chunk(-1), Chunk(0), Chunk(1, 2, 3))
Converts this chunk to a list, discarding the result.
Converts this chunk to a list, discarding the result.
Caution: calling toList
on an infinite sequence will not terminate.
scala> Segment(1, 2, 3).cons(0).cons(-1).force.toList res0: List[Int] = List(-1, 0, 1, 2, 3)
Converts this segment to a vector, discarding the result.
Converts this segment to a vector, discarding the result.
Caution: calling toVector
on an infinite sequence will not terminate.
scala> Segment(1, 2, 3).cons(0).cons(-1).force.toVector res0: Vector[Int] = Vector(-1, 0, 1, 2, 3)
Returns the first output sub-segment of this segment along with the remainder, wrapped in Right
, or
if this segment is empty, returns the result wrapped in Left
.
Returns the first output sub-segment of this segment along with the remainder, wrapped in Right
, or
if this segment is empty, returns the result wrapped in Left
.
scala> Segment(1, 2, 3).cons(0).force.uncons res0: Either[Unit,(Segment[Int,Unit], Segment[Int,Unit])] = Right((Chunk(0),Chunk(1, 2, 3))) scala> Segment.empty[Int].force.uncons res1: Either[Unit,(Segment[Int,Unit], Segment[Int,Unit])] = Left(())
Returns the first output of this segment along with the remainder, wrapped in Right
, or
if this segment is empty, returns the result wrapped in Left
.
Returns the first output of this segment along with the remainder, wrapped in Right
, or
if this segment is empty, returns the result wrapped in Left
.
scala> Segment(1, 2, 3).cons(0).force.uncons1 res0: Either[Unit,(Int, Segment[Int,Unit])] = Right((0,Chunk(1, 2, 3))) scala> Segment.empty[Int].force.uncons1 res1: Either[Unit,(Int, Segment[Int,Unit])] = Left(())
Returns all output chunks and the result of this segment after stepping it to completion.
Returns all output chunks and the result of this segment after stepping it to completion.
Will not terminate if run on an infinite segment.
scala> Segment(1, 2, 3).prepend(Segment(-1, 0)).force.unconsAll res0: (Catenable[Chunk[Int]], Unit) = (Catenable(Chunk(-1, 0), Chunk(1, 2, 3)),())
Returns the first output chunk of this segment along with the remainder, wrapped in Right
, or
if this segment is empty, returns the result wrapped in Left
.
Returns the first output chunk of this segment along with the remainder, wrapped in Right
, or
if this segment is empty, returns the result wrapped in Left
.
scala> Segment(1, 2, 3).prepend(Segment(-1, 0)).force.unconsChunk res0: Either[Unit,(Chunk[Int], Segment[Int,Unit])] = Right((Chunk(-1, 0),Chunk(1, 2, 3))) scala> Segment.empty[Int].force.unconsChunk res1: Either[Unit,(Chunk[Int], Segment[Int,Unit])] = Left(())
Returns the first output chunks of this segment along with the remainder, wrapped in Right
, or
if this segment is empty, returns the result wrapped in Left
.
Returns the first output chunks of this segment along with the remainder, wrapped in Right
, or
if this segment is empty, returns the result wrapped in Left
.
Differs from unconsChunk
when a single step results in multiple outputs.
scala> Segment(1, 2, 3).prepend(Segment(-1, 0)).force.unconsChunks res0: Either[Unit,(Catenable[Chunk[Int]], Segment[Int,Unit])] = Right((Catenable(Chunk(-1, 0)),Chunk(1, 2, 3))) scala> Segment.empty[Int].force.unconsChunks res1: Either[Unit,(Catenable[Chunk[Int]], Segment[Int,Unit])] = Left(())
Operations on a
Segment
which force evaluation of some part or all of a segments elements.