15

我需要一个可以像 一样工作的镜头功能over,但具有单子操作:

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

虽然这个函数很容易定义(它实际上只是一个恒等模WrappedMonad),但我想知道这些函数是否定义在lens的某个地方?

{-# LANGUAGE RankNTypes #-}
import Control.Applicative
import Control.Lens

overF :: (Functor f) => Lens s t a b -> (a -> f b) -> (s -> f t)
overF l = l

overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -> m t)
overM l = (unwrapMonad .) . l . (WrapMonad .)
4

1 回答 1

8

在 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)

例子:

Prelude Control.Lens> traverseOf _1 (Just . (+2)) (2,2)
Just (4,2)

Prelude Control.Lens> mapMOf _1 (Just . (+2)) (2,2)
Just (4,2)
于 2014-03-05T22:34:09.340 回答