Skip to content

Commit f889af2

Browse files
committed
Tabs for Scala 2/3 in collections-2.13/maps.
1 parent 9f831d6 commit f889af2

File tree

1 file changed

+85
-47
lines changed

1 file changed

+85
-47
lines changed

_overviews/collections-2.13/maps.md

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,68 +24,87 @@ The fundamental operations on maps are similar to those on sets. They are summar
2424

2525
### Operations in Class Map ###
2626

27-
| WHAT IT IS | WHAT IT DOES |
28-
| ------ | ------ |
29-
| **Lookups:** | |
30-
| `ms get k` |The value associated with key `k` in map `ms` as an option, `None` if not found.|
31-
| `ms(k)` |(or, written out, `ms apply k`) The value associated with key `k` in map `ms`, or exception if not found.|
32-
| `ms getOrElse (k, d)` |The value associated with key `k` in map `ms`, or the default value `d` if not found.|
33-
| `ms contains k` |Tests whether `ms` contains a mapping for key `k`.|
34-
| `ms isDefinedAt k` |Same as `contains`. |
35-
| **Subcollections:** | |
27+
| WHAT IT IS | WHAT IT DOES |
28+
| ------ | ------ |
29+
| **Lookups:** | |
30+
| `ms.get(k)` |The value associated with key `k` in map `ms` as an option, `None` if not found.|
31+
| `ms(k)` |(or, written out, `ms.apply(k)`) The value associated with key `k` in map `ms`, or exception if not found.|
32+
| `ms.getOrElse(k, d)` |The value associated with key `k` in map `ms`, or the default value `d` if not found.|
33+
| `ms.contains(k)` |Tests whether `ms` contains a mapping for key `k`.|
34+
| `ms.isDefinedAt(k)` |Same as `contains`. |
35+
| **Subcollections:** | |
3636
| `ms.keys` |An iterable containing each key in `ms`. |
3737
| `ms.keySet` |A set containing each key in `ms`. |
3838
| `ms.keysIterator` |An iterator yielding each key in `ms`. |
3939
| `ms.values` |An iterable containing each value associated with a key in `ms`.|
4040
| `ms.valuesIterator` |An iterator yielding each value associated with a key in `ms`.|
41-
| **Transformation:** | |
42-
| `ms.view filterKeys p` |A map view containing only those mappings in `ms` where the key satisfies predicate `p`.|
43-
| `ms.view mapValues f` |A map view resulting from applying function `f` to each value associated with a key in `ms`.|
41+
| **Transformation:** | |
42+
| `ms.view.filterKeys(p)` |A map view containing only those mappings in `ms` where the key satisfies predicate `p`.|
43+
| `ms.view.mapValues(f)` |A map view resulting from applying function `f` to each value associated with a key in `ms`.|
4444

4545
Immutable maps support in addition operations to add and remove mappings by returning new `Map`s, as summarized in the following table.
4646

4747
### Operations in Class immutable.Map ###
4848

49-
| WHAT IT IS | WHAT IT DOES |
50-
| ------ | ------ |
51-
| **Additions and Updates:**| |
49+
| WHAT IT IS | WHAT IT DOES |
50+
| ------ | ------ |
51+
| **Additions and Updates:**| |
5252
| `ms.updated(k, v)`<br>or `ms + (k -> v)` |The map containing all mappings of `ms` as well as the mapping `k -> v` from key `k` to value `v`.|
53-
| **Removals:** | |
54-
| `ms remove k`<br>or `ms - k` |The map containing all mappings of `ms` except for any mapping of key `k`.|
55-
| `ms removeAll ks`<br>or `ms -- ks` |The map containing all mappings of `ms` except for any mapping with a key in `ks`.|
53+
| **Removals:** | |
54+
| `ms.remove(k)`<br>or `ms - k` |The map containing all mappings of `ms` except for any mapping of key `k`.|
55+
| `ms.removeAll(ks)`<br>or `ms -- ks` |The map containing all mappings of `ms` except for any mapping with a key in `ks`.|
5656

5757
Mutable maps support in addition the operations summarized in the following table.
5858

5959

6060
### Operations in Class mutable.Map ###
6161

62-
| WHAT IT IS | WHAT IT DOES |
63-
| ------ | ------ |
64-
| **Additions and Updates:**| |
65-
| `ms(k) = v` |(Or, written out, `ms.update(k, v)`). Adds mapping from key `k` to value `v` to map ms as a side effect, overwriting any previous mapping of `k`.|
66-
| `ms.addOne(k -> v)`<br>or `ms += (k -> v)` |Adds mapping from key `k` to value `v` to map `ms` as a side effect and returns `ms` itself.|
62+
| WHAT IT IS | WHAT IT DOES |
63+
| ------ | ------ |
64+
| **Additions and Updates:** | |
65+
| `ms(k) = v` |(Or, written out, `ms.update(k, v)`). Adds mapping from key `k` to value `v` to map ms as a side effect, overwriting any previous mapping of `k`.|
66+
| `ms.addOne(k -> v)`<br>or `ms += (k -> v)` |Adds mapping from key `k` to value `v` to map `ms` as a side effect and returns `ms` itself.|
6767
| `ms addAll xvs`<br>or `ms ++= kvs` |Adds all mappings in `kvs` to `ms` as a side effect and returns `ms` itself.|
68-
| `ms.put(k, v)` |Adds mapping from key `k` to value `v` to `ms` and returns any value previously associated with `k` as an option.|
69-
| `ms getOrElseUpdate (k, d)`|If key `k` is defined in map `ms`, return its associated value. Otherwise, update `ms` with the mapping `k -> d` and return `d`.|
70-
| **Removals:**| |
71-
| `ms subtractOne k`<br>or `ms -= k` |Removes mapping with key `k` from ms as a side effect and returns `ms` itself.|
72-
| `ms subtractAll ks`<br>or `ms --= ks` |Removes all keys in `ks` from `ms` as a side effect and returns `ms` itself.|
73-
| `ms remove k` |Removes any mapping with key `k` from `ms` and returns any value previously associated with `k` as an option.|
74-
| `ms filterInPlace p` |Keeps only those mappings in `ms` that have a key satisfying predicate `p`.|
75-
| `ms.clear()` |Removes all mappings from `ms`. |
76-
| **Transformation:** | |
77-
| `ms mapValuesInPlace f` |Transforms all associated values in map `ms` with function `f`.|
78-
| **Cloning:** | |
79-
| `ms.clone` |Returns a new mutable map with the same mappings as `ms`.|
68+
| `ms.put(k, v)` |Adds mapping from key `k` to value `v` to `ms` and returns any value previously associated with `k` as an option.|
69+
| `ms.getOrElseUpdate(k, d)` |If key `k` is defined in map `ms`, return its associated value. Otherwise, update `ms` with the mapping `k -> d` and return `d`.|
70+
| **Removals:** | |
71+
| `ms.subtractOne(k)`<br>or `ms -= k` |Removes mapping with key `k` from ms as a side effect and returns `ms` itself.|
72+
| `ms.subtractAll(ks)`<br>or `ms --= ks` |Removes all keys in `ks` from `ms` as a side effect and returns `ms` itself.|
73+
| `ms.remove(k)` |Removes any mapping with key `k` from `ms` and returns any value previously associated with `k` as an option.|
74+
| `ms.filterInPlace(p)` |Keeps only those mappings in `ms` that have a key satisfying predicate `p`.|
75+
| `ms.clear()` |Removes all mappings from `ms`. |
76+
| **Transformation:** | |
77+
| `ms.mapValuesInPlace(f)` |Transforms all associated values in map `ms` with function `f`.|
78+
| **Cloning:** | |
79+
| `ms.clone` |Returns a new mutable map with the same mappings as `ms`.|
8080

8181
The addition and removal operations for maps mirror those for sets. A mutable map `m` is usually updated "in place", using the two variants `m(key) = value` or `m += (key -> value)`. There is also the variant `m.put(key, value)`, which returns an `Option` value that contains the value previously associated with `key`, or `None` if the `key` did not exist in the map before.
8282

8383
The `getOrElseUpdate` is useful for accessing maps that act as caches. Say you have an expensive computation triggered by invoking a function `f`:
8484

85-
scala> def f(x: String) = {
86-
println("taking my time."); sleep(100)
87-
x.reverse }
88-
f: (x: String)String
85+
{% tabs expensive-computation-reverse class=tabs-scala-version %}
86+
87+
{% tab 'Scala 2' for=expensive-computation-reverse %}
88+
```scala
89+
scala> def f(x: String): String = {
90+
println("taking my time."); Thread.sleep(100)
91+
x.reverse
92+
}
93+
f: (x: String)String
94+
```
95+
{% endtab %}
96+
97+
{% tab 'Scala 3' for=expensive-computation-reverse %}
98+
```scala
99+
scala> def f(x: String): String =
100+
println("taking my time."); Thread.sleep(100)
101+
x.reverse
102+
103+
def f(x: String): String
104+
```
105+
{% endtab %}
106+
107+
{% endtabs %}
89108

90109
Assume further that `f` has no side-effects, so invoking it again with the same argument will always yield the same result. In that case you could save time by storing previously computed bindings of argument and results of `f` in a map and only computing the result of `f` if a result of an argument was not found there. One could say the map is a _cache_ for the computations of the function `f`.
91110

@@ -94,7 +113,7 @@ Assume further that `f` has no side-effects, so invoking it again with the same
94113

95114
You can now create a more efficient caching version of the `f` function:
96115

97-
scala> def cachedF(s: String) = cache.getOrElseUpdate(s, f(s))
116+
scala> def cachedF(s: String): String = cache.getOrElseUpdate(s, f(s))
98117
cachedF: (s: String)String
99118
scala> cachedF("abc")
100119
taking my time.
@@ -104,10 +123,29 @@ You can now create a more efficient caching version of the `f` function:
104123

105124
Note that the second argument to `getOrElseUpdate` is "by-name", so the computation of `f("abc")` above is only performed if `getOrElseUpdate` requires the value of its second argument, which is precisely if its first argument is not found in the `cache` map. You could also have implemented `cachedF` directly, using just basic map operations, but it would take more code to do so:
106125

107-
def cachedF(arg: String) = cache get arg match {
108-
case Some(result) => result
109-
case None =>
110-
val result = f(x)
111-
cache(arg) = result
112-
result
113-
}
126+
{% tabs cacheF class=tabs-scala-version %}
127+
128+
{% tab 'Scala 2' for=cacheF %}
129+
```scala
130+
def cachedF(arg: String): String = cache.get(arg) match {
131+
case Some(result) => result
132+
case None =>
133+
val result = f(x)
134+
cache(arg) = result
135+
result
136+
}
137+
```
138+
{% endtab %}
139+
140+
{% tab 'Scala 3' for=cacheF %}
141+
```scala
142+
def cachedF(arg: String): String = cache.get(arg) match
143+
case Some(result) => result
144+
case None =>
145+
val result = f(x)
146+
cache(arg) = result
147+
result
148+
```
149+
{% endtab %}
150+
151+
{% endtabs %}

0 commit comments

Comments
 (0)