From f6f011d167194dbc5704b97b113fc646855f6031 Mon Sep 17 00:00:00 2001 From: Gilles Dubochet Date: Wed, 31 Jan 2007 14:05:04 +0000 Subject: Small updates to collection library: - added shorter mkString with single separator parameter - ListMap's "elements" Iterator keeps element order. --- src/library/scala/Iterable.scala | 8 ++++++++ src/library/scala/collection/immutable/ListMap.scala | 15 ++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') 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 toString()) + * are separated by the string sep. + * + * @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") -- cgit v1.2.3