summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Zenger <mzenger@gmail.com>2004-04-23 10:11:21 +0000
committerMatthias Zenger <mzenger@gmail.com>2004-04-23 10:11:21 +0000
commit6ab8129e5852ab9cb981a5a9a0bb93e6fcd42ee0 (patch)
treec381b98e04f0aae79047f860c643430a8a2aaa37
parent278d0ef80e8d4c681f350afbbe14f2e076d6a2c1 (diff)
downloadscala-6ab8129e5852ab9cb981a5a9a0bb93e6fcd42ee0.tar.gz
scala-6ab8129e5852ab9cb981a5a9a0bb93e6fcd42ee0.tar.bz2
scala-6ab8129e5852ab9cb981a5a9a0bb93e6fcd42ee0.zip
*** empty log message ***
-rw-r--r--config/list/library.lst2
-rw-r--r--sources/scala/Iterable.scala18
-rw-r--r--sources/scala/List.scala51
-rw-r--r--sources/scala/Predef.scala22
4 files changed, 72 insertions, 21 deletions
diff --git a/config/list/library.lst b/config/list/library.lst
index d21ad26cd7..f1774505c1 100644
--- a/config/list/library.lst
+++ b/config/list/library.lst
@@ -3,7 +3,6 @@
##############################################################################
# $Id$
-$colon$colon.scala
AnyVal.java
Array.java
Boolean.java
@@ -32,7 +31,6 @@ Iterator.scala
List.scala
Long.java
MatchError.java
-Nil.scala
None.scala
Option.scala
Ord.scala
diff --git a/sources/scala/Iterable.scala b/sources/scala/Iterable.scala
index a93c3352a3..87f2d33636 100644
--- a/sources/scala/Iterable.scala
+++ b/sources/scala/Iterable.scala
@@ -9,6 +9,24 @@
package scala;
+object Iterable {
+ def view[A <% Ordered[A]](x: Iterable[A]): Ordered[Iterable[A]] = new Ordered[Iterable[A]] {
+ def compareTo[B >: Iterable[A] <% Ordered[B]](that: B): Int = that match {
+ case y: Iterable[A] =>
+ val xs = x.elements;
+ val ys = y.elements;
+ var res = 0;
+ while (xs.hasNext && ys.hasNext && (res == 0)) {
+ res = xs.next compareTo ys.next;
+ }
+ if (xs.hasNext) 1
+ else if (ys.hasNext) -1
+ else res;
+ case _ =>
+ -(that compareTo x)
+ }
+ }
+}
/** Collection classes supporting this trait provide a method
* <code>elements</code> which returns an iterator over all the
diff --git a/sources/scala/List.scala b/sources/scala/List.scala
index b11c6d688a..bc8936e2c0 100644
--- a/sources/scala/List.scala
+++ b/sources/scala/List.scala
@@ -9,6 +9,8 @@
package scala;
+import java.io.Serializable;
+
/** This object provides methods for creating specialized lists, and for
* transforming special kinds of lists (e.g. lists of lists).
@@ -159,7 +161,6 @@ object List {
}
/** Lists with ordered elements are ordered
- * not yet since not compilable with bootstrap
*/
def view[a <% Ordered[a]](x: List[a]): Ordered[List[a]] = new Ordered[List[a]] {
def compareTo [b >: List[a] <% Ordered[b]](y: b): int = y match {
@@ -171,9 +172,9 @@ object List {
else if (xs.isEmpty) -1
else if (ys.isEmpty) 1
else {
- val s = xs.head compareTo ys.head;
- if (s != 0) s
- else compareLists(xs.tail, ys.tail)
+ val s = xs.head compareTo ys.head;
+ if (s != 0) s
+ else compareLists(xs.tail, ys.tail)
}
}
}
@@ -188,10 +189,10 @@ object List {
* @author Martin Odersky and others
* @version 1.0, 16/07/2003
*/
-trait List[+a] extends Seq[a] {
+sealed trait List[+a] extends Seq[a] {
- /** Tests if this list is empty.
- * @return true, iff the list contains no element.
+ /** Returns true if the list does not contain any elements.
+ * @return true, iff the list is empty.
*/
def isEmpty: Boolean;
@@ -462,8 +463,8 @@ trait List[+a] extends Seq[a] {
case head :: tail =>
val tail1 = tail filter p;
if (p(head))
- if (tail eq tail1) this
- else head :: tail1
+ if (tail eq tail1) this
+ else head :: tail1
else tail1
};
@@ -517,7 +518,7 @@ trait List[+a] extends Seq[a] {
if (lt(x, y)) x::(y::acc) else y::x::acc
case List(x, y, z) =>
if (lt(x, y)) {
- if (lt(y, z)) x::y::z::acc
+ if (lt(y, z)) x::y::z::acc
else if (lt(x, z)) x::z::y::acc
else z::x::y::acc
} else if (lt(x, z)) y::x::z::acc
@@ -538,16 +539,16 @@ trait List[+a] extends Seq[a] {
if (lt(x, y)) this else y::x::Nil
case List(x, y, z) =>
if (lt(x, y)) {
- if (lt(y, z)) this
+ if (lt(y, z)) this
else if (lt(x, z)) x::z::y::Nil
else z::x::y::Nil
} else if (lt(x, z)) y::x::z::Nil
else if (lt(z, y)) z::y::x::Nil
else y::z::x::Nil
case hd1::hd2::hd3::tail => {
- val List(x, y, z) = sort_1(hd1::hd2::hd3::Nil, Nil);
- val Pair(small,large) = tail.partition((e2) => lt(e2, y));
- sort_1(x::small, y::sort_1(z::large, Nil));
+ val List(x, y, z) = sort_1(hd1::hd2::hd3::Nil, Nil);
+ val Pair(small,large) = tail.partition((e2) => lt(e2, y));
+ sort_1(x::small, y::sort_1(z::large, Nil));
}
}
}
@@ -740,6 +741,28 @@ trait List[+a] extends Seq[a] {
if (tail contains head) tail.removeDuplicates
else head :: tail.removeDuplicates
}
+}
+/** The empty list.
+ *
+ * @author Martin Odersky
+ * @version 1.0, 15/07/2003
+ */
+case object Nil extends List[All] with Serializable {
+ private val serialVersionUID = 0 - 8256821097970055419L;
+ def isEmpty = true;
+ def head: All = error("head of empty list");
+ def tail: List[All] = error("tail of empty list");
}
+/** A non empty list characterized by a head and a tail.
+ *
+ * @author Martin Odersky
+ * @version 1.0, 15/07/2003
+ */
+final case class ::[b](hd: b, tl: List[b]) extends List[b] with Serializable {
+ private val serialVersionUID = 0 - 8476791151983527571L;
+ def isEmpty: boolean = false;
+ def head: b = hd;
+ def tail: List[b] = tl;
+}
diff --git a/sources/scala/Predef.scala b/sources/scala/Predef.scala
index 4063a32f64..9691941201 100644
--- a/sources/scala/Predef.scala
+++ b/sources/scala/Predef.scala
@@ -16,7 +16,7 @@ package scala;
*/
object Predef {
-// aliases -------------------------------------------------------
+ // aliases -------------------------------------------------------
type byte = scala.Byte;
type short = scala.Short;
@@ -44,7 +44,7 @@ object Predef {
type Function[-a,+b] = Function1[a,b];
-// arrays -----------------------------------------------------------
+ // arrays -----------------------------------------------------------
/** Create an array with given elements.
*
@@ -58,7 +58,7 @@ object Predef {
array;
}
-// errors and asserts -------------------------------------------------
+ // errors and asserts -------------------------------------------------
def error(message: String): All = throw new Error(message);
@@ -73,7 +73,7 @@ object Predef {
throw new Error("assertion failed: " + message);
}
-// views -------------------------------------------------------------
+ // views -------------------------------------------------------------
def view(x: int): Ordered[int] = new Ordered[int] {
def compareTo [b >: int <% Ordered[b]](y: b): int = y match {
@@ -84,6 +84,7 @@ object Predef {
case _ => -(y compareTo x)
}
}
+
def view(x: char): Ordered[char] = new Ordered[char] {
def compareTo [b >: char <% Ordered[b]](y: b): int = y match {
case y1: char =>
@@ -93,6 +94,7 @@ object Predef {
case _ => -(y compareTo x)
}
}
+
def view(x: long): Ordered[long] = new Ordered[long] {
def compareTo [b >: long <% Ordered[b]](y: b): int = y match {
case y1: long =>
@@ -102,6 +104,7 @@ object Predef {
case _ => -(y compareTo x)
}
}
+
def view(x: float): Ordered[float] = new Ordered[float] {
def compareTo [b >: float <% Ordered[b]](y: b): int = y match {
case y1: float =>
@@ -111,6 +114,7 @@ object Predef {
case _ => -(y compareTo x)
}
}
+
def view(x: double): Ordered[double] = new Ordered[double] {
def compareTo [b >: double <% Ordered[b]](y: b): int = y match {
case y1: double =>
@@ -120,6 +124,7 @@ object Predef {
case _ => -(y compareTo x)
}
}
+
def view(x: boolean): Ordered[boolean] = new Ordered[boolean] {
def compareTo [b >: boolean <% Ordered[b]](y: b): int = y match {
case y1: boolean =>
@@ -129,11 +134,18 @@ object Predef {
case _ => -(y compareTo x)
}
}
+
def view(x: String): Ordered[String] = new Ordered[String] {
def compareTo [b >: String <% Ordered[b]](y: b): int = y match {
case y1: String => x compareTo y1;
case _ => -(y compareTo x)
}
}
-}
+ def view[A](xs: Array[A]): Seq[A] = new Seq[A] {
+ def length = xs.length;
+ def elements = Iterator.fromArray(xs);
+ def apply(n: Int) = xs(n);
+ override protected def stringPrefix: String = "Array";
+ }
+}