Skip to content

Commit c64d802

Browse files
chessaiandrewthad
authored andcommitted
add foldMap and foldMap_
1 parent dbebbd7 commit c64d802

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/Streaming/Prelude.hs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ module Streaming.Prelude (
172172
, fold_
173173
, foldM
174174
, foldM_
175+
, foldMap
176+
, foldMap_
175177
, all
176178
, all_
177179
, any
@@ -273,7 +275,7 @@ import Prelude hiding (map, mapM, mapM_, filter, drop, dropWhile, take, mconcat
273275
, print, zipWith, zip, zipWith3, zip3, unzip, seq, show, read
274276
, readLn, sequence, concat, span, break, readFile, writeFile
275277
, minimum, maximum, elem, notElem, all, any, head
276-
, last)
278+
, last, foldMap)
277279

278280
import qualified GHC.IO.Exception as G
279281
import qualified System.IO as IO
@@ -1389,9 +1391,18 @@ last<Enter>
13891391
Last {getLast = Just "last"} :> ()
13901392
13911393
-}
1394+
1395+
foldMap :: (Monad m, Monoid w) => (a -> w) -> Stream (Of a) m r -> m (Of w r)
1396+
foldMap f = fold (\acc a -> let !fa = f $! a in mappend acc fa) mempty id
1397+
{-# INLINE foldMap #-}
1398+
1399+
foldMap_ :: (Monad m, Monoid w) => (a -> w) -> Stream (Of a) m r -> m w
1400+
foldMap_ f = fold_ (\acc a -> let !fa = f $! a in mappend acc fa) mempty id
1401+
{-# INLINE foldMap_ #-}
1402+
13921403
mconcat :: (Monad m, Monoid w) => Stream (Of w) m r -> m (Of w r)
13931404
mconcat = fold mappend mempty id
1394-
{-#INLINE mconcat #-}
1405+
{-# INLINE mconcat #-}
13951406

13961407
data Maybe_ a = Just_ !a | Nothing_
13971408
mconcat_ :: (Monad m, Monoid w) => Stream (Of w) m r -> m w
@@ -1401,7 +1412,7 @@ minimum :: (Monad m, Ord a) => Stream (Of a) m r -> m (Of (Maybe a) r)
14011412
minimum = fold (\m a -> case m of Nothing_ -> Just_ a ; Just_ a' -> Just_ (min a a'))
14021413
Nothing_
14031414
(\m -> case m of Nothing_ -> Nothing; Just_ r -> Just r)
1404-
{-#INLINE minimum #-}
1415+
{-# INLINE minimum #-}
14051416

14061417
minimum_ :: (Monad m, Ord a) => Stream (Of a) m r -> m (Maybe a)
14071418
minimum_ = fold_ (\m a -> case m of Nothing_ -> Just_ a ; Just_ a' -> Just_ (min a a'))

0 commit comments

Comments
 (0)