Skip to content

Commit 98737f7

Browse files
chessaiandrewthad
authored andcommitted
don't force function value, but make accumulation strict. add docs to foldMap
1 parent c64d802 commit 98737f7

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

src/Streaming/Prelude.hs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,23 +1383,31 @@ mappedPost :: (Monad m, Functor g) => (forall x . f x -> m (g x)) -> Stream f m
13831383
mappedPost = mapsMPost
13841384
{-# INLINE mappedPost #-}
13851385

1386-
{-| Fold streamed items into their monoidal sum
1386+
{-| Map each element of the stream to a monoid, and take the monoidal sum of the results.
13871387
1388-
>>> S.mconcat $ S.take 2 $ S.map (Data.Monoid.Last . Just) (S.stdinLn)
1389-
first<Enter>
1390-
last<Enter>
1391-
Last {getLast = Just "last"} :> ()
1388+
>>> S.foldMap Sum $ S.take 2 (S.stdinLn)
1389+
1<Enter>
1390+
2<Enter>
1391+
3<Enter>
1392+
Sum {getSum = 6} :> ()
13921393
13931394
-}
1394-
13951395
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
1396+
foldMap f = fold (\ !acc a -> mappend acc (f a)) mempty id
13971397
{-# INLINE foldMap #-}
13981398

13991399
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
1400+
foldMap_ f = fold_ (\ !acc a -> mappend acc (f a)) mempty id
14011401
{-# INLINE foldMap_ #-}
14021402

1403+
{-| Fold streamed items into their monoidal sum
1404+
1405+
>>> S.mconcat $ S.take 2 $ S.map (Data.Monoid.Last . Just) (S.stdinLn)
1406+
first<Enter>
1407+
last<Enter>
1408+
Last {getLast = Just "last"} :> ()
1409+
1410+
-}
14031411
mconcat :: (Monad m, Monoid w) => Stream (Of w) m r -> m (Of w r)
14041412
mconcat = fold mappend mempty id
14051413
{-# INLINE mconcat #-}

0 commit comments

Comments
 (0)