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 祝福语驱散霉运的话简短