Haskell在没有模板Haskell的情况下写镜头
示例
为了揭开Haskell模板的神秘面纱,假设您有
data Example a = Example { _foo :: Int, _bar :: a }然后
makeLenses 'Example
产生(或多或少)
foo :: Lens' (Example a) Int bar :: Lens (Example a) (Example b) a b
但是,没有什么特别神奇的事情发生。您可以自己编写以下内容:
foo :: Lens' (Example a) Int -- :: Functor f => (Int -> f Int) -> (Example a -> f (Example a)) ;; expand the alias foo wrap (Example foo bar) = fmap (\newFoo -> Example newFoo bar) (wrap foo) bar :: Lens (Example a) (Example b) a b -- :: Functor f => (a -> f b) -> (Example a -> f (Example b)) ;; expand the alias bar wrap (Example foo bar) = fmap (\newBar -> Example foo newBar) (wrap bar)
本质上,您想使用该wrap功能“访问”镜头的“焦点”,然后重建“整个”类型。
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短