Package

com

thoughtworks

Permalink

package thoughtworks

Visibility
  1. Public
  2. All

Type Members

  1. sealed trait Extractor[-A, +B] extends (A) ⇒ Option[B]

    Permalink

    A pattern that can be used in match / case expressions.

Value Members

  1. object Extractor extends LowPriorityExtractor

    Permalink

    Utilities to convert between A => Option[B], scala.PartialFunction and Extractor.

    Utilities to convert between A => Option[B], scala.PartialFunction and Extractor.

    Example:
    1. import com.thoughtworks.Extractor._
      // Define a PartialFunction
      val pf: PartialFunction[Int, String] = {
        case 1 => "matched by PartialFunction"
      }
      // Define an optional function
      val f: Int => Option[String] = { i =>
        if (i == 2) {
          Some("matched by optional function")
        } else {
          None
        }
      }
      // Convert an optional function to a PartialFunction
      val pf2: PartialFunction[Int, String] = Function.unlift(f)
      util.Random.nextInt(4) match {
        case pf.extract(m) => // Convert a PartialFunction to a pattern
          println(m)
        case f.extract(m) => // Convert an optional function to a pattern
          println(m)
        case pf2.extract(m) => // Convert a PartialFunction to a pattern
          throw new AssertionError("This case should never occur because it has the same condition as `f.extract`.")
        case _ =>
          println("Not matched")
      }

Ungrouped