GithubHelp home page GithubHelp logo

thoughtworksinc / extractor.scala Goto Github PK

View Code? Open in Web Editor NEW
22.0 24.0 4.0 232 KB

Make PartialFunction and extractors composable

License: MIT License

Scala 100.00%
scala extractor-objects partial-functions extractor

extractor.scala's Introduction

Extractor.scala

Build Status Latest version Scaladoc

Extractor.scala is a library to convert between A => Option[B], PartialFunction[A, B] and extractor objects for pattern matching.

Usage

// Add this line in your build.sbt
libraryDependencies += "com.thoughtworks.extractor" %% "extractor" % "latest.release"
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")
}

You can also use .extract.seq to extract elements of a sequence data.

import com.thoughtworks.Extractor._

val csvRow: String => Option[Seq[String]] = { line =>
  Some(line.split(','))
}

"foo,bar,baz" match {
  case csvRow.extract.seq(cell0, cell1, cell2) =>
    println(s"cell1=$cell1") // Output: cell1=bar
}

extractor.scala's People

Contributors

atry avatar maowug avatar scala-steward avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

extractor.scala's Issues

intellij reports "cannot resolve symbol extract"

Hi Yang Bo,
I found intellij reported "cannot resolve symbol extract" on line of: case underlying.extract(b) => b in Extractor.OptionFunctionToPartialFunction.unlift.
But the test can be run correctly, so it seems intellij is not smart enought to find out its an implicit conversion, I think.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.