如何在不嵌套 Maybe 的情况下 "zoom" 通过多个 Maybe 级别?
How to "zoom" through multiple levels of Maybe without getting nested Maybes?
让我们
data B = B {_b :: Maybe Int} deriving (Eq, Show, Generic)
makeFieldsNoPrefix ''B
data A = A {_a :: Maybe B} deriving (Eq, Show, Generic)
makeFieldsNoPrefix ''A
现在我想从 A
(Maybe Int
in Maybe B
)“放大”到 b
。我想到了:
let _TEST :: Maybe Int = (A {_a = Just B{_b = Just 7}}) ^. a ^? _Just . b & join
是否可以使用一些镜头运算符直接获得 Maybe Int
而无需使用 join
从 Maybe (Maybe Int)
解压它?
_TEST ^? a . _Just . b . _Just
让我们
data B = B {_b :: Maybe Int} deriving (Eq, Show, Generic)
makeFieldsNoPrefix ''B
data A = A {_a :: Maybe B} deriving (Eq, Show, Generic)
makeFieldsNoPrefix ''A
现在我想从 A
(Maybe Int
in Maybe B
)“放大”到 b
。我想到了:
let _TEST :: Maybe Int = (A {_a = Just B{_b = Just 7}}) ^. a ^? _Just . b & join
是否可以使用一些镜头运算符直接获得 Maybe Int
而无需使用 join
从 Maybe (Maybe Int)
解压它?
_TEST ^? a . _Just . b . _Just