Open Lev135 opened 1 year ago
Maybe you want insertWith union foo (singleton bar baz)
?
> f a b c = insertWith union a (singleton b c)
> :t f
f :: (Ord k1, Ord k2) => k1 -> k2 -> a -> Map k1 (Map k2 a) -> Map k1 (Map k2 a)
> f "0" "1" "2" mempty
fromList [("0",fromList [("1","2")])]
> f "0" "1" "2" $ singleton "0" mempty
fromList [("0",fromList [("1","2")])]
> f "0" "1" "2" $ singleton "0" $ singleton "1" ""
fromList [("0",fromList [("1","2")])]
Alternatively, see https://hackage.haskell.org/package/monoidal-containers. That should let you do nested inserts direcly
let path = singleton a $ singleton b $ singleton c $ First d
in path <> oldMap
Maybe, I'm doing something wrong, but this combination is very frequent in my programms. Imagine we have some nested map
and we want to insert a value
baz
to thebar
offoo
. I think it would be nice to have something liketo call this like
mupdate (insert bar baz) foo
. Maybe with upsert from #809 it will be also acceptable --- justupsert (insert bar baz . fromMaybe mempty) foo
or even there is a perfect way using currently implemented functionality, but I can't see them.P. S. of course, this can be easily done throw
non
from lens, but lens are not always advisable