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 婆婆高寿祝福语大全简短