summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authorMatthias Zenger <mzenger@gmail.com>2003-10-03 09:32:55 +0000
committerMatthias Zenger <mzenger@gmail.com>2003-10-03 09:32:55 +0000
commitafabca613118c1864d8130b5940341b9f0249c12 (patch)
treed8f21c0dca91267b4ce25b696d81fb5b2b1d7031 /sources
parent6ca43bcd97900f82748f4525f68d08c56a8047ce (diff)
downloadscala-afabca613118c1864d8130b5940341b9f0249c12.tar.gz
scala-afabca613118c1864d8130b5940341b9f0249c12.tar.bz2
scala-afabca613118c1864d8130b5940341b9f0249c12.zip
- Added new methods
- Bug fixes
Diffstat (limited to 'sources')
-rw-r--r--sources/scala/List.scala57
-rw-r--r--sources/scala/Option.scala25
-rw-r--r--sources/scala/Seq.scala19
-rw-r--r--sources/scala/collection/mutable/JavaSetAdaptor.scala2
4 files changed, 74 insertions, 29 deletions
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 <code>arr</code>
+ * 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 <code>arr</code>
+ * 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
* <code>a</code>. This class comes with two implementing case
* classes <code>scala.Nil</code> and <code>scala.::</code> 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 <code>xs</code> with the elements of
* this list starting at position <code>start</code>. Does not
* work with empty lists.
@@ -457,8 +494,8 @@ trait List[+a] extends Seq[a] {
* @return the given array <code>xs</code> 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 <code>None</code>.
*
* @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 <code>A</code>.
*
* @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();