From afabca613118c1864d8130b5940341b9f0249c12 Mon Sep 17 00:00:00 2001 From: Matthias Zenger Date: Fri, 3 Oct 2003 09:32:55 +0000 Subject: - Added new methods - Bug fixes --- sources/scala/List.scala | 57 ++++++++++++++++++---- sources/scala/Option.scala | 25 +++++++--- sources/scala/Seq.scala | 19 +++----- .../scala/collection/mutable/JavaSetAdaptor.scala | 2 +- 4 files changed, 74 insertions(+), 29 deletions(-) (limited to 'sources') diff --git a/sources/scala/List.scala b/sources/scala/List.scala index 8f3e854493..9459184829 100644 --- a/sources/scala/List.scala +++ b/sources/scala/List.scala @@ -70,8 +70,53 @@ object List { val Pair(fs, ss) = unzip(tail); Pair(f :: fs, s :: ss) } + + /** Converts an array into a list. + * @param arr the array to convert + * @returns a list that contains the same elements than arr + * in the same order + */ + def fromArray[a](arr: Array[a]): List[a] = fromArray(arr, 0, arr.length); + + /** Converts a range of an array into a list. + * @param arr the array to convert + * @param start the first index to consider + * @param len the lenght of the range to convert + * @returns a list that contains the same elements than arr + * in the same order + */ + def fromArray[a](arr: Array[a], start: Int, len: Int): List[a] = { + var res: List[a] = Nil; + var i = start + len; + while (i > start) { + i = i - 1; + res = arr(i) :: res; + } + res + } + + /** Parses a string which contains substrings separated by a + * separator character and returns a list of all substrings. + * @param str the string to parse + * @param separator the separator character + * @return the list of substrings + */ + def fromString(str: String, separator: Char): List[String] = { + var res: List[String] = Nil; + var start = 0; + var end = str.indexOf(separator); + while (end >= 0) { + if (end > start) + res = str.substring(start, end) :: res; + end = end + 1; + start = end; + end = str.indexOf(separator, end); + } + res.reverse + } } + /** A trait representing an ordered collection of elements of type * a. This class comes with two implementing case * classes scala.Nil and scala.:: that @@ -441,14 +486,6 @@ trait List[+a] extends Seq[a] { def reverse: List[a] = foldLeft(Nil : List[a])((xs, x) => x :: xs); - /* - def toArray: Array[a] = { - val xs = new Array[a](length); - copyToArray(xs, 0); - xs - } - */ - /** Fills the given array xs with the elements of * this list starting at position start. Does not * work with empty lists. @@ -457,8 +494,8 @@ trait List[+a] extends Seq[a] { * @return the given array xs filled with this list. * @throws error if the list is empty. */ - def copyToArray[b >: a](xs: Array[b], start: Int): int = match { - case Nil => start + def copyToArray[b >: a](xs: Array[b], start: Int): Array[b] = match { + case Nil => xs case y :: ys => xs(start) = y; ys.copyToArray(xs, start + 1) } diff --git a/sources/scala/Option.scala b/sources/scala/Option.scala index 0f373db10c..b037a3ef04 100644 --- a/sources/scala/Option.scala +++ b/sources/scala/Option.scala @@ -15,36 +15,47 @@ package scala; * object None. * * @author Martin Odersky + * @author Matthias Zenger * @version 1.0, 16/07/2003 */ -trait Option[+a] { +trait Option[+A] extends Iterable[A] { - def get: a = this match { + def isEmpty: Boolean = this match { + case None => true + case _ => false + } + + def get: A = this match { case None => error("None.get") case Some(x) => x } - def map[b](f: a => b): Option[b] = this match { + def map[B](f: A => B): Option[B] = this match { case None => None case Some(x) => Some(f(x)) } - def flatMap[b](f: a => Option[b]): Option[b] = this match { + def flatMap[B](f: A => Option[B]): Option[B] = this match { case None => None case Some(x) => f(x) } - def filter(p: a => boolean): Option[a] = this match { + def filter(p: A => Boolean): Option[A] = this match { case None => None case Some(x) => if (p(x)) Some(x) else None } - def foreach(f: a => Unit): Unit = this match { + def foreach(f: A => Unit): Unit = this match { case None => () case Some(x) => f(x) } - def toList: List[a] = this match { + def elements: Iterator[A] = this match { + case None => Iterator.empty + case Some(x) => Iterator.fromSeq(x) + } + + def toList: List[A] = this match { case None => Predef.List() case Some(x) => Predef.List(x) } diff --git a/sources/scala/Seq.scala b/sources/scala/Seq.scala index fac644ab4a..f5e8d54577 100644 --- a/sources/scala/Seq.scala +++ b/sources/scala/Seq.scala @@ -14,6 +14,7 @@ package scala; * of type A. * * @author Martin Odersky + * @author Matthias Zenger * @version 1.0, 16/07/2003 */ trait Seq[+A] with PartialFunction[Int, A] with Iterable[A] { @@ -35,17 +36,13 @@ trait Seq[+A] with PartialFunction[Int, A] with Iterable[A] { * @return a string representation of this sequence. */ override def toString() = { - def toString1(it: Iterator[A]):String = { - if (it.hasNext) { - ",".concat(it.next.toString()) - .concat(toString1(it)) - } else - ")" + val iter = elements; + var res = "Seq("; + if (iter.hasNext) { + res = res + iter.next; + while (iter.hasNext) + res = res + ", " + iter.next; } - val it = elements; - if (it.hasNext) - "Seq(" + it.next.toString() + toString1(it) - else - "Seq()" + res + ")" } } diff --git a/sources/scala/collection/mutable/JavaSetAdaptor.scala b/sources/scala/collection/mutable/JavaSetAdaptor.scala index 39f6ae5755..172c056c21 100644 --- a/sources/scala/collection/mutable/JavaSetAdaptor.scala +++ b/sources/scala/collection/mutable/JavaSetAdaptor.scala @@ -16,7 +16,7 @@ package scala.collection.mutable; * @author Matthias Zenger * @version 1.0, 19/09/2003 */ -class JavaSetAdaptor[A, B](jset: java.util.Set) extends Set[A] { +class JavaSetAdaptor[A](jset: java.util.Set) extends Set[A] { def size: Int = jset.size(); -- cgit v1.2.3