User trapdoor showed up that there is a nicer solution - http://juick.com/qrilka/2602819#19 and it is right in the lens library : non
Is it possible to nest `at` with setters?
-
28-08-2022 - |
Domanda
I'd like to set a value in 2 level map - i.e. to a key in a map which is a value for some key in an "outer" map. For ordinary Map I could use something like m & at 42 ?= "value"
But I could not find any way to nest 2 at
's
What is the easiest way to set such nested value using lens?
Soluzione
Altri suggerimenti
Kind of, we can use the _Just
prism which will no-op if there is no key available. This has the unfortunate effect that we can't create keys 2 layers down. But as an example
at2 :: Ord k => k -> k -> IndexedTraversal' k (Map k (Map k v)) (Maybe v)
at2 k1 k2 = at k1 . _Just . at k2
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow