是否有一个标准的组合器将透镜的结果包装在函子中?
Is there a standard combinator packing the result of a lens in functor?
尝试将具有多层仿函数的函数应用于宿主数据结构的成员时,如下例所示:
update ::
(SomeProductField -> Maybe (Int, SomeProductField)) ->
(SomeProduct -> Maybe (Int, SomeProduct))
我想到了以下实用程序:
inFunctor :: Functor f => Lens s t a b -> Lens s (f t) a (f b)
inFunctor lens f = getCompose . lens (Compose . f)
允许我像这样实现 update
功能:
someProductFieldLens :: Lens' SomeProduct SomeProductField
update = inFunctor someProductFieldLens
我想知道像这样的东西是否已经存在于“镜头”中,以及是否可以通过其他方式使用现有手段实现。
也许不是标准方法,但无论如何:alaf
可以处理 Compose
包装。例如:
data SomeProduct = SomeProduct
{ _someProductField :: Bool
, _anotherProductField :: String
}
deriving Show
makeLenses ''SomeProduct
ghci> alaf Compose someProductField (\b -> Just (fromEnum b, not b)) (SomeProduct False "foo")
Just (0,SomeProduct {_someProductField = True, _anotherProductField = "foo"})
如果您仍想定义通用组合器:
inFunctor :: Functor f => Lens s t a b -> Lens s (f t) a (f b)
inFunctor l = alaf Compose l
尝试将具有多层仿函数的函数应用于宿主数据结构的成员时,如下例所示:
update ::
(SomeProductField -> Maybe (Int, SomeProductField)) ->
(SomeProduct -> Maybe (Int, SomeProduct))
我想到了以下实用程序:
inFunctor :: Functor f => Lens s t a b -> Lens s (f t) a (f b)
inFunctor lens f = getCompose . lens (Compose . f)
允许我像这样实现 update
功能:
someProductFieldLens :: Lens' SomeProduct SomeProductField
update = inFunctor someProductFieldLens
我想知道像这样的东西是否已经存在于“镜头”中,以及是否可以通过其他方式使用现有手段实现。
也许不是标准方法,但无论如何:alaf
可以处理 Compose
包装。例如:
data SomeProduct = SomeProduct
{ _someProductField :: Bool
, _anotherProductField :: String
}
deriving Show
makeLenses ''SomeProduct
ghci> alaf Compose someProductField (\b -> Just (fromEnum b, not b)) (SomeProduct False "foo")
Just (0,SomeProduct {_someProductField = True, _anotherProductField = "foo"})
如果您仍想定义通用组合器:
inFunctor :: Functor f => Lens s t a b -> Lens s (f t) a (f b)
inFunctor l = alaf Compose l