Last active
December 18, 2015 01:48
-
-
Save YoEight/5706306 to your computer and use it in GitHub Desktop.
Scala-machines Machine to Play Enumerator
This file contains 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
import com.clarifi.machines._ | |
import play.api.libs.iteratee.{ Enumerator, Input, Iteratee } | |
import play.api.libs.concurrent.Execution.Implicits._ | |
import scala.concurrent.Future | |
def simple[K, E](machine: Machine[K, E]): Enumerator[E] = new Enumerator[E] { | |
def apply[A](start: Iteratee[E, A]): Future[Iteratee[E, A]] = | |
val result = machine.foldLeft(Future.successful(start)) { (future, value) ⇒ | |
future.flatMap(_.feed(Input.El(value))) | |
} | |
result.flatMap(_.feed(Input.EOF)) | |
} | |
} | |
def precise[K, E](machine: Machine[K, E]): Enumerator[E] = new Enumerator[E] { | |
def apply[A](start: Iteratee[E, A]): Future[Iteratee[E, A]] = { | |
def go(m: Machine[K, E], i: Iteratee[E, A]): Future[Iteratee[E, A]] = | |
i.fold { | |
case Cont(k) ⇒ m match { | |
case Return(_) ⇒ sys.error("impossible situation") | |
case Stop ⇒ go(m, k(Input.EOF)) | |
case Await(_, _, next) ⇒ go(next(), i) | |
case Emit(value, next) ⇒ go(next(), k(Input.El(value))) | |
} | |
case _ ⇒ | |
Future.successful(i) | |
} | |
go(machine, start) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment