Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ed2660c

Browse filesBrowse files
pawelkrupinskiPawel Krupinski
authored andcommitted
Added HashMap.map method to map keys and values.
1 parent 7075280 commit ed2660c
Copy full SHA for ed2660c

File tree

Expand file treeCollapse file tree

2 files changed

+27
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+27
-1
lines changed
Open diff view settings
Collapse file

‎core/src/main/java/fj/data/HashMap.java‎

Copy file name to clipboardExpand all lines: core/src/main/java/fj/data/HashMap.java
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ public Option<V> getDelete(final K k) {
243243
return fromNull(m.remove(new Key<K>(k, e, h)));
244244
}
245245

246+
public <A, B> HashMap<A, B> map(F<K, A> keyFunction,
247+
F<V, B> valueFunction,
248+
Equal<A> equal, Hash<A> hash) {
249+
final HashMap hashMap = new HashMap(equal, hash);
250+
for (K key : keys()) {
251+
final A newKey = keyFunction.f(key);
252+
final B newValue = valueFunction.f(get(key).some());
253+
hashMap.set(newKey, newValue);
254+
}
255+
return hashMap;
256+
}
257+
246258
public List<P2<K, V>> toList() {
247259
return keys().map(new F<K, P2<K, V>>() {
248260
public P2<K, V> f(final K k) {
Collapse file

‎core/src/test/fj/data/CheckHashMap.scala‎

Copy file name to clipboardExpand all lines: core/src/test/fj/data/CheckHashMap.scala
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package data
44
import org.scalacheck.Prop._
55
import ArbitraryHashMap._
66
import Equal.{intEqual, stringEqual, optionEqual}
7-
import Hash.intHash
7+
import Hash.{intHash, stringHash}
88
import fj.data.Option._
99
import scala.collection.JavaConversions._
1010
import org.scalacheck.{Arbitrary, Properties}
@@ -69,4 +69,18 @@ object CheckHashMap extends Properties("HashMap") {
6969
.forall((e: (Int, Iterable[P2[Int, String]])) => e._2
7070
.exists((elem: P2[Int, String]) => optionEqual(stringEqual).eq(map.get(e._1), Option.some(elem._2))))
7171
})
72+
73+
property("map") = forAll((m: HashMap[Int, String]) => {
74+
val keyFunction: F[Int, String] = (i: Int) => i.toString
75+
val valueFunction: (String) => String = (s: String) => s + "a"
76+
val mapped = m.map(keyFunction, valueFunction, stringEqual, stringHash)
77+
val keysAreEqual = m.keys().map(keyFunction).toSet == mapped.keys.toSet
78+
val appliedFunctionsToKeysAndValues: Boolean = m.keys().forall((key: Int) => {
79+
val mappedValue = mapped.get(keyFunction.f(key))
80+
val oldValueMapped = some(valueFunction.f(m.get(key).some()))
81+
Equal.optionEqual(stringEqual).eq(mappedValue, oldValueMapped)
82+
})
83+
84+
keysAreEqual && appliedFunctionsToKeysAndValues
85+
})
7286
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.