1

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?

Qrilka
  • 612
  • 1
  • 6
  • 19

2 Answers2

3

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

Qrilka
  • 612
  • 1
  • 6
  • 19
2

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
daniel gratzer
  • 52,833
  • 11
  • 94
  • 134
  • Maybe it's possible to use `maybe` (or its lens analog)? At the moment it seems like explicit creation of new "submap" should look a little bit better – Qrilka Dec 04 '13 at 06:58