From 96fd08897359e2851aad31b7b515225dd02b0195 Mon Sep 17 00:00:00 2001 From: Lex Spoon Date: Fri, 23 Mar 2007 16:46:49 +0000 Subject: - toString() on large collections now only prints a few of the elements - all maps now have getOrElse - mutable maps now have getOrElsePut --- src/library/scala/Seq.scala | 13 ++++++++++++- src/library/scala/collection/Map.scala | 25 ++++++++++++++++++++++--- src/library/scala/collection/Set.scala | 13 ++++++++++--- src/library/scala/collection/mutable/Map.scala | 11 +++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala index bdf2db2869..2dd89b5762 100644 --- a/src/library/scala/Seq.scala +++ b/src/library/scala/Seq.scala @@ -263,7 +263,18 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Iterable[A] { * * @return a string representation of this sequence. */ - override def toString() = mkString(stringPrefix+"(", ",", ")") + override def toString() = { + val middle = + if(length <= 20) + mkString(",") + else { + val topr = 2 + elements.take(topr).mkString(",") + + (", and " + (length - topr) + " more...") + } + stringPrefix + "(" + middle + ")" + } + /** Defines the prefix of the string representation. */ diff --git a/src/library/scala/collection/Map.scala b/src/library/scala/collection/Map.scala index 6b7e7cd0b1..d10b063d21 100644 --- a/src/library/scala/collection/Map.scala +++ b/src/library/scala/collection/Map.scala @@ -50,6 +50,15 @@ trait Map[A, +B] extends PartialFunction[A, B] with Iterable[(A, B)] { */ def get(key: A): Option[B] + /** Check if this map maps key to a value. + * Return that value if it exists, otherwise return default. + */ + def getOrElse[B2 >: B](key: A, default: B2): B2 = + get(key) match { + case Some(v) => v + case None => default + } + /** Is this an empty map? * * @return true iff the map is empty. @@ -139,10 +148,20 @@ trait Map[A, +B] extends PartialFunction[A, B] with Iterable[(A, B)] { /** Creates a string representation for this map. * - * @return a string showing all mappings + * @return a string showing all mappings, or a subset of them + * if the map is large. */ - override def toString() = - elements.toList.map(kv => kv._1 + " -> " + kv._2).mkString("Map(", ", ", ")") + override def toString() = { + def elem2str(kv: (A, B)) = kv._1 + " -> " + kv._2 + if(size <= 20) + elements.map(elem2str ).mkString("Map(", ", ", ")") + else { + val topr = 2 + val initStrs = elements.take(topr).map(elem2str) + initStrs.mkString("Map(", ", ", + ", and " + (size - topr) + " more ...)") + } + } /** The default value for the map, returned when a key is not found * The method implemented here yields an error, diff --git a/src/library/scala/collection/Set.scala b/src/library/scala/collection/Set.scala index 94cb1d9256..58e9640704 100644 --- a/src/library/scala/collection/Set.scala +++ b/src/library/scala/collection/Set.scala @@ -93,8 +93,15 @@ trait Set[A] extends (A => Boolean) with Iterable[A] { /** Returns a string representation of this set. * - * @return a string showing all elements of this set. + * @return a string showing all elements of this set, or a subset + * of them if the set is large. */ - override def toString(): String = mkString("Set(", ", ", ")") - + override def toString(): String = + if(size <= 20) + mkString("Set(", ", ", ")") + else { + val topr = 2 + elements.take(topr).mkString("Set(", ", ", + (", and " + (size - topr) + " more...)")) + } } diff --git a/src/library/scala/collection/mutable/Map.scala b/src/library/scala/collection/mutable/Map.scala index 6242eb5fea..4c908aed92 100644 --- a/src/library/scala/collection/mutable/Map.scala +++ b/src/library/scala/collection/mutable/Map.scala @@ -159,6 +159,17 @@ trait Map[A, B] extends AnyRef */ def clear(): Unit = keys foreach -= + /** Check if this map maps key to a value. + * Return that value if it exists, otherwise put default + * as that key's value and return it. + */ + def getOrElsePut(key: A, default: B): B = + get(key) match { + case Some(v) => v + case None => this(key) = default; default + } + + /** This function transforms all the values of mappings contained * in this map with function f. * -- cgit v1.2.3