summaryrefslogtreecommitdiff
path: root/src/library/scala/Function1.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-09-09 17:05:40 +0000
committerPaul Phillips <paulp@improving.org>2010-09-09 17:05:40 +0000
commitaab959bbe2263962add5da425a312b1ea209692f (patch)
tree94f2b8d4779344645e924f015c3ebc450097d744 /src/library/scala/Function1.scala
parent5a150395e7a3ff7e2795a044ae302702a2e0c904 (diff)
downloadscala-aab959bbe2263962add5da425a312b1ea209692f.tar.gz
scala-aab959bbe2263962add5da425a312b1ea209692f.tar.bz2
scala-aab959bbe2263962add5da425a312b1ea209692f.zip
Proposed implementation of 'unlift' on Function...
Proposed implementation of 'unlift' on Function1, the inverse function of PartialFunction#lift. Review by rytz and other interested parties. References #3825, but not closing until this is further considered.
Diffstat (limited to 'src/library/scala/Function1.scala')
-rw-r--r--src/library/scala/Function1.scala17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/library/scala/Function1.scala b/src/library/scala/Function1.scala
index a6a6fa2828..beac376f4e 100644
--- a/src/library/scala/Function1.scala
+++ b/src/library/scala/Function1.scala
@@ -7,7 +7,7 @@
\* */
-// generated by genprod on Thu Apr 29 17:52:16 CEST 2010 (with fancy comment) (with extra methods)
+// generated by genprod on Thu Sep 09 09:06:40 PDT 2010 (with fancy comment) (with extra methods)
package scala
@@ -47,4 +47,19 @@ trait Function1[@specialized(scala.Int, scala.Long, scala.Float, scala.Double) -
*/
def andThen[A](g: R => A): T1 => A = { x => g(apply(x)) }
+ /** Turns a function A => Option[B] into a PartialFunction[A, B]. Important note:
+ * this transformation implies the original function will be called 2 or more
+ * times on each logical invocation, because the only way to supply an implementation
+ * of isDefinedAt is to call the function and examine the return value.
+ *
+ * @see PartialFunction#lift
+ * @return a partial function which is defined for those inputs
+ * where this function returns Some(_) and undefined where
+ * this function returns None.
+ */
+ def unlift[R1](implicit ev: R <:< Option[R1]): PartialFunction[T1, R1] = new PartialFunction[T1, R1] {
+ def apply(x: T1): R1 = ev(Function1.this.apply(x)).get
+ def isDefinedAt(x: T1): Boolean = Function1.this.apply(x).isDefined
+ override def lift = Function1.this.asInstanceOf[T1 => Option[R1]]
+ }
}