sealed abstract class Scan[-I, +O] extends Serializable
The Scan trait is an alternative to the scanLeft
method on iterators/other collections for a range of
of use-cases where scanLeft
is awkward to use. At a high level it provides some of the same functionality as
scanLeft
, but with a separation of "what is the state of the scan" from
"what are the elements that I'm scanning over?". In particular, when scanning over an iterator with N
elements,
the output is an iterator with N
elements (in contrast to scanLeft's N+1
).
If you find yourself writing a scanLeft
over pairs of elements, where you only use one element of the pair within
the scanLeft
, then throw that element away in a map
immediately after the scanLeft is done, then this
abstraction is for you.
The canonical method to use a scan is apply
.
- I
The type of elements that the computation is scanning over.
- O
The output type of the scan (typically distinct from the hidden
State
of the scan).
- Alphabetic
- By Inheritance
- Scan
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- abstract type State
The computation of any given scan involves keeping track of a hidden state.
Abstract Value Members
- abstract def initialState: State
The state of the scan before any elements have been processed
- abstract def presentAndNextState(i: I, stateBeforeProcessingI: State): (O, State)
- i
An element in the stream to process
- stateBeforeProcessingI
The state of the scan before processing i
- returns
The output of the scan corresponding to processing i with state stateBeforeProcessing, along with the result of updating stateBeforeProcessing with the information from i.
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def andThenPresent[O1](g: (O) => O1): Aux[I, State, O1]
- def apply[In <: TraversableOnce[I], Out](inputs: In)(implicit bf: BuildFrom[In, O, Out]): Out
- In
The type of the input collection
- Out
The type of the output collection
- returns
Given inputs as a collection of the form
[a_1, ..., a_n]
the output will be a collection of the form:[o_1, ..., o_n]
where(o_(i+1), state_(i+1)) = presentAndNextState(a_i, state_i)
andstate_0 = initialState
.
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def compose[P](scan2: Scan[O, P]): Aux[I, (State, State), P]
Takes the output of this scan and feeds as input into scan2.
Takes the output of this scan and feeds as input into scan2.
- returns
If this Scan's apply method is given inputs
[a_1, ..., a_n]
resulting in outputs of the form[o_1, ..., o_n]
, andscan2.apply([o_1, ..., o_n] = [p_1, ..., p_n]
thencompose
will return a scan which returns[p_1, ..., p_n]
.
- def composePrepare[I1](f: (I1) => I): Aux[I1, State, O]
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def join[I2 <: I, O2](scan2: Scan[I2, O2]): Aux[I2, (State, State), (O, O2)]
Given a scan that takes compatible input to this one, pairwise compose the state and outputs of each scan on a common input stream.
Given a scan that takes compatible input to this one, pairwise compose the state and outputs of each scan on a common input stream.
- returns
If this Scan's apply method is given inputs [a_1, ..., a_n] resulting in outputs of the form
[o_1, ..., o_n]
, andscan2.apply([a_1, ..., a_n] = [p_1, ..., p_n]
thenjoin
will return a scan whose apply method returns[(o_1, p_1), ..., (o_2, p_2)]
. In other words:scan.join(scan2)(foo) == scan(foo).zip(scan2(foo))
- def joinWithIndex: Aux[I, (State, Long), (O, Long)]
For every
foo
,scan.joinWithIndex(foo) == scan(foo).zipWithIndex
.For every
foo
,scan.joinWithIndex(foo) == scan(foo).zipWithIndex
.- returns
If this Scan's
apply
method is given inputs[a_1, ..., a_n]
resulting in outputs of the form[o_1, ..., o_n]
, return a scan that whose apply method, when given the same input, will return[(o_1, 1), ..., (o_n, n)]
.
- def joinWithInput[I1 <: I]: Aux[I1, State, (O, I1)]
Return a scan that is semantically identical to
this.join(Scan.identity[I1])
, but where we don't pollute theState
by pairing it redundantly withUnit
.Return a scan that is semantically identical to
this.join(Scan.identity[I1])
, but where we don't pollute theState
by pairing it redundantly withUnit
.- returns
If this Scan's
apply
method is given inputs[a_1, ..., a_n]
resulting in outputs of the form[o_1, ..., o_n
, then this results in a Scan whoseapply
method returns[(o_1, a_1), ..., (o_n, a_n)]
when given the same input.
- def joinWithPosteriorState: Aux[I, State, (O, State)]
Return a scan whose output is paired with the state of the scan after each input updates the state.
Return a scan whose output is paired with the state of the scan after each input updates the state.
- returns
If this Scan's
apply
method is given inputs[a_1, ..., a_n]
resulting in outputs of the form[o_1, ..., o_n]
, where(o_(i+1), state_(i+1)) = presentAndNextState(a_i, state_i)
and state_0 = initialState, return a scan that whose apply method, when given inputs
[a_1, ..., a_n]will return
[(o_1, state_1), ..., (o_n, state_n]
.
- def joinWithPriorState: Aux[I, State, (State, O)]
Return a scan whose output is paired with the state of the scan before each input updates the state.
Return a scan whose output is paired with the state of the scan before each input updates the state.
- returns
If this Scan's
apply
method is given inputs [a_1, ..., a_n] resulting in outputs of the form[o_1, ..., o_n]
, where(o_(i+1), state_(i+1)) = presentAndNextState(a_i, state_i)
andstate_0 = initialState
, return a scan that whose apply method, when given inputs[a_1, ..., a_n]
will return[(o_1, state_0), ..., (o_n, state_(n-1))]
.
- 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()
- def replaceState(newInitialState: => State): Aux[I, State, O]
Return a new scan that is the same as this scan, but with a different
initialState
. - def scanIterator(iter: Iterator[I]): Iterator[O]
- returns
If
iter = Iterator(a_1, ..., a_n)
, return:Iterator(o_1, ..., o_n)
where(o_(i+1), state_(i+1)) = presentAndNextState(a_i, state_i)
and
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- def zip[I2, O2](scan2: Scan[I2, O2]): Aux[(I, I2), (State, State), (O, O2)]
Compose two scans pairwise such that, when given pairwise zipped inputs, the resulting scan will output pairwise zipped outputs.
Compose two scans pairwise such that, when given pairwise zipped inputs, the resulting scan will output pairwise zipped outputs.
- returns
If this Scan's apply method is given inputs
[a_1, ..., a_n]
resulting in outputs of the form[o_1, ..., o_n]
, andscan2.apply([b_1, ..., b_n] = [p_1, ..., p_n]
thenzip
will return a scan whose apply method, when given input[(a_1, b_1), ..., (a_n, b_n)]
results in the output[(o_1, p_1), ..., (o_2, p_2)]
. In other words:scan.zip(scan2)(foo.zip(bar)) == scan(foo).zip(scan2(bar))