summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2007-10-26 17:26:45 +0000
committerMartin Odersky <odersky@gmail.com>2007-10-26 17:26:45 +0000
commite97eb8f50e2ae30c3a651b7f975282659a57e817 (patch)
tree5835476f458a667bc24576b1e550ec6f760f00ef /src/library
parent74c60ffa67853c170e4f23e500c0380a2118c8b4 (diff)
downloadscala-e97eb8f50e2ae30c3a651b7f975282659a57e817.tar.gz
scala-e97eb8f50e2ae30c3a651b7f975282659a57e817.tar.bz2
scala-e97eb8f50e2ae30c3a651b7f975282659a57e817.zip
fixed tickets 152, 123.
spreadsheet demo crash. Made Lists more tail recursive. toString in Sets and Maps now says just Set(...) or Map(...) without revealing the implementation.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/List.scala34
-rw-r--r--src/library/scala/Responder.scala2
-rw-r--r--src/library/scala/collection/Map.scala4
-rw-r--r--src/library/scala/collection/Set.scala4
-rw-r--r--src/library/scala/util/parsing/combinator1/lexical/StdLexical.scala8
5 files changed, 40 insertions, 12 deletions
diff --git a/src/library/scala/List.scala b/src/library/scala/List.scala
index 38028b7385..537f649a8a 100644
--- a/src/library/scala/List.scala
+++ b/src/library/scala/List.scala
@@ -456,9 +456,14 @@ sealed abstract class List[+A] extends Seq[A] {
* @param prefix the prefix to reverse and then prepend
* @return the concatenation of the reversed prefix and the current list.
*/
- def reverse_:::[B >: A](prefix: List[B]): List[B] = prefix match {
- case Nil => this
- case head :: tail => (head :: this).reverse_:::(tail)
+ def reverse_:::[B >: A](prefix: List[B]): List[B] = {
+ var these: List[B] = this
+ var pres = prefix
+ while (!pres.isEmpty) {
+ these = pres.head :: these
+ pres = pres.tail
+ }
+ these
}
/** Returns the number of elements in the list.
@@ -540,8 +545,15 @@ sealed abstract class List[+A] extends Seq[A] {
*/
override def last: A =
if (isEmpty) throw new Predef.NoSuchElementException("Nil.last")
- else if (tail.isEmpty) head
- else tail.last
+ else {
+ var cur = this
+ var next = this.tail
+ while (!next.isEmpty) {
+ cur = next
+ next = next.tail
+ }
+ cur.head
+ }
/** Returns the <code>n</code> first elements of this list, or else the whole
* list, if it has less than <code>n</code> elements.
@@ -580,9 +592,15 @@ sealed abstract class List[+A] extends Seq[A] {
* @param n the number of elements to drop.
* @return the list without its <code>n</code> first elements.
*/
- override def drop(n: Int): List[A] =
- if (isEmpty || n <= 0) this
- else (tail drop (n-1))
+ override def drop(n: Int): List[A] = {
+ var these = this
+ var count = n
+ while (!these.isEmpty && count > 0) {
+ these = these.tail
+ count -= 1
+ }
+ these
+ }
/** Returns the rightmost <code>n</code> elements from this list.
*
diff --git a/src/library/scala/Responder.scala b/src/library/scala/Responder.scala
index 9aade29aa1..fd7e5f1b35 100644
--- a/src/library/scala/Responder.scala
+++ b/src/library/scala/Responder.scala
@@ -12,6 +12,7 @@ package scala
/** This object contains utility methods to build responders.
*
+ * @author Martin Odersky
* @author Burak Emir
* @version 1.0
*
@@ -60,6 +61,7 @@ object Responder {
* Scala while giving the impression that programs in these DSLs are
* written in direct style.
*
+ * @author Martin Odersky
* @author Burak Emir
* @version 1.0
*/
diff --git a/src/library/scala/collection/Map.scala b/src/library/scala/collection/Map.scala
index 015bda5f21..fafdf8e175 100644
--- a/src/library/scala/collection/Map.scala
+++ b/src/library/scala/collection/Map.scala
@@ -191,4 +191,8 @@ trait Map[A, +B] extends PartialFunction[A, B] with Collection[(A, B)] {
override def contains(key : A) = Map.this.contains(key)
override def get(key : A) = Map.this.get(key).map(f)
}
+
+ /** Defines the prefix of this object's <code>toString</code> representation.
+ */
+ override protected def stringPrefix : String = "Map"
}
diff --git a/src/library/scala/collection/Set.scala b/src/library/scala/collection/Set.scala
index 4f412712d6..aac1342a37 100644
--- a/src/library/scala/collection/Set.scala
+++ b/src/library/scala/collection/Set.scala
@@ -124,4 +124,8 @@ trait Set[A] extends (A => Boolean) with Collection[A] {
copyToArray(result, 0)
result
}
+
+ /** Defines the prefix of this object's <code>toString</code> representation.
+ */
+ override protected def stringPrefix : String = "Set"
}
diff --git a/src/library/scala/util/parsing/combinator1/lexical/StdLexical.scala b/src/library/scala/util/parsing/combinator1/lexical/StdLexical.scala
index fc04116692..e278869f7a 100644
--- a/src/library/scala/util/parsing/combinator1/lexical/StdLexical.scala
+++ b/src/library/scala/util/parsing/combinator1/lexical/StdLexical.scala
@@ -37,9 +37,9 @@ class StdLexical extends Lexical with StdTokens {
def token: Parser[Token] =
( letter ~ rep( letter | digit ) ^^ { case first ~ rest => processIdent(first :: rest mkString "") }
| digit ~ rep( digit ) ^^ { case first ~ rest => NumericLit(first :: rest mkString "") }
- | '\'' ~ rep( chrExcept('\'', '\n', EofCh) ) ~ '\'' ^^ { case _ ~ chars ~ _ => StringLit(chars mkString "") }
- | '\"' ~ rep( chrExcept('\"', '\n', EofCh) ) ~ '\"' ^^ { case _ ~ chars ~ _ => StringLit(chars mkString "") }
- | EofCh ^^ const(EOF)
+ | '\'' ~ rep( chrExcept('\'', '\n', EofCh) ) ~ '\'' ^^ { case '\'' ~ chars ~ '\'' => StringLit(chars mkString "") }
+ | '\"' ~ rep( chrExcept('\"', '\n', EofCh) ) ~ '\"' ^^ { case '\"' ~ chars ~ '\"' => StringLit(chars mkString "") }
+ | EofCh ^^^ EOF
| '\'' ~> failure("unclosed string literal")
| '\"' ~> failure("unclosed string literal")
| delim
@@ -70,7 +70,7 @@ class StdLexical extends Lexical with StdTokens {
private var _delim: Parser[Token] = null
protected def delim: Parser[Token] = {
- if(_delim eq null) { // construct parser for delimiters by |'ing together the parsers for the individual delimiters,
+ if (_delim eq null) { // construct parser for delimiters by |'ing together the parsers for the individual delimiters,
// starting with the longest one (hence the sort + reverse) -- otherwise a delimiter D will never be matched if
// there is another delimiter that is a prefix of D
def parseDelim(s: String): Parser[Token] = accept(s.toList) ^^ { x => Keyword(s) }