grizzled

generator

object generator extends AnyRef

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:enable flag.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. generator
  2. AnyRef
  3. Any
Visibility
  1. Public
  2. All

Type Members

  1. trait Iteration [+T] extends AnyRef

    Attributes
    sealed
  2. case class Yield [+T] (result: T, next: () ⇒ Iteration[T]) extends Iteration[T] with Product with Serializable

Value Members

  1. def != (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  2. def != (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  3. def ## (): Int

    Attributes
    final
    Definition Classes
    AnyRef → Any
  4. def == (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  5. def == (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  6. object Done extends Iteration[Nothing] with Product with Serializable

  7. def asInstanceOf [T0] : T0

    Attributes
    final
    Definition Classes
    Any
  8. def clone (): AnyRef

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  9. def eq (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  10. def equals (arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  11. def finalize (): Unit

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  12. def generate [T] (result: T): Unit @util.continuations.package.cps[grizzled.generator.Iteration[T]]

    Called from within the body of a generator, generate() yields a value back from the generator.

  13. def generator [T] (body: ⇒ Unit @util.continuations.package.cps[grizzled.generator.Iteration[T]]): Iterator[T]

    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.

  14. def getClass (): java.lang.Class[_]

    Attributes
    final
    Definition Classes
    AnyRef → Any
  15. def hashCode (): Int

    Definition Classes
    AnyRef → Any
  16. def isInstanceOf [T0] : Boolean

    Attributes
    final
    Definition Classes
    Any
  17. def ne (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  18. def notify (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  19. def notifyAll (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  20. def synchronized [T0] (arg0: ⇒ T0): T0

    Attributes
    final
    Definition Classes
    AnyRef
  21. def toString (): String

    Definition Classes
    AnyRef → Any
  22. def trampoline [T] (body: ⇒ Iteration[T]): Iterator[T]

    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.

  23. def wait (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  24. def wait (arg0: Long, arg1: Int): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  25. def wait (arg0: Long): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from AnyRef

Inherited from Any