I want to use lens to create a function which updates a HashMap key if it exists; if the key doesn't exist, the function evaluates to Nothing. This is my attempt so far..
module Foo where
import Data.HashMap.Strict (HashMap, empty)
import Control.Lens
foo :: HashMap String Integer
foo = empty
-- I want this to be:
-- bar :: Maybe (HashMap String Integer)
bar :: HashMap String Integer
bar = over (at "1") (\_ -> Just 2) foo
This is a simplified example of what I'm ultimately trying to do -- the lens path is actually Dex.exprs . at exprId . Dex.children
, so I also want to be able to access fields further down the path after the "at". But if "at" focuses Nothing, the whole expression should evaluate to Nothing.
I'm also very new to Haskell, so I would appreciate pointers on communicating concepts better/concisely/standardly. I had a hard time writing a title for this post.