diff options
author | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2007-01-31 14:05:04 +0000 |
---|---|---|
committer | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2007-01-31 14:05:04 +0000 |
commit | f6f011d167194dbc5704b97b113fc646855f6031 (patch) | |
tree | b7915799fcf93cab1333a6a110a3bb34fa7595cd /src | |
parent | cc77b573c34324e0034a0e4c7834913f17fe348e (diff) | |
download | scala-f6f011d167194dbc5704b97b113fc646855f6031.tar.gz scala-f6f011d167194dbc5704b97b113fc646855f6031.tar.bz2 scala-f6f011d167194dbc5704b97b113fc646855f6031.zip |
Small updates to collection library:
- added shorter mkString with single separator parameter
- ListMap's "elements" Iterator keeps element order.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/Iterable.scala | 8 | ||||
-rw-r--r-- | src/library/scala/collection/immutable/ListMap.scala | 15 |
2 files changed, 16 insertions, 7 deletions
diff --git a/src/library/scala/Iterable.scala b/src/library/scala/Iterable.scala index 42a5f2eb82..6ef8e2ec06 100644 --- a/src/library/scala/Iterable.scala +++ b/src/library/scala/Iterable.scala @@ -359,6 +359,14 @@ trait Iterable[+A] { addString(buf, start, sep, end).toString } + /** Returns a string representation of this iterable object. The string + * representations of elements (w.r.t. the method <code>toString()</code>) + * are separated by the string <code>sep</code>. + * + * @param sep separator string. + * @return a string representation of this iterable object. */ + def mkString(sep: String): String = this.mkString("", sep, "") + /** Write all elements of this string into given string builder */ def addString(buf: StringBuilder, start: String, sep: String, end: String): StringBuilder = { buf.append(start) diff --git a/src/library/scala/collection/immutable/ListMap.scala b/src/library/scala/collection/immutable/ListMap.scala index 782d471aa4..1705378846 100644 --- a/src/library/scala/collection/immutable/ListMap.scala +++ b/src/library/scala/collection/immutable/ListMap.scala @@ -79,13 +79,14 @@ class ListMap[A, +B] extends Map[A, B] { /** Returns an iterator over key-value pairs. */ - def elements: Iterator[Pair[A,B]] = new Iterator[Pair[A,B]] { - var self: ListMap[A,B] = ListMap.this - def hasNext = !self.isEmpty - def next: Pair[A,B] = - if (!hasNext) throw new NoSuchElementException("next on empty iterator") - else { val res = Pair(self.key, self.value); self = self.next; res } - } + def elements: Iterator[Pair[A,B]] = + new Iterator[Pair[A,B]] { + var self: ListMap[A,B] = ListMap.this + def hasNext = !self.isEmpty + def next: Pair[A,B] = + if (!hasNext) throw new NoSuchElementException("next on empty iterator") + else { val res = Pair(self.key, self.value); self = self.next; res } + }.toList.reverse.elements protected def key: A = throw new NoSuchElementException("empty map") protected def value: B = throw new NoSuchElementException("empty map") |