Last active
August 29, 2015 13:57
-
-
Save pchlupacek/9414581 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Feed a sequence of inputs to a `Process1`. */ | |
def feed[I, O](i: Seq[I])(p: Process1[I, O]): Process1[I, O] = { | |
@tailrec | |
def go( | |
in: Seq[I], out: Vector[O] | |
, cur: Process1[I, O] | |
, stack: Vector[Throwable => Trampoline[Process1[I, O]]] | |
): Process1[I, O] = { | |
if (in.nonEmpty) { | |
cur match { | |
case Halt(_) | Emit(_) if stack.isEmpty => emitAll(out) ++ cur | |
case Halt(rsn) => go(in, out, Try(stack.head(rsn).run), stack.tail) | |
case Emit(os) => go(in, out fast_++ os, Try(stack.head(End).run), stack.tail) | |
case Append(p, n) => go(in, out, p, n fast_++ stack) | |
case AwaitP1(rcv, fb) => go(in.tail, out, Try(rcv(in.head)) onHalt fb, stack) | |
} | |
} else emitAll(out) ++ cur | |
} | |
go(i, Vector(), p, Vector()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment