From cc4427befb91bcdc183008d8a0cc727f0549f55e Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 18 Jan 2007 16:27:05 +0000 Subject: updated Optiona and Array --- src/library/scala/Array.scala | 4 +- src/library/scala/Option.scala | 113 ++++++++++++++++++----------- src/library/scala/runtime/BoxedArray.scala | 4 +- 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 len elements * starting at index from * @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 from < 0 * or length < from + len */ - 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 p. 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 None, 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. Some(f(this.get)). + * 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 p + * yields false on its value, return None. + * 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 f 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 Some[A] 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 -- cgit v1.2.3