Current section
Files
Jump to
Current section
Files
lib/Data/Foldable.hm
-----------------------------------------------------------------------------
-- |
-- Module : Data.Foldable
-- Copyright : (c) 2020-2021 EMQ Technologies Co., Ltd.
-- License : BSD-style (see the LICENSE file)
--
-- Maintainer : Feng Lee, feng@emqx.io
-- Yang M, yangm@emqx.io
-- Stability : experimental
-- Portability : portable
--
-- The Foldable typeclass.
--
-----------------------------------------------------------------------------
module Data.Foldable
( class Foldable
, foldl, foldr, foldMap
, module Data.Monoid
) where
import Data.Monoid (class Monoid, mempty)
import Data.Semigroup ((<>))
class Foldable f where
foldl :: forall a b. (b -> a -> b) -> b -> f a -> b
foldr :: forall a b. (a -> b -> b) -> b -> f a -> b
foldMap :: forall a m. Monoid m => (a -> m) -> f a -> m
instance Foldable List where
foldl = foldlListImpl
foldr = foldrListImpl
foldMap = foldMapDefaultR
foreign import foldlListImpl :: forall a b. (b -> a -> b) -> b -> [a] -> b
foreign import foldrListImpl :: forall a b. (a -> b -> b) -> b -> [a] -> b
foldMapDefaultR
:: forall f a m
. Foldable f
=> Monoid m
=> (a -> m)
-> f a
-> m
foldMapDefaultR f = foldr (\x acc -> f x <> acc) mempty