summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2007-01-18 16:27:05 +0000
committerMartin Odersky <odersky@gmail.com>2007-01-18 16:27:05 +0000
commitcc4427befb91bcdc183008d8a0cc727f0549f55e (patch)
tree283db5fea8be263b70c4e09a011f61ca04f81a89
parent990a28f37c35950f31f84db9ee0ba8ef2673a1eb (diff)
downloadscala-cc4427befb91bcdc183008d8a0cc727f0549f55e.tar.gz
scala-cc4427befb91bcdc183008d8a0cc727f0549f55e.tar.bz2
scala-cc4427befb91bcdc183008d8a0cc727f0549f55e.zip
updated Optiona and Array
-rw-r--r--src/library/scala/Array.scala4
-rw-r--r--src/library/scala/Option.scala113
-rw-r--r--src/library/scala/runtime/BoxedArray.scala4
3 files changed, 76 insertions, 45 deletions
diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala
index 041213beb8..82745709ba 100644
--- a/src/library/scala/Array.scala
+++ b/src/library/scala/Array.scala
@@ -202,11 +202,11 @@ final class Array[A](_length: Int) extends Seq[A] {
/** A sub-array of <code>len</code> elements
* starting at index <code>from</code>
* @param from The index of the first element of the slice
- * @param len The number of elements in the slice
+ * @param end The index of the element following the slice
* @throws IndexOutOfBoundsException if <code>from < 0</code>
* or <code>length < from + len<code>
*/
- override def slice(from: Int, len: Int): Array[A] = throw new Error()
+ override def slice(from: Int, end: Int): Array[A] = throw new Error()
/** Returns an array consisting of all elements of this array that satisfy the
* predicate <code>p</code>. The order of the elements is preserved.
diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala
index 96d9fb2457..8167b5cb6a 100644
--- a/src/library/scala/Option.scala
+++ b/src/library/scala/Option.scala
@@ -15,10 +15,9 @@ package scala;
import Predef._
object Option {
- implicit def option2Iterable[a](xo: Option[a]): Iterable[a] = xo match {
- case Some(x) => List(x)
- case None => Nil
- }
+ /** An implicit conversion that converts an option to an iterable value
+ */
+ implicit def option2Iterable[a](xo: Option[a]): Iterable[a] = xo.toList
}
@@ -28,58 +27,82 @@ object Option {
*
* @author Martin Odersky
* @author Matthias Zenger
- * @version 1.0, 16/07/2003
+ * @version 1.1, 16/01/2007
*/
sealed abstract class Option[+A] extends Product {
- def isEmpty: Boolean = this match {
- case None => true
- case _ => false
- }
+ /** True if the option is the <code>None</value>, false otherwise.
+ */
+ def isEmpty: Boolean
- /**
- * @throws Predef.NoSuchElementException
+ /** get the value of this option.
+ * @requires that the option is nonEmpty.
+ * @throws Predef.NoSuchElementException if the option is empty.
*/
- def get: A = this match {
- case None => throw new NoSuchElementException("None.get")
- case Some(x) => x
- }
+ def get: A
- def get[B >: A](default: B): B = this match {
+ /** @deprecated; use getOrElse instead */
+ [deprecated] def get[B >: A](default: B): B = this match {
case None => default
case Some(x) => x
}
- def map[B](f: A => B): Option[B] = this match {
- case None => None
- case Some(x) => Some(f(x))
- }
+ /** If the option is nonempty return its value,
+ * otherwise return the result of evaluating a default expression.
+ * @param default the default expression.
+ */
+ def getOrElse[B >: A](default: => B): B =
+ if (isEmpty) default else this.get
- def flatMap[B](f: A => Option[B]): Option[B] = this match {
- case None => None
- case Some(x) => f(x)
- }
+ /** If the option is nonempty, return a function applied to its value,
+ * wrapped in a Some i.e. <code>Some(f(this.get))</code>.
+ * Otherwise return None.
+ * @param f the function to apply
+ */
+ def map[B](f: A => B): Option[B] =
+ if (isEmpty) None else Some(f(this.get))
- def filter(p: A => Boolean): Option[A] = this match {
- case None => None
- case Some(x) => if (p(x)) Some(x) else None
- }
+ /** If the option is nonempty, return a function applied to its value.
+ * Otherwise return None.
+ * @param f the function to apply
+ */
+ def flatMap[B](f: A => Option[B]): Option[B] =
+ if (isEmpty) None else f(this.get)
- def foreach(f: A => Unit): Unit = this match {
- case None => ()
- case Some(x) => f(x)
- }
+ /** If the option is nonempty and the given predicate <code>p</code>
+ * yields <code>false</code> on its value, return <code>None</code>.
+ * Otherwise return the option value itself.
+ * @param p the predicate used for testing.
+ */
+ def filter(p: A => Boolean): Option[A] =
+ if (isEmpty || p(this.get)) this else None
- def elements: Iterator[A] = this match {
- case None => Iterator.empty
- case Some(x) => Iterator.fromValues(x)
+ /** Apply the given procedure <code>f</code> to the option's value,
+ * if it is nonempty. Do nothing if it is empty.
+ * @param f the procedure to apply.
+ */
+ def foreach(f: A => Unit) {
+ if (!isEmpty) f(this.get)
}
- def toList: List[A] = this match {
- case None => List()
- case Some(x) => List(x)
- }
+ /** If the option is nonempty return it,
+ * otherwise return the result of evaluating an alternative expression.
+ * @param alternative the alternative expression.
+ */
+ def orElse[B >: A](alternative: => Option[B]): Option[B] =
+ if (isEmpty) alternative else this
+
+ /** An singleton iterator returning the option's value if it is nonempty
+ * or the empty iterator if the option is empty.
+ */
+ def elements: Iterator[A] =
+ if (isEmpty) Iterator.empty else Iterator.fromValues(this.get)
+ /** A singleton list containing the option's value if it is nonempty
+ * or the empty list if the option is empty.
+ */
+ def toList: List[A] =
+ if (isEmpty) List() else List(this.get)
}
/** Class <code>Some[A]</code> represents existing values of type
@@ -88,10 +111,18 @@ sealed abstract class Option[+A] extends Product {
* @author Martin Odersky
* @version 1.0, 16/07/2003
*/
-final case class Some[+A](x: A) extends Option[A]
+final case class Some[+A](x: A) extends Option[A] {
+ def isEmpty = false
+ def get = x
+}
+
+
/** This case object represents non-existent values.
*
* @author Martin Odersky
* @version 1.0, 16/07/2003
*/
-case object None extends Option[Nothing];
+case object None extends Option[Nothing] {
+ def isEmpty = true
+ def get = throw new NoSuchElementException("None.get")
+}
diff --git a/src/library/scala/runtime/BoxedArray.scala b/src/library/scala/runtime/BoxedArray.scala
index 8559dfde18..3ba9433f8a 100644
--- a/src/library/scala/runtime/BoxedArray.scala
+++ b/src/library/scala/runtime/BoxedArray.scala
@@ -72,8 +72,8 @@ abstract class BoxedArray extends Seq[Any] {
// todo: eliminate
def subArray(from: Int, end: Int): AnyRef
- override def slice(from: Int, len: Int): Seq[Object] =
- subArray(from, from + len).asInstanceOf[Seq[Object]]
+ override def slice(from: Int, end: Int): Seq[Object] =
+ subArray(from, end).asInstanceOf[Seq[Object]]
final override def map[b](f: Any => b): Array[b] = {
val len = length