summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2012-04-04 10:01:47 -0700
committerPaul Phillips <paulp@improving.org>2012-04-04 10:29:29 -0700
commitbb4935e92c26778a1d1096cd5cd66812a9122f66 (patch)
tree7dcfb28fa551a812e47a62775dde018a62da3857 /src
parenta7f68ce32c35c73af855eab26635251249ac171a (diff)
downloadscala-bb4935e92c26778a1d1096cd5cd66812a9122f66.tar.gz
scala-bb4935e92c26778a1d1096cd5cd66812a9122f66.tar.bz2
scala-bb4935e92c26778a1d1096cd5cd66812a9122f66.zip
Added Option#fold.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/Option.scala12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala
index 6db4904b93..2d87ccb261 100644
--- a/src/library/scala/Option.scala
+++ b/src/library/scala/Option.scala
@@ -130,6 +130,18 @@ sealed abstract class Option[+A] extends Product with Serializable {
@inline final def map[B](f: A => B): Option[B] =
if (isEmpty) None else Some(f(this.get))
+ /** Returns the result of applying $f to this $option's
+ * value if the $option is nonempty. Otherwise, evaluates
+ * expression $ifEmpty.
+ *
+ * @note This is equivalent to `$option map f getOrElse ifEmpty`.
+ *
+ * @param ifEmpty the expression to evaluate if empty.
+ * @param f the function to apply if nonempty.
+ */
+ @inline final def fold[B](ifEmpty: => B)(f: A => B): B =
+ if (isEmpty) ifEmpty else f(this.get)
+
/** Returns the result of applying $f to this $option's value if
* this $option is nonempty.
* Returns $none if this $option is empty.