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. sealed trait Iteration[+T] extends AnyRef

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

Value Members

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

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

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

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

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

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

  7. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  8. def clone(): AnyRef

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

    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. final def getClass(): java.lang.Class[_]

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

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

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

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

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

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

    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. final def wait(): Unit

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from AnyRef

Inherited from Any