diff options
author | Paul Phillips <paulp@improving.org> | 2010-10-19 22:14:29 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-10-19 22:14:29 +0000 |
commit | 9bd74024a120aca0c777ee3917084776875daa69 (patch) | |
tree | 2ed674dcc5e08e09a9d10eaa468c211af3ac4ffb /src/library/scala/collection/immutable/Map.scala | |
parent | 8b17d54737ba37bc96f91bfcd69590acb3f728e9 (diff) | |
download | scala-9bd74024a120aca0c777ee3917084776875daa69.tar.gz scala-9bd74024a120aca0c777ee3917084776875daa69.tar.bz2 scala-9bd74024a120aca0c777ee3917084776875daa69.zip |
A modified approach to map withDefault[Value].
Diffstat (limited to 'src/library/scala/collection/immutable/Map.scala')
-rw-r--r-- | src/library/scala/collection/immutable/Map.scala | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/src/library/scala/collection/immutable/Map.scala b/src/library/scala/collection/immutable/Map.scala index ef1f6b60ed..272caec5ea 100644 --- a/src/library/scala/collection/immutable/Map.scala +++ b/src/library/scala/collection/immutable/Map.scala @@ -7,7 +7,6 @@ \* */ - package scala.collection package immutable @@ -32,6 +31,12 @@ trait Map[A, +B] extends Iterable[(A, B)] override def empty: Map[A, B] = Map.empty + /** The same map with a given default function */ + def withDefault[B1 >: B](d: A => B1): immutable.Map[A, B1] = new Map.WithDefault[A, B1](this, d) + + /** The same map with a given default value */ + def withDefaultValue[B1 >: B](d: B1): immutable.Map[A, B1] = new Map.WithDefault[A, B1](this, x => d) + /** Add a key/value pair to this map. * @param key the key * @param value the value @@ -52,6 +57,15 @@ object Map extends ImmutableMapFactory[Map] { def empty[A, B]: Map[A, B] = EmptyMap.asInstanceOf[Map[A, B]] + class WithDefault[A, +B](underlying: Map[A, B], d: A => B) extends collection.Map.WithDefault[A, B](underlying, d) with Map[A, B] { + override def empty = new WithDefault(underlying.empty, d) + override def updated[B1 >: B](key: A, value: B1): WithDefault[A, B1] = new WithDefault[A, B1](underlying.updated[B1](key, value), d) + override def + [B1 >: B](kv: (A, B1)): WithDefault[A, B1] = updated(kv._1, kv._2) + override def - (key: A): WithDefault[A, B] = new WithDefault(underlying - key, d) + override def withDefault[B1 >: B](d: A => B1): immutable.Map[A, B1] = new WithDefault[A, B1](underlying, d) + override def withDefaultValue[B1 >: B](d: B1): immutable.Map[A, B1] = new WithDefault[A, B1](underlying, x => d) + } + @serializable private object EmptyMap extends Map[Any, Nothing] { override def size: Int = 0 |