summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2003-02-17 09:07:13 +0000
committerMartin Odersky <odersky@gmail.com>2003-02-17 09:07:13 +0000
commit4bb5759c296e187279c923c313445fff1a2956a5 (patch)
tree25132863329fdf6bd0a9216ea35dcbae9dade462
parentfaca8cb93fb09150e59108294c9321bf1e690dd7 (diff)
downloadscala-4bb5759c296e187279c923c313445fff1a2956a5.tar.gz
scala-4bb5759c296e187279c923c313445fff1a2956a5.tar.bz2
scala-4bb5759c296e187279c923c313445fff1a2956a5.zip
New classes Symbol, Labelled, Seq.
-rw-r--r--sources/scala/Labelled.scala3
-rw-r--r--sources/scala/List.scala30
-rw-r--r--sources/scala/Predef.scala24
-rw-r--r--sources/scala/Seq.scala6
-rw-r--r--sources/scala/Stream.scala6
-rw-r--r--sources/scala/Symbol.scala4
6 files changed, 44 insertions, 29 deletions
diff --git a/sources/scala/Labelled.scala b/sources/scala/Labelled.scala
new file mode 100644
index 0000000000..9967185572
--- /dev/null
+++ b/sources/scala/Labelled.scala
@@ -0,0 +1,3 @@
+package scala;
+
+case class Labelled(symbol: Symbol, elements: List[+Any]) {}
diff --git a/sources/scala/List.scala b/sources/scala/List.scala
index d1b04319f5..a6b44d9dfe 100644
--- a/sources/scala/List.scala
+++ b/sources/scala/List.scala
@@ -8,7 +8,7 @@ package scala {
*
* @arg a the type of the elements contained in the list.
*/
- trait List[a] {
+ trait List[a] extends Seq[a] {
/** Tests if this list is empty.
* @return True iff the list contains no element.
@@ -27,13 +27,6 @@ package scala {
*/
def tail: List[a];
- /** Throws a runtime exception with output message <code>x</code>. This method
- * is used to signal an error.
- * @param x message to be displayed.
- * @throws java.lang.RuntimeException with the given message <code>x</code>.
- */
- protected def error[a](x: String):a = (new java.lang.RuntimeException(x)).throw;
-
/** Add an element <code>x</code> at the beginning of this list.
* <p>
* Ex:<br>
@@ -59,10 +52,17 @@ package scala {
/** Returns the number of elements in the list.
* @return the number of elements in the list.
*/
- def length: Int = {
- this match {
- case Nil() => 0
- case _ :: xs => xs.length + 1}
+ def length: Int = this match {
+ case Nil() => 0
+ case _ :: xs => xs.length + 1
+ }
+
+ /** Returns the elements in the list as an iterator
+ */
+ def elements: Iterator[a] = new Iterator[a] {
+ var current = List.this;
+ def hasNext: Boolean = !current.isEmpty;
+ def next: a = { val result = current.head; current = current.tail; result }
}
/** Returns the list without its last element.
@@ -182,7 +182,7 @@ package scala {
*/
def :_foldl[b](z: b)(f: (b, a) => b): b = match {
case Nil() => z
- case x :: xs => (xs.:_foldl@[b](f(z, x)))(f)
+ case x :: xs => (xs.:_foldl[b](f(z, x)))(f)
}
def foldr[b](z: b)(f: (a, b) => b): b = match {
@@ -281,9 +281,9 @@ package scala {
* <code>[a0, ..., an] zip [b0, ..., bn]</code> is invoked.
* @throws java.lang.RuntimeException if lists have different lengths.
*/
- def zip[b](that: List[b]): List[[a,b]] =
+ def zip[b](that: List[b]): List[Tuple2[a,b]] =
if (this.isEmpty || that.isEmpty) Nil()
- else [this.head, that.head] :: this.tail.zip(that.tail);
+ else Tuple2(this.head, that.head) :: this.tail.zip(that.tail);
/** Tests if the given value <code>elem</code> is a member of
* this list.
diff --git a/sources/scala/Predef.scala b/sources/scala/Predef.scala
index f504813aab..cebb909d40 100644
--- a/sources/scala/Predef.scala
+++ b/sources/scala/Predef.scala
@@ -5,16 +5,12 @@ package scala {
val True = Boolean.True;
val False = Boolean.False;
- def List[a](xs: Nil[a]): List[a] = [];
- def List[a](xs: [a]): List[a] = xs._1 :: [];
- def List[a](xs: [a, a]): List[a] = xs._1 :: xs._2 :: [];
- def List[a](xs: [a, a, a]): List[a] = xs._1 :: xs._2 :: xs._3 :: [];
- def List[a](xs: [a, a, a, a]): List[a] = xs._1 :: xs._2 :: xs._3 :: xs._4 :: [];
- def List[a](xs: [a, a, a, a, a]): List[a] = xs._1 :: xs._2 :: xs._3 :: xs._4 :: xs._5 :: [];
- def List[a](xs: [a, a, a, a, a, a]): List[a] = xs._1 :: xs._2 :: xs._3 :: xs._4 :: xs._5 :: xs._6 :: [];
- def List[a](xs: [a, a, a, a, a, a, a]): List[a] = xs._1 :: xs._2 :: xs._3 :: xs._4 :: xs._5 :: xs._6 :: xs._7 :: [];
- def List[a](xs: [a, a, a, a, a, a, a, a]): List[a] = xs._1 :: xs._2 :: xs._3 :: xs._4 :: xs._5 :: xs._6 :: xs._7 :: xs._8 :: [];
- def List[a](xs: [a, a, a, a, a, a, a, a, a]): List[a] = xs._1 :: xs._2 :: xs._3 :: xs._4 :: xs._5 :: xs._6 :: xs._7 :: xs._8 :: xs._9 :: [];
+ def List[a](x: a*): List[a] = {
+ def mkList(elems: Iterator[a]): List[a] =
+ if (elems.hasNext) elems.next :: mkList(elems)
+ else Nil();
+ mkList(x.elements);
+ }
def error[a](x: String):a = (new java.lang.RuntimeException(x)).throw;
@@ -22,7 +18,7 @@ package scala {
new ConsStreamClass(hd, () => tl);
def range(lo: Int, hi: Int): List[Int] =
- if (lo > hi) [] else lo :: range(lo + 1, hi);
+ if (lo > hi) List() else lo :: range(lo + 1, hi);
def while(def condition: Boolean)(def command: Unit): Unit =
if (condition) {
@@ -42,6 +38,12 @@ package scala {
else until(condition)
}
}
+
+ type Pair[a, b] = Tuple2[a, b];
+ def Pair[a, b](x: a, y: b) = Tuple2(x, y);
+
+ type Triple[a, b, c] = Tuple3[a, b, c];
+ def Triple[a, b, c](x: a, y: b, z: c) = Tuple3(x, y, z);
}
}
diff --git a/sources/scala/Seq.scala b/sources/scala/Seq.scala
new file mode 100644
index 0000000000..2ab0d37e78
--- /dev/null
+++ b/sources/scala/Seq.scala
@@ -0,0 +1,6 @@
+package scala;
+
+trait Seq[a] {
+ def length: Int;
+ def elements: Iterator[a];
+}
diff --git a/sources/scala/Stream.scala b/sources/scala/Stream.scala
index fe6cd98130..2211517eed 100644
--- a/sources/scala/Stream.scala
+++ b/sources/scala/Stream.scala
@@ -103,9 +103,9 @@ trait Stream[a] {
tail.copyToArray(xs, start + 1)
}
- def zip[b](that: Stream[b]): Stream[[a, b]] =
- if (this.isEmpty || that.isEmpty) new EmptyStream[[a, b]]()
- else ConsStream([this.head, that.head], this.tail.zip(that.tail));
+ def zip[b](that: Stream[b]): Stream[Tuple2[a, b]] =
+ if (this.isEmpty || that.isEmpty) new EmptyStream[Tuple2[a, b]]()
+ else ConsStream(Tuple2(this.head, that.head), this.tail.zip(that.tail));
def print: Unit =
if (isEmpty) System.out.println("EmptyStream")
diff --git a/sources/scala/Symbol.scala b/sources/scala/Symbol.scala
new file mode 100644
index 0000000000..e180b93125
--- /dev/null
+++ b/sources/scala/Symbol.scala
@@ -0,0 +1,4 @@
+package scala;
+
+case class Symbol(name: String) {}
+