Haskell反向折叠结构
示例
借助Dualmonoid,任何折叠都可以沿相反的方向进行,后者会翻转现有的monoid,从而使聚合向后进行。
newtype Dual a = Dual { getDual :: a }
instance Monoid m => Monoid (Dual m) where
mempty = Dual mempty
(Dual x) `mappend` (Dual y) = Dual (y `mappend` x)当foldMap呼叫的基本单边形被翻转时Dual,折线向后运行;在Reverse中定义了以下类型Data.Functor.Reverse:
newtype Reverse t a = Reverse { getReverse :: t a }
instance Foldable t => Foldable (Reverse t) where
foldMap f = getDual . foldMap (Dual . f) . getReverse我们可以使用这种机制reverse为列表写一个简洁的代码:
reverse :: [a] -> [a] reverse = toList . Reverse
热门推荐
10 托班调皮祝福语简短
11 假期愉快蛋糕祝福语简短
12 祝贺新生宝宝祝福语简短
13 订婚红包吉利祝福语简短
14 晚辈读大学祝福语简短
15 美女新人结婚祝福语简短
16 领证祝福语高级文案简短
17 亲人提升的祝福语简短
18 老师祝您订婚祝福语简短