Skip to content

Instantly share code, notes, and snippets.

@arkadius
Last active August 29, 2015 14:04
Show Gist options
  • Save arkadius/36d913f8657649dbfed0 to your computer and use it in GitHub Desktop.
Save arkadius/36d913f8657649dbfed0 to your computer and use it in GitHub Desktop.
class Phase[-In, +Out](private[phase] val name: String,
private[phase] val process: In => Out)
object Phase {
def apply[In, Out](name: String)
(process: In => Out) = new Phase(name, process)
}
trait PhasesChain[-In, +Out] extends ChainTransformation[In, Out] {
private[phase] def run(progress: MultiPhasedProgress)(in: In): Out = {
processWithProgress(progress)(in)
}
private[phase] def processWithProgress(progress: MultiPhasedProgress): In => Out
def ::[NIn](prev: Phase[NIn, In]): PhasesChain[NIn, Out] = new ChainedPhase(prev, this)
protected def processWrapped[I, O](name: String, process: I => O)
(progress: MultiPhasedProgress) = (in: I) =>
progress.inPhase(name) {
process(in)
}
val phasesCount: Int
}
object PhasesChain extends SequencedChainCreation
private[phase] class ChainedPhase[-In, Mid, +Out](prev: Phase[In, Mid], next: PhasesChain[Mid, Out])
extends PhasesChain[In, Out] {
def processWithProgress(progress: MultiPhasedProgress) = {
val processPrevWrapped = processWrapped(prev.name, prev.process)(progress)
val processNext = next.processWithProgress(progress)
processPrevWrapped andThen processNext
}
val phasesCount = 1 + next.phasesCount
}
case class NilChain[T]() extends PhasesChain[T, T] {
def processWithProgress(progress: MultiPhasedProgress): T => T = in => in
val phasesCount = 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment