Do you need to change the type of value? If not, and if your value can be assigned, you can use map[key] = new_value or equivalently hana::at_key(map, key) = new_value , since hana::at_key returns a link.
If you need to change the type of value, this is more complicated. We will not be able to do anything in place, because the type of card after replacing the value will be different from its type before replacing the value. Therefore, we must create a new map or some modified representation of this map (but representations are not currently supported). Using erase_key and insert will actually create two cards, which is inefficient. Instead, we could provide some update function that would achieve the same, but create only one copy of the map (result). I believe that we could do better than erase_key + insert in terms of compilation time by providing our own function. I discovered this problem in order to track your request, since I think it is important to provide something like this; thanks.
Finally, I would like to comment on what Jason said:
In another note, perhaps hana :: map should be Functor.
hana::map could be made Functor, but I'm not sure that transform can touch the map keys, while observing the laws of Functor. If this is not the case, you still cannot say "replace the value with XXX if the key satisfies some predicates" using, for example, hana::replace_if . I tried (but failed so far) to find an example of functions that would violate laws if hana::transform basically transformed the sequence of underlying key / value pairs, but my intuition is that you can find such an example. To be continued in issue No. 278.
source share