Haskell语言学习笔记(14)Foldable

Foldable

Foldable(可折叠)是个类型类,它是 fold 这个函数一般化的结果。
class Foldable t where
    fold :: Monoid m => t m -> m
    fold = foldMap id
    foldMap :: Monoid m => (a -> m) -> t a -> m
    foldMap f = foldr (mappend . f) mempty
    foldr :: (a -> b -> b) -> b -> t a -> b
    foldr' :: (a -> b -> b) -> b -> t a -> b
    foldl :: (b -> a -> b) -> b -> t a -> b
    foldl' :: (b -> a -> b) -> b -> t a -> b
    foldr1 :: (a -> a -> a) -> t a -> a
    foldl1 :: (a -> a -> a) -> t a -> a
    toList :: t a -> [a]
    null :: t a -> Bool
    length :: t a -> Int
    elem :: Eq a => a -> t a -> Bool
    maximum :: forall a . Ord a => t a -> a
    minimum :: forall a . Ord a => t a -> a
    sum :: Num a => t a -> a
    product :: Num a => t a -> a

Foldable Tree

import qualified Data.Foldable as F
import Data.Monoid

data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)

instance F.Foldable Tree where
    foldMap f Empty = mempty
    foldMap f (Node x l r) = F.foldMap f l <>
                             f x           <>
                             F.foldMap f r
                             
testTree = Node 5  
            (Node 3  
                (Node 1 Empty Empty)  
                (Node 6 Empty Empty)  
            )  
            (Node 9  
                (Node 8 Empty Empty)  
                (Node 10 Empty Empty)  
            )

main = do
    -- print $ F.foldl (+) 0 testTree
    print $ F.sum testTree
    -- print $ F.foldl (*) 1 testTree
    print $ F.product testTree
    print $ getAny $ F.foldMap (\x -> Any $ x == 3) testTree
    print $ getAny $ F.foldMap (\x -> Any $ x > 15) testTree
    -- print $ F.foldMap (\x -> [x]) testTree
    print $ F.toList testTree
    print $ F.length testTree
    print $ F.maximum testTree
    print $ F.minimum testTree
    print $ 3 `F.elem` testTree
    print $ F.null testTree
      
{-
42
64800
True
False
[1,3,6,5,8,9,10]
7
10
1
True
False
-}

traverse_ for_ mapM_ forM_ sequenceA_ sequence_ 函数

traverse_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f ()
traverse_ f = foldr ((*>) . f) (pure ())
for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f ()
for_ = flip traverse_
mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()
mapM_ f= foldr ((>>) . f) (return ())
forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m ()
forM_ = flip mapM_
sequenceA_ :: (Foldable t, Applicative f) => t (f a) -> f ()
sequenceA_ = foldr (*>) (pure ())
sequence_ :: (Foldable t, Monad m) => t (m a) -> m ()
sequence_ = foldr (>>) (return ())
Prelude> mapM_ putStrLn [show x ++ "*" ++ show y ++ "=" ++ show (x*y) | x <- [1..9], y <- [1..9]]
1*1=1
1*2=2
...
...
9*8=72
9*9=81
Prelude Data.Foldable> forM_ [1,2,3,4] print
1
2
3
4
Prelude Data.Foldable> sequence_ [print 3, print 4]
3
4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值