summaryrefslogtreecommitdiff
path: root/src/library/scala/PartialFunction.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-08-20 13:39:16 +0000
committerPaul Phillips <paulp@improving.org>2009-08-20 13:39:16 +0000
commit397c2027d930d3a1ecc707622bbb9135b29e5099 (patch)
treecc7637efd2ca70380ee69404950afe7110101f0e /src/library/scala/PartialFunction.scala
parent522bf3a7d81310cf569932c1fef89c4053238916 (diff)
downloadscala-397c2027d930d3a1ecc707622bbb9135b29e5099.tar.gz
scala-397c2027d930d3a1ecc707622bbb9135b29e5099.tar.bz2
scala-397c2027d930d3a1ecc707622bbb9135b29e5099.zip
A couple super useful partial function methods ...
A couple super useful partial function methods I am frequently reimplementing.
Diffstat (limited to 'src/library/scala/PartialFunction.scala')
-rw-r--r--src/library/scala/PartialFunction.scala47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/library/scala/PartialFunction.scala b/src/library/scala/PartialFunction.scala
index 331af8faa7..189d76df53 100644
--- a/src/library/scala/PartialFunction.scala
+++ b/src/library/scala/PartialFunction.scala
@@ -11,6 +11,7 @@
package scala
+import annotation.experimental
/** A partial function of type <code>PartialFunction[A, B]</code> is a
* unary function where the domain does not include all values of type
@@ -44,3 +45,49 @@ trait PartialFunction[-A, +B] extends AnyRef with (A => B) {
}
}
+/** A few handy operations which leverage the extra bit of information
+ * available in partial functions. Examples:
+ *
+ * <pre>
+ * import PartialFunction._
+ *
+ * def strangeConditional(other: Any): Boolean = ?(other) {
+ * case x: String if x == "abc" || x == "def" => true
+ * case x: Int => true
+ * }
+ * def onlyInt(v: Any): Option[Int] = opt(v) { case x: Int => x }
+ * </pre>
+ *
+ * @author Paul Phillips
+ * @since 2.8
+ */
+@experimental
+object PartialFunction
+{
+ /** Creates a Boolean test based on a value and a partial function.
+ * It behaves like a 'match' statement with an implied 'case _ => false'
+ * following the supplied cases.
+ *
+ * @param x the value to test
+ * @param pf the partial function
+ * @return true, iff <code>x</code> is in the domain of pf && pf(x) == true
+ */
+ def ?[T](x: T)(pf: PartialFunction[T, Boolean]): Boolean =
+ (pf isDefinedAt x) && pf(x)
+
+ /** Transforms a PartialFunction[T,U] `pf' into Function1[T,Option[U]] `f'
+ * whose result is Some(x) if the argument is in pf's domain and None otherwise,
+ * and applies it to the value `x'. In effect, it is a 'match' statement
+ * which wraps all case results in Some(_) and adds 'case _ => None' to the end.
+ *
+ * @param x the value to test
+ * @param pf the PartialFunction[T,U]
+ * @return Some(pf(x)) iff (pf isDefinedAt x) and None otherwise
+ */
+ def opt[T,U](x: T)(pf: PartialFunction[T, U]): Option[U] =
+ if (pf isDefinedAt x) Some(pf(x)) else None
+
+ // If only getOrElse were a bit less unwieldy...
+ // def opt[T,U](x: T, default: U)(pf: PartialFunction[T, U]): U =
+ // opt(x)(pf) getOrElse default
+} \ No newline at end of file