Called from within the body of a generator, generate() yields a
value back from the generator.
Used to define a generator; the code (body) is the partial function
to run as the generator.
Used to define a generator; the code (body) is the partial function
to run as the generator. Within the body, you can call generate()
to yield values. The result of a generator, from the caller's
perspective, is a typed iterator.
Create a function trampoline.
Create a function trampoline. The body should return either
- Yield, with the result and the next function to call, or
- Done, to signal completion.
Functions that can be used to simulate Python-style generators. Adapted liberally from Rich Dougherty's solution, as outlined in Stack Overflow: http://stackoverflow.com/questions/2201882#2215182
Example usage:
import grizzled.generator._ import scala.util.continuations._ import java.io.File def recursivelyListFiles(dir: File): Iterator[File] = generator[File] { def handleList(list: List[File]): Unit @cps[GeneratorIteration[File]] = { list match { case Nil => () case f :: tail => { generate(f) doList(if (f.isDirectory) f.listFiles.toList else Nil) doList(tail) } } } handleList(dir.listFiles.toList) }This package uses the Scala compilers continuations plug-in. The above example must be compiled with tha plug-in enabled. Use the
-P:continuations:enableflag.