You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 23, 2020. It is now read-only.
"playground" should "eval amb via delimited continuations" in {
import scala.util.continuations._
case class Cont[T, U](fn: T => U, arg: T)
class Evaluator[T, U](init: Cont[T, U]) {
var queue = Queue.empty[Cont[Any, U]]
var results = Vector.empty[U]
lazy val eval: Vector[U] = {
queue.enqueue(init)
loop()
results
}
def amb[V](vs: Vector[V]): V @cpsParam[U, Unit] = {
shift {
k: (V => U) =>
vs.foreach {
v =>
queue.enqueue(Cont[V, U](k, v))
}
}
}
@tailrec
private def loop(): Unit = {
if (queue.nonEmpty) {
val (cont, newQueue) = queue.dequeue
queue = newQueue
reset {
val result = cont.fn(cont.arg)
results = results :+ result
}
loop()
}
}
}
}