abstract class Segment[+O, +R] extends AnyRef
Potentially infinite, pure sequence of values of type O
and a result of type R
.
All methods on Segment
support fusion with other arbitrary methods that
return Segment
s. This is similar to the staging approach described in
Stream Fusion, to Completeness, but without
code generation in staging.
To force evaluation of one or more values of a segment, call .force
followed by one
of the operations on the returned Segment.Force
type. For example, to convert a
segment to a vector, call s.force.toVector
.
Stack safety of fused operations is ensured by tracking a fusion depth. If the depth
reaches the limit, the computation is trampolined using cats.Eval
.
The equals
and hashCode
methods are not defined for Segment
.
Implementation notes:
- Some operators ask for a segment remainder from within a callback (e.g.,
emits
). As such, segments should update state before invoking callbacks so that remainders can be computed accurately.
- Self Type
- Segment[O, R]
- Source
- Segment.scala
- Alphabetic
- By Inheritance
- Segment
- AnyRef
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
- new Segment()
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
- def +(other: String): String
-
final
def
++[O2 >: O, R2 >: R](s2: Segment[O2, R2]): Segment[O2, R2]
Concatenates this segment with
s2
.Concatenates this segment with
s2
.scala> (Segment(1,2,3) ++ Segment(4,5,6)).force.toVector res0: Vector[Int] = Vector(1, 2, 3, 4, 5, 6)
Example: - def ->[B](y: B): (Segment[O, R], B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
append[O2 >: O, R2](s2: Segment[O2, R2]): Segment[O2, (R, R2)]
Like
++
but allows the result type ofs2
to differ fromR
. -
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
final
def
asResult[R2](r2: R2): Segment[O, R2]
Alias for
mapResult( => r2)
. -
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
collect[O2](pf: PartialFunction[O, O2]): Segment[O2, R]
Filters and maps simultaneously.
Filters and maps simultaneously.
scala> Segment(Some(1), None, Some(2)).collect { case Some(i) => i }.force.toVector res0: Vector[Int] = Vector(1, 2)
Example: -
final
def
cons[O2 >: O](o2: O2): Segment[O2, R]
Equivalent to
Segment.singleton(o2) ++ this
.Equivalent to
Segment.singleton(o2) ++ this
.scala> Segment(1, 2, 3).cons(0).force.toVector res0: Vector[Int] = Vector(0, 1, 2, 3)
Example: -
final
def
drain: Segment[Nothing, R]
Returns a segment that suppresses all output and returns the result of this segment when run.
Returns a segment that suppresses all output and returns the result of this segment when run.
scala> Segment.from(0).take(3).drain.force.run.toOption.get.take(5).force.toVector res0: Vector[Long] = Vector(3, 4, 5, 6, 7)
Example: - def ensuring(cond: (Segment[O, R]) ⇒ Boolean, msg: ⇒ Any): Segment[O, R]
- def ensuring(cond: (Segment[O, R]) ⇒ Boolean): Segment[O, R]
- def ensuring(cond: Boolean, msg: ⇒ Any): Segment[O, R]
- def ensuring(cond: Boolean): Segment[O, R]
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
filter[O2](p: (O) ⇒ Boolean): Segment[O, R]
Filters output elements of this segment with the supplied predicate.
Filters output elements of this segment with the supplied predicate.
scala> Segment(1,2,3,4,5).filter(_ % 2 == 0).force.toVector res0: Vector[Int] = Vector(2, 4)
Example: -
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
flatMap[O2, R2](f: (O) ⇒ Segment[O2, R2]): Segment[O2, (R, Option[R2])]
List-like
flatMap
, which appliesf
to each element of the segment and concatenates the results.List-like
flatMap
, which appliesf
to each element of the segment and concatenates the results.scala> Segment(1, 2, 3).flatMap(i => Segment.seq(List.fill(i)(i))).force.toVector res0: Vector[Int] = Vector(1, 2, 2, 3, 3, 3)
Example: -
final
def
flatMapAccumulate[S, O2](init: S)(f: (S, O) ⇒ Segment[O2, S]): Segment[O2, (R, S)]
Stateful version of
flatMap
, where the function depends on a state value initialized toinit
and updated upon each output.Stateful version of
flatMap
, where the function depends on a state value initialized toinit
and updated upon each output.The final state is returned in the result, paired with the result of the source stream.
scala> val src = Segment("Hello", "World", "\n", "From", "Mars").flatMapAccumulate(0)((l,s) => | if (s == "\n") Segment.empty.asResult(0) else Segment((l,s)).asResult(l + s.length)) scala> src.force.toVector res0: Vector[(Int,String)] = Vector((0,Hello), (5,World), (0,From), (4,Mars)) scala> src.drain.force.run res1: (Unit,Int) = ((),8)
Example: -
final
def
flatMapResult[O2 >: O, R2](f: (R) ⇒ Segment[O2, R2]): Segment[O2, R2]
Like
append
but allows to use result to continue the segment. -
final
def
flatten[O2, R2 >: R](implicit ev: <:<[O, Segment[O2, R2]]): Segment[O2, R2]
Flattens a
Segment[Segment[O2,R],R]
in to aSegment[O2,R]
.Flattens a
Segment[Segment[O2,R],R]
in to aSegment[O2,R]
.scala> Segment(Segment(1, 2), Segment(3, 4, 5)).flatten.force.toVector res0: Vector[Int] = Vector(1, 2, 3, 4, 5)
Example: -
final
def
flattenChunks[O2](implicit ev: <:<[O, Chunk[O2]]): Segment[O2, R]
Flattens a
Segment[Chunk[O2],R]
in to aSegment[O2,R]
.Flattens a
Segment[Chunk[O2],R]
in to aSegment[O2,R]
.scala> Segment(Chunk(1, 2), Chunk(3, 4, 5)).flattenChunks.force.toVector res0: Vector[Int] = Vector(1, 2, 3, 4, 5)
Example: -
final
def
fold[B](z: B)(f: (B, O) ⇒ B): Segment[Nothing, (R, B)]
Folds the output elements of this segment and returns the result as the result of the returned segment.
Folds the output elements of this segment and returns the result as the result of the returned segment.
scala> Segment(1,2,3,4,5).fold(0)(_ + _).force.run res0: (Unit, Int) = ((),15)
Example: -
def
force: Force[O, R]
Provides access to operations which force evaluation of some or all elements of this segment.
- def formatted(fmtstr: String): String
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
last: Segment[O, (R, Option[O])]
Returns a segment that outputs all but the last element from the original segment, returning the last element as part of the result.
Returns a segment that outputs all but the last element from the original segment, returning the last element as part of the result.
scala> Segment(1,2,3).last.drain.force.run res0: (Unit, Option[Int]) = ((),Some(3)) scala> Segment(1,2,3).last.force.toList res1: List[Int] = List(1, 2)
Example: -
def
map[O2](f: (O) ⇒ O2): Segment[O2, R]
Returns a segment that maps each output using the supplied function.
Returns a segment that maps each output using the supplied function.
scala> Segment(1,2,3).map(_ + 1).force.toVector res0: Vector[Int] = Vector(2, 3, 4)
Example: -
def
mapAccumulate[S, O2](init: S)(f: (S, O) ⇒ (S, O2)): Segment[O2, (R, S)]
Stateful version of map, where the map function depends on a state value initialized to
init
and updated upon each output value.Stateful version of map, where the map function depends on a state value initialized to
init
and updated upon each output value.The final state is returned in the result, paired with the result of the source stream.
scala> val src = Segment("Hello", "World").mapAccumulate(0)((l,s) => (l + s.length, (l, s))) scala> src.force.toVector res0: Vector[(Int,String)] = Vector((0,Hello), (5,World)) scala> src.drain.force.run res1: (Unit,Int) = ((),10)
Example: -
final
def
mapConcat[O2](f: (O) ⇒ Chunk[O2]): Segment[O2, R]
Returns a segment that maps each output using the supplied function and concatenates all the results.
Returns a segment that maps each output using the supplied function and concatenates all the results.
scala> Segment(1,2,3).mapConcat(o => Chunk.seq(List.range(0, o))).force.toVector res0: Vector[Int] = Vector(0, 0, 1, 0, 1, 2)
Example: -
final
def
mapResult[R2](f: (R) ⇒ R2): Segment[O, R2]
Maps the supplied function over the result of this segment.
Maps the supplied function over the result of this segment.
scala> Segment('a', 'b', 'c').withSize.mapResult { case (_, size) => size }.drain.force.run res0: Long = 3
Example: -
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
prepend[O2 >: O](c: Segment[O2, Any]): Segment[O2, R]
Equivalent to
s2 ++ this
.Equivalent to
s2 ++ this
.scala> Segment(1, 2, 3).prepend(Segment(-1, 0)).force.toVector res0: Vector[Int] = Vector(-1, 0, 1, 2, 3)
Example: -
final
def
scan[B](z: B, emitFinal: Boolean = true)(f: (B, O) ⇒ B): Segment[B, (R, B)]
Like fold but outputs intermediate results.
Like fold but outputs intermediate results. If
emitFinal
is true, upon reaching the end of the stream, the accumulated value is output. IfemitFinal
is false, the accumulated output is not output. Regardless, the accumulated value is returned as the result of the segment.scala> Segment(1, 2, 3, 4, 5).scan(0)(_+_).force.toVector res0: Vector[Int] = Vector(0, 1, 3, 6, 10, 15)
Example: -
final
def
sum[N >: O](implicit N: Numeric[N]): Segment[Nothing, N]
Sums the elements of this segment and returns the sum as the segment result.
Sums the elements of this segment and returns the sum as the segment result.
scala> Segment(1, 2, 3, 4, 5).sum.force.run res0: Int = 15
Example: -
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
final
def
take(n: Long): Segment[O, Either[(R, Long), Segment[O, R]]]
Lazily takes
n
elements from this segment.Lazily takes
n
elements from this segment. The result of the returned segment is either a left containing the result of the original segment and the number of elements remaining to take when the end of the source segment was reached, or a right containing the remainder of the source segment aftern
elements are taken.scala> Segment.from(0).take(3).force.toVector res0: Vector[Long] = Vector(0, 1, 2) scala> Segment.from(0).take(3).drain.force.run.toOption.get.take(5).force.toVector res1: Vector[Long] = Vector(3, 4, 5, 6, 7) scala> Segment(1, 2, 3).take(5).drain.force.run res2: Either[(Unit, Long),Segment[Int,Unit]] = Left(((),2))
Example: -
final
def
takeWhile(p: (O) ⇒ Boolean, takeFailure: Boolean = false): Segment[O, Either[R, Segment[O, R]]]
Returns a segment that outputs elements while
p
is true.Returns a segment that outputs elements while
p
is true.The result of the returned segment is either the result of the original stream, if the end was reached and the predicate was still passing, or the remaining stream, if the predicate failed. If
takeFailure
is true, the last element output is the first element which failed the predicate. IftakeFailure
is false, the first element of the remainder is the first element which failed the predicate.scala> Segment.from(0).takeWhile(_ < 3).force.toVector res0: Vector[Long] = Vector(0, 1, 2) scala> Segment.from(0).takeWhile(_ < 3, takeFailure = true).force.toVector res1: Vector[Long] = Vector(0, 1, 2, 3) scala> Segment.from(0).takeWhile(_ < 3).drain.force.run.toOption.get.take(5).force.toVector res2: Vector[Long] = Vector(3, 4, 5, 6, 7)
Example: -
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
void: Segment[Unit, R]
Alias for
map(_ => ())
.Alias for
map(_ => ())
.scala> Segment(1, 2, 3).void.force.toList res0: List[Unit] = List((), (), ())
Example: -
final
def
voidResult: Segment[O, Unit]
Returns a new segment which discards the result and replaces it with unit.
Returns a new segment which discards the result and replaces it with unit.
scala> Segment(1, 2, 3).take(2).voidResult res0: Segment[Int,Unit] = ((Chunk(1, 2, 3)).take(2)).mapResult(<f1>)
Example: -
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
def
withSize: Segment[O, (R, Long)]
Returns a new segment which includes the number of elements output in the result.
Returns a new segment which includes the number of elements output in the result.
scala> Segment(1, 2, 3).withSize.drain.force.run res0: (Unit,Long) = ((),3)
Example: -
def
zipWith[O2, R2, O3](that: Segment[O2, R2])(f: (O, O2) ⇒ O3): Segment[O3, Either[(R, Segment[O2, R2]), (R2, Segment[O, R])]]
Zips this segment with another segment using the supplied function to combine elements from this and that.
Zips this segment with another segment using the supplied function to combine elements from this and that. Terminates when either segment terminates.
scala> Segment(1,2,3).zipWith(Segment(4,5,6,7))(_+_).force.toList res0: List[Int] = List(5, 7, 9)
Example: - def →[B](y: B): (Segment[O, R], B)