Lens question: Adding a key in a nested object

I’m using profunctor lenses and have an Object (Object Int) (Object meaning Foreign.Object).

Assuming no knowledge of the contents this object, I’d like to access the object below the key foo, and inside this object, modify bar to 3. If those keys don’t exist, they should be created. My initial idea was:

set (at "foo" <<< traversed <<< at "bar") (Just 3) myobject

But that fails when I type it as Lens' MyState (Maybe Int) (the compiler complains about a missing Wander instance). When I type it as Traversal' (Object (Object Int)) (Maybe Int), it type-checks, but doesn’t actually modify the object.

What’s the problem here and can this be solved with lenses?

Maybe you want non?

at "foo" <<< non Object.empty <<< at "bar"

Ah, I think that’s what I want indeed. Thanks!

1 Like