Skip to content

Commit 0ed79ba

Browse files
committed
squashed another stream usage in MapLens#mappingValues
1 parent ea011d4 commit 0ed79ba

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

  • src/main/java/com/jnape/palatable/lambda/lens/lenses

‎src/main/java/com/jnape/palatable/lambda/lens/lenses/MapLens.java‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.lens.lenses;
22

33
import com.jnape.palatable.lambda.adt.Maybe;
4+
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
45
import com.jnape.palatable.lambda.functions.builtin.fn2.Filter;
56
import com.jnape.palatable.lambda.lens.Lens;
67

@@ -15,11 +16,11 @@
1516
import static com.jnape.palatable.lambda.functions.builtin.fn2.Eq.eq;
1617
import static com.jnape.palatable.lambda.functions.builtin.fn2.Map.map;
1718
import static com.jnape.palatable.lambda.functions.builtin.fn2.ToCollection.toCollection;
19+
import static com.jnape.palatable.lambda.functions.builtin.fn2.ToMap.toMap;
1820
import static com.jnape.palatable.lambda.lens.Lens.lens;
1921
import static com.jnape.palatable.lambda.lens.Lens.simpleLens;
2022
import static com.jnape.palatable.lambda.lens.functions.View.view;
2123
import static com.jnape.palatable.lambda.lens.lenses.MaybeLens.unLiftA;
22-
import static java.util.stream.Collectors.toMap;
2324
import static java.util.stream.Collectors.toSet;
2425

2526
/**
@@ -137,16 +138,16 @@ public static <K, V> Lens.Simple<Map<K, V>, Map<V, K>> inverted() {
137138
* @return a lens that focuses on a map while mapping its values
138139
*/
139140
public static <K, V, V2> Lens.Simple<Map<K, V>, Map<K, V2>> mappingValues(Function<? super V, ? extends V2> fn) {
140-
return Lens.simpleLens(m -> m.entrySet().stream().collect(toMap(Map.Entry::getKey, kv -> fn.apply(kv.getValue()))),
141-
(s, b) -> {
142-
//todo: remove this madness upon arrival of either invertible functions or Iso<V,V2>
143-
Set<K> retainKeys = Filter.<Map.Entry<K, V>>filter(kv -> eq(fn.apply(kv.getValue()), b.get(kv.getKey())))
144-
.andThen(map(Map.Entry::getKey))
145-
.andThen(toCollection(HashSet::new))
146-
.apply(s.entrySet());
147-
Map<K, V> copy = new HashMap<>(s);
148-
copy.keySet().retainAll(retainKeys);
149-
return copy;
150-
});
141+
return simpleLens(m -> toMap(HashMap::new, map(t -> t.biMapR(fn), map(Tuple2::fromEntry, m.entrySet()))),
142+
(s, b) -> {
143+
//todo: remove this madness upon arrival of either invertible functions or Iso<V,V2>
144+
Set<K> retainKeys = Filter.<Map.Entry<K, V>>filter(kv -> eq(fn.apply(kv.getValue()), b.get(kv.getKey())))
145+
.andThen(map(Map.Entry::getKey))
146+
.andThen(toCollection(HashSet::new))
147+
.apply(s.entrySet());
148+
Map<K, V> copy = new HashMap<>(s);
149+
copy.keySet().retainAll(retainKeys);
150+
return copy;
151+
});
151152
}
152153
}

0 commit comments

Comments
 (0)