summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Zenger <mzenger@gmail.com>2004-03-15 10:34:09 +0000
committerMatthias Zenger <mzenger@gmail.com>2004-03-15 10:34:09 +0000
commit35338a63997d29210cea61b5dc2de5dee60c0d81 (patch)
tree66ca31b7b7d4d4209a484c42e5835820f2734d5a
parent6d13331746d6f81f4b0650c1f06d73c35f3436c2 (diff)
downloadscala-35338a63997d29210cea61b5dc2de5dee60c0d81.tar.gz
scala-35338a63997d29210cea61b5dc2de5dee60c0d81.tar.bz2
scala-35338a63997d29210cea61b5dc2de5dee60c0d81.zip
Refactored code.
-rw-r--r--sources/scala/Iterable.scala111
-rw-r--r--sources/scala/Iterator.scala73
-rw-r--r--sources/scala/Seq.scala30
-rw-r--r--sources/scala/collection/immutable/Tree.scala2
-rw-r--r--sources/scala/collection/immutable/TreeMap.scala2
5 files changed, 121 insertions, 97 deletions
diff --git a/sources/scala/Iterable.scala b/sources/scala/Iterable.scala
index 88ac6d0035..a93c3352a3 100644
--- a/sources/scala/Iterable.scala
+++ b/sources/scala/Iterable.scala
@@ -31,7 +31,10 @@ trait Iterable[+A] {
*
* @param f a function that is applied to every element.
*/
- def foreach(f: A => Unit): Unit = elements foreach f;
+ def foreach(f: A => Unit): Unit = {
+ val it = elements;
+ while (it.hasNext) { f(it.next) }
+ }
/** Apply a predicate <code>p</code> to all elements of this
* iterable object and return true, iff the predicate yields
@@ -40,69 +43,109 @@ trait Iterable[+A] {
* @param p the predicate
* @returns true, iff the predicate yields true for all elements.
*/
- def forall(p: A => Boolean): Boolean = elements forall p;
+ def forall(p: A => Boolean): Boolean = {
+ val it = elements;
+ var res = true;
+ while (res && it.hasNext) { res = p(it.next) }
+ res
+ }
- /** Apply a predicate <code>p</code> to all elements of this
- * iterable object and return true, iff there is at least one
- * element for which <code>p</code> yields true.
+ /** Apply a predicate <code>p</code> to all elements of this
+ * iterable object and return true, iff there is at least one
+ * element for which <code>p</code> yields true.
*
* @param p the predicate
* @returns true, iff the predicate yields true for at least one element.
*/
- def exists(p: A => Boolean): Boolean = elements exists p;
+ def exists(p: A => Boolean): Boolean = {
+ val it = elements;
+ var res = false;
+ while (!res && it.hasNext) { res = p(it.next) }
+ res
+ }
+
+ /** Find and return the first element of the iterable object satisfying a
+ * predicate, if any.
+ *
+ * @param p the predicate
+ * @return the first element in the iterable object satisfying <code>p</code>,
+ * or <code>None</code> if none exists.
+ */
+ def find(p: A => Boolean): Option[A] = {
+ val it = elements;
+ var res: Option[A] = None;
+ while (res.isEmpty && it.hasNext) {
+ val e = it.next;
+ if (p(e))
+ res = Some(e);
+ }
+ res
+ }
- /** Combines the elements of this list together using the binary
+ /** Combines the elements of this list together using the binary
* operator <code>op</code>, from left to right, and starting with
* the value <code>z</code>.
* @return <code>op(... (op(op(z,a0),a1) ...), an)</code> if the list
* is <code>List(a0, a1, ..., an)</code>.
*/
- def foldLeft[B](z: B)(op: (B, A) => B): B = elements.foldLeft(z)(op);
+ def foldLeft[B](z: B)(op: (B, A) => B): B = {
+ val it = elements;
+ var acc = z;
+ while (it.hasNext) {
+ acc = op(acc, it.next)
+ }
+ acc
+ }
- /** Combines the elements of this list together using the binary
- * operator <code>op</code>, from rigth to left, and starting with
- * the value <code>z</code>.
- * @return <code>a0 op (... op (an op z)...)</code> if the list
- * is <code>[a0, a1, ..., an]</code>.
- */
- def foldRight[B](z: B)(f: (A, B) => B): B = match {
- case Nil => z
- case x :: xs => f(x, xs.foldRight(z)(f))
- }
+ /** Combines the elements of this list together using the binary
+ * operator <code>op</code>, from rigth to left, and starting with
+ * the value <code>z</code>.
+ * @return <code>a0 op (... op (an op z)...)</code> if the list
+ * is <code>[a0, a1, ..., an]</code>.
+ */
+ def foldRight[B](z: B)(op: (A, B) => B): B = {
+ val it = elements;
+ def fold(z: B): B =
+ if (it.hasNext) op(it.next, fold(z)) else z;
+ fold(z)
+ }
/** Similar to <code>foldLeft</code> but can be used as
* an operator with the order of list and zero arguments reversed.
* That is, <code>z /: xs</code> is the same as <code>xs foldLeft z</code>
*/
- def /:[B](z: B)(f: (B, A) => B): B = foldLeft(z)(f);
+ def /:[B](z: B)(f: (B, A) => B): B = foldLeft(z)(f);
/** An alias for <code>foldRight</code>.
* That is, <code>xs :\ z</code> is the same as <code>xs foldRight z</code>
*/
- def :\[B](z: B)(f: (A, B) => B): B = foldRight(z)(f);
+ def :\[B](z: B)(f: (A, B) => B): B = foldRight(z)(f);
/** Transform this iterable object into a list of all elements.
*
* @return a list which enumerates all elements of this set.
*/
def toList: List[A] = {
+ val it = elements;
var res: List[A] = Nil;
- elements foreach { elem => res = elem :: res; }
+ while (it.hasNext) {
+ res = it.next :: res;
+ }
res.reverse
}
- def similar(x:Any) = {
- x match {
- case that:Iterable[A] =>
- val ita = this.elements;
- val itb = that.elements;
- var res = true;
- while( ita.hasNext && itb.hasNext && res ) {
- res = ( ita.next == itb.next );
- };
- !ita.hasNext && !itb.hasNext && res
- case _ =>
- false
- }
+ /** Checks if the other iterable object contains the same elements.
+ *
+ * @param that the other iterable object
+ * @return true, iff both iterable objects contain the same elements.
+ */
+ def sameElements[B >: A](that: Iterable[B]): Boolean = {
+ val ita = this.elements;
+ val itb = that.elements;
+ var res = true;
+ while (res && ita.hasNext && itb.hasNext) {
+ res = (ita.next == itb.next);
+ }
+ !ita.hasNext && !itb.hasNext && res
}
}
diff --git a/sources/scala/Iterator.scala b/sources/scala/Iterator.scala
index 0345ebe4af..f8cc965355 100644
--- a/sources/scala/Iterator.scala
+++ b/sources/scala/Iterator.scala
@@ -13,7 +13,8 @@ package scala;
/** The <code>Iterator</code> object provides various functions for
* creating specialized iterators.
*
- * @author Martin Odersky, Matthias Zenger
+ * @author Martin Odersky
+ * @author Matthias Zenger
* @version 1.1, 04/02/2004
*/
object Iterator {
@@ -61,27 +62,17 @@ object Iterator {
* if there is a next element available, and a <code>next</code> method
* which returns the next element and discards it from the iterator.
*
- * @author Martin Odersky, Matthias Zenger
- * @version 1.1, 04/02/2004
+ * @author Martin Odersky
+ * @author Matthias Zenger
+ * @version 1.2, 15/03/2004
*/
trait Iterator[+a] with Iterable[a] {
- def hasNext: Boolean;
- def next: a;
- override def foreach(f: a => Unit): Unit =
- while (hasNext) { f(next) }
+ def hasNext: Boolean;
- override def forall(p: a => Boolean): Boolean = {
- var res = true;
- while (res && hasNext) { res = p(next); }
- res;
- }
+ def next: a;
- override def exists(p: a => Boolean): Boolean = {
- var res = false;
- while (!res && hasNext) { res = p(next); }
- res;
- }
+ def elements: Iterator[a] = this;
def take(n: Int) = new Iterator[a] {
var remaining = n;
@@ -91,24 +82,14 @@ trait Iterator[+a] with Iterable[a] {
else error("next on empty iterator");
}
- def drop(n: Int): Iterator[a] = if (n > 0) { next; drop(n - 1); } else this;
+ def drop(n: Int): Iterator[a] =
+ if (n > 0) { next; drop(n - 1) } else this;
def map[b](f: a => b): Iterator[b] = new Iterator[b] {
def hasNext = Iterator.this.hasNext;
def next = f(Iterator.this.next)
}
- override def foldLeft[b](z: b)(f: (b, a) => b): b = {
- var acc = z;
- while( hasNext ) {
- acc = f(acc, next)
- }
- acc
- }
-
- override def foldRight[b](z: b)(f: (a, b) => b): b =
- if (hasNext) f(next, foldRight(z)(f)) else z;
-
def append[b >: a](that: Iterator[b]) = new Iterator[b] {
def hasNext = Iterator.this.hasNext || that.hasNext;
def next = if (Iterator.this.hasNext) Iterator.this.next else that.next;
@@ -118,12 +99,16 @@ trait Iterator[+a] with Iterable[a] {
private var cur: Iterator[b] = Iterator.empty;
def hasNext: Boolean =
if (cur.hasNext) true
- else if (Iterator.this.hasNext) { cur = f(Iterator.this.next); hasNext }
- else false;
+ else if (Iterator.this.hasNext) {
+ cur = f(Iterator.this.next);
+ hasNext
+ } else false;
def next: b =
if (cur.hasNext) cur.next
- else if (Iterator.this.hasNext) { cur = f(Iterator.this.next); next }
- else error("next on empty iterator");
+ else if (Iterator.this.hasNext) {
+ cur = f(Iterator.this.next);
+ next
+ } else error("next on empty iterator");
}
def filter(p: a => Boolean): Iterator[a] = new BufferedIterator[a] {
@@ -131,12 +116,9 @@ trait Iterator[+a] with Iterable[a] {
Iterator.this.buffered;
private def skip: Unit =
while (source.hasNext && !p(source.head)) { source.next; () }
- def hasNext: Boolean =
- { skip; source.hasNext }
- def next: a =
- { skip; source.next }
- def head: a =
- { skip; source.head; }
+ def hasNext: Boolean = { skip; source.hasNext }
+ def next: a = { skip; source.next }
+ def head: a = { skip; source.head; }
}
def zip[b](that: Iterator[b]) = new Iterator[Pair[a, b]] {
@@ -148,19 +130,18 @@ trait Iterator[+a] with Iterable[a] {
private var hd: a = _;
private var ahead: Boolean = false;
def head: a = {
- if (!ahead) { hd = Iterator.this.next; ahead = true }
+ if (!ahead) {
+ hd = Iterator.this.next;
+ ahead = true
+ }
hd
}
def next: a =
- if (ahead) { ahead = false; hd }
- else head;
- def hasNext: Boolean =
- ahead || Iterator.this.hasNext;
+ if (ahead) { ahead = false; hd } else head;
+ def hasNext: Boolean = ahead || Iterator.this.hasNext;
override def buffered: BufferedIterator[a] = this;
}
- def elements: Iterator[a] = this;
-
def duplicate: Pair[Iterator[a], Iterator[a]] = {
var xs: List[a] = Nil;
var ahead: Iterator[a] = null;
diff --git a/sources/scala/Seq.scala b/sources/scala/Seq.scala
index e3534ce27e..3f86281eba 100644
--- a/sources/scala/Seq.scala
+++ b/sources/scala/Seq.scala
@@ -19,18 +19,6 @@ package scala;
*/
trait Seq[+A] with PartialFunction[Int, A] with Iterable[A] with Similarity {
- /** Returns true if the elements in this sequence are equal
- * to the elements in another sequence
- */
- override def similar( x:Any ):boolean = {
- x.match {
- case that:Seq[A] =>
- ( that.length == this.length ) && super[Iterable].similar( that )
- case _ =>
- false
- };
- }
-
/** Returns the length of the sequence.
*
* @return the sequence length.
@@ -43,6 +31,18 @@ trait Seq[+A] with PartialFunction[Int, A] with Iterable[A] with Similarity {
*/
def isDefinedAt(x: Int): Boolean = (x >= 0) && (x < length);
+ /** Returns true if the elements in this sequence are equal
+ * to the elements in another sequence
+ */
+ def similar(x: Any): Boolean = {
+ x.match {
+ case that: Seq[A] =>
+ (that.length == this.length) && sameElements(that)
+ case _ =>
+ false
+ }
+ }
+
/** Customizes the <code>toString</code> method.
*
* @return a string representation of this sequence.
@@ -51,9 +51,9 @@ trait Seq[+A] with PartialFunction[Int, A] with Iterable[A] with Similarity {
val iter = elements;
var res = "Seq(";
if (iter.hasNext) {
- res = res + iter.next;
- while (iter.hasNext)
- res = res + ", " + iter.next;
+ res = res + iter.next;
+ while (iter.hasNext)
+ res = res + ", " + iter.next;
}
res + ")"
}
diff --git a/sources/scala/collection/immutable/Tree.scala b/sources/scala/collection/immutable/Tree.scala
index 12a2c58ccc..5dff37a307 100644
--- a/sources/scala/collection/immutable/Tree.scala
+++ b/sources/scala/collection/immutable/Tree.scala
@@ -167,7 +167,7 @@ abstract class Tree[KEY,Entry](order:Order[KEY]) {
* @param key the key of the mapping of interest
* @return the value of the mapping, if it exists
*/
- def find(key:KEY) = tree.get(key);
+ def findValue(key:KEY) = tree.get(key);
/**
* Gives you an iterator over all elements in the tree.
diff --git a/sources/scala/collection/immutable/TreeMap.scala b/sources/scala/collection/immutable/TreeMap.scala
index 7c9ffefc62..097506f7a0 100644
--- a/sources/scala/collection/immutable/TreeMap.scala
+++ b/sources/scala/collection/immutable/TreeMap.scala
@@ -61,7 +61,7 @@ package scala.collection.immutable;
* @return the value of the mapping, if it exists
*/
override def get(key:KEY) =
- find(key).match {
+ findValue(key).match {
case Some(Pair(_,value:VALUE)) => Some(value);
case _ => None;
}