Haskell Lens:棱镜在可能之上



我有那些镜头:

getB :: Lens' A (Maybe B) 
getC :: Prism' B C

如何从 A 中提取Maybe C? 我能找到的最好的:

case A ^. getB of
Just b -> b ^? getC
Nothing -> Nothing

还有什么更优雅的吗?

_Just :: Prism' (Maybe a) a

_Just棱镜将使您从Maybe中获得价值。

a ^? getB . _Just . getC

或者你可以使用MaybeTraversable实例,如果有的话,它会获取Just中的值。

-- traverse :: Traversal' (Maybe a) a
a ^? getB . traverse . getC

最新更新