summaryrefslogtreecommitdiff
path: root/src/build/genprod.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/build/genprod.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/build/genprod.scala')
-rw-r--r--src/build/genprod.scala13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/build/genprod.scala b/src/build/genprod.scala
index 5d47b0317e..6849206f3c 100644
--- a/src/build/genprod.scala
+++ b/src/build/genprod.scala
@@ -153,6 +153,18 @@ object FunctionOne extends Function(1) {
/** (f andThen g)(x) == g(f(x))
*/
def andThen[A](g: R => A): T1 => A = { x => g(apply(x)) }
+
+ /** Turns a function A => Option[B] into a PartialFunction[A, B].
+ * @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]]
+ }
"""
}
@@ -314,6 +326,7 @@ import scala.collection.generic.CanBuildFrom
class Zipped[+Repr1, +El1, +Repr2, +El2](coll1: TraversableLike[El1, Repr1], coll2: IterableLike[El2, Repr2]) { // coll2: IterableLike for filter
def map[B, To](f: (El1, El2) => B)(implicit cbf: CanBuildFrom[Repr1, B, To]): To = {
val b = cbf(coll1.repr)
+ b.sizeHint(coll1)
val elems2 = coll2.iterator
for(el1 <- coll1)