How to modify using a monadic function with lenses?

前端 未结 1 335
暗喜
暗喜 2021-01-02 00:16

I needed a lens function that works like over, but with monadic operations:

overM :: (Monad m) => Lens s t a b -> (a -> m b) -         


        
相关标签:
1条回答
  • 2021-01-02 01:08

    in Control.Lens.Traversal:

    traverseOf :: Over p f s t a b -> p a (f b) -> s -> f t
    traverseOf = id
    
    mapMOf :: Profunctor p =>
         Over p (WrappedMonad m) s t a b -> p a (m b) -> s -> m t
    mapMOf l cmd = unwrapMonad #. l (WrapMonad #. cmd)
    

    Example:

    Prelude Control.Lens> traverseOf _1 (Just . (+2)) (2,2)
    Just (4,2)
    
    Prelude Control.Lens> mapMOf _1 (Just . (+2)) (2,2)
    Just (4,2)
    
    0 讨论(0)
提交回复
热议问题