55import com .jnape .palatable .lambda .functions .builtin .fn2 .Filter ;
66import com .jnape .palatable .lambda .lens .Lens ;
77
8+ import java .util .ArrayList ;
89import java .util .Collection ;
910import java .util .HashMap ;
1011import java .util .HashSet ;
1718import static com .jnape .palatable .lambda .functions .builtin .fn2 .Map .map ;
1819import static com .jnape .palatable .lambda .functions .builtin .fn2 .ToCollection .toCollection ;
1920import static com .jnape .palatable .lambda .functions .builtin .fn2 .ToMap .toMap ;
20- import static com .jnape .palatable .lambda .lens .Lens .lens ;
2121import static com .jnape .palatable .lambda .lens .Lens .simpleLens ;
2222import static com .jnape .palatable .lambda .lens .functions .View .view ;
2323import static com .jnape .palatable .lambda .lens .lenses .MaybeLens .unLiftA ;
24- import static java . util . stream . Collectors . toSet ;
24+ import static com . jnape . palatable . lambda . lens . lenses . MaybeLens . unLiftB ;
2525
2626/**
2727 * Lenses that operate on {@link Map}s.
@@ -45,20 +45,29 @@ public static <K, V> Lens.Simple<Map<K, V>, Map<K, V>> asCopy() {
4545 /**
4646 * A lens that focuses on a value at a key in a map, as a {@link Maybe}.
4747 *
48- * @param k the key to focus on
4948 * @param <K> the key type
5049 * @param <V> the value type
50+ * @param k the key to focus on
5151 * @return a lens that focuses on the value at key, as a {@link Maybe}
5252 */
53- public static <K , V > Lens <Map <K , V >, Map <K , V >, Maybe <V >, V > valueAt (K k ) {
54- return lens (m -> maybe (m .get (k )), (m , v ) -> {
55- m .put (k , v );
56- return m ;
53+ public static <K , V > Lens .Simple <Map <K , V >, Maybe <V >> valueAt (K k ) {
54+ return simpleLens (m -> maybe (m .get (k )), (m , maybeV ) -> {
55+ Map <K , V > updated = new HashMap <>(m );
56+ return maybeV .fmap (v -> {
57+ updated .put (k , v );
58+ return updated ;
59+ }).orElseGet (() -> {
60+ updated .remove (k );
61+ return updated ;
62+ });
5763 });
5864 }
5965
6066 /**
6167 * A lens that focuses on a value at a key in a map, falling back to <code>defaultV</code> if the value is missing.
68+ * <p>
69+ * Note that this lens is NOT lawful, since "putting back what you got changes nothing" fails for any value
70+ * <code>B</code> where <code>S</code> is the empty map
6271 *
6372 * @param k the key to focus on
6473 * @param defaultValue the default value to use in case of a missing value at key
@@ -68,7 +77,7 @@ public static <K, V> Lens<Map<K, V>, Map<K, V>, Maybe<V>, V> valueAt(K k) {
6877 */
6978 @ SuppressWarnings ("unchecked" )
7079 public static <K , V > Lens .Simple <Map <K , V >, V > valueAt (K k , V defaultValue ) {
71- return unLiftA (valueAt (k ), defaultValue )::apply ;
80+ return unLiftB ( unLiftA (valueAt (k ), defaultValue ) )::apply ;
7281 }
7382
7483 /**
@@ -79,32 +88,38 @@ public static <K, V> Lens.Simple<Map<K, V>, V> valueAt(K k, V defaultValue) {
7988 * @return a lens that focuses on the keys of a map
8089 */
8190 public static <K , V > Lens .Simple <Map <K , V >, Set <K >> keys () {
82- return simpleLens (Map ::keySet , (m , ks ) -> {
83- Set <K > keys = m .keySet ();
84- keys .retainAll (ks );
85- ks .removeAll (keys );
86- ks .forEach (k -> m .put (k , null ));
87- return m ;
91+ return simpleLens (m -> new HashSet <>(m .keySet ()), (m , ks ) -> {
92+ HashSet <K > ksCopy = new HashSet <>(ks );
93+ Map <K , V > updated = new HashMap <>(m );
94+ Set <K > keys = updated .keySet ();
95+ keys .retainAll (ksCopy );
96+ ksCopy .removeAll (keys );
97+ ksCopy .forEach (k -> updated .put (k , null ));
98+ return updated ;
8899 });
89100 }
90101
91102 /**
92103 * A lens that focuses on the values of a map. In the case of updating the map, only the entries with a value listed
93104 * in the update collection of values are kept.
105+ * <p>
106+ * Note that this lens is NOT lawful, since "you get back what you put in" fails for all values <code>B</code> that
107+ * represent a non-surjective superset of the existing values in <code>S</code>.
94108 *
95109 * @param <K> the key type
96110 * @param <V> the value type
97111 * @return a lens that focuses on the values of a map
98112 */
99113 public static <K , V > Lens .Simple <Map <K , V >, Collection <V >> values () {
100- return simpleLens (Map ::values , (m , vs ) -> {
114+ return simpleLens (m -> new ArrayList <>(m .values ()), (m , vs ) -> {
115+ Map <K , V > updated = new HashMap <>(m );
101116 Set <V > valueSet = new HashSet <>(vs );
102- Set <K > matchingKeys = m . entrySet (). stream ( )
103- .filter ( kv -> valueSet . contains ( kv . getValue () ))
104- .map ( Map . Entry :: getKey )
105- .collect ( toSet ());
106- m .keySet ().retainAll (matchingKeys );
107- return m ;
117+ Set <K > matchingKeys = Filter .< Map . Entry < K , V >> filter ( kv -> valueSet . contains ( kv . getValue ()) )
118+ .andThen ( map ( Map . Entry :: getKey ))
119+ .andThen ( toCollection ( HashSet :: new ) )
120+ .apply ( updated . entrySet ());
121+ updated .keySet ().retainAll (matchingKeys );
122+ return updated ;
108123 });
109124 }
110125
@@ -122,19 +137,24 @@ public static <K, V> Lens.Simple<Map<K, V>, Map<V, K>> inverted() {
122137 m .forEach ((key , value ) -> inverted .put (value , key ));
123138 return inverted ;
124139 }, (m , im ) -> {
125- m .clear ();
126- m .putAll (view (inverted (), im ));
127- return m ;
140+ Map <K , V > updated = new HashMap <>(m );
141+ updated .clear ();
142+ updated .putAll (view (inverted (), im ));
143+ return updated ;
128144 });
129145 }
130146
131147 /**
132148 * A lens that focuses on a map while mapping its values with the mapping function.
149+ * <p>
150+ * Note that this lens is NOT lawful, since "you get back what you put in" fails for all values <code>B</code> that
151+ * do not map from the current values in <code>S</code> (new mappings cannot be preserved as the inversion of
152+ * <code>fn</code> is not known).
133153 *
134154 * @param fn the mapping function
135155 * @param <K> the key type
136156 * @param <V> the unfocused map value type
137- * @param <V2> the focused map value type
157+ * @param <V2> the focused map value types
138158 * @return a lens that focuses on a map while mapping its values
139159 */
140160 public static <K , V , V2 > Lens .Simple <Map <K , V >, Map <K , V2 >> mappingValues (Function <? super V , ? extends V2 > fn ) {
0 commit comments