haskell - How to "zoom" through multiple levels of Maybe without getting nested Maybes?
问题描述
Let's have
data B = B {_b :: Maybe Int} deriving (Eq, Show, Generic)
makeFieldsNoPrefix ''B
data A = A {_a :: Maybe B} deriving (Eq, Show, Generic)
makeFieldsNoPrefix ''A
and now I want to "zoom" onto b
from A
(Maybe Int
in Maybe B
). I came up with:
let _TEST :: Maybe Int = (A {_a = Just B{_b = Just 7}}) ^. a ^? _Just . b & join
Is it possible to directly get Maybe Int
using some lens operator without unpacking it with join
from Maybe (Maybe Int)
?
解决方案
_TEST ^? a . _Just . b . _Just
推荐阅读
- html - bulma 导航栏菜单默认在移动设备上打开
- javascript - 如何使用 xpath 获得硒中的非罢工价格金额?
- sql-server - 如何在 SSRS 中获取连接查询的列值?
- jquery - 如何在 Post 方法中获取模型内动态生成的复选框的值
- jakarta-ee - 如何在 thorntail 2.4 中将配置文件设置为默认值
- javascript - 在 Internet Explorer 中如果元素没有 tabIndex,它将返回 0 作为默认值
- postgresql - PostgresSql 在执行时抛出语法错误
- c# - 为什么我不能将 DataAnnotations KeyAttribute 用于复合键?
- c# - 如何在运行时从字符串变量执行/运行代码
- php - 如何在 [Symfony] 中使用 google 制作 CalendarChart