Skip to content

Commit d278dbc

Browse files
authored
Merge pull request #48 from ondrap/master
* Fix performance problem with `S.concat`. * Define a custom `*>`.
2 parents 7b12b30 + 31e59e1 commit d278dbc

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/Streaming/Internal.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,8 @@ instance (Functor f, Monad m) => Functor (Stream f m) where
231231
instance (Functor f, Monad m) => Monad (Stream f m) where
232232
return = Return
233233
{-# INLINE return #-}
234-
stream1 >> stream2 = loop stream1 where
235-
loop stream = case stream of
236-
Return _ -> stream2
237-
Effect m -> Effect (fmap loop m)
238-
Step f -> Step (fmap loop f)
239-
{-# INLINABLE (>>) #-}
234+
(>>) = (*>)
235+
{-# INLINE (>>) #-}
240236
-- (>>=) = _bind
241237
-- {-#INLINE (>>=) #-}
242238
--
@@ -281,6 +277,13 @@ instance (Functor f, Monad m) => Applicative (Stream f m) where
281277
{-# INLINE pure #-}
282278
streamf <*> streamx = do {f <- streamf; x <- streamx; return (f x)}
283279
{-# INLINE (<*>) #-}
280+
stream1 *> stream2 = loop stream1 where
281+
loop stream = case stream of
282+
Return _ -> stream2
283+
Effect m -> Effect (fmap loop m)
284+
Step f -> Step (fmap loop f)
285+
{-# INLINABLE (*>) #-}
286+
284287

285288
{- | The 'Alternative' instance glues streams together stepwise.
286289

src/Streaming/Prelude.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,18 @@ chain f = loop where
557557
-}
558558

559559
concat :: (Monad m, Foldable.Foldable f) => Stream (Of (f a)) m r -> Stream (Of a) m r
560-
concat str = for str each
561-
{-# INLINE concat #-}
560+
concat = loop
561+
where
562+
loop str = case str of
563+
Return r -> Return r
564+
Effect m -> Effect (fmap loop m)
565+
Step (lst :> as) ->
566+
let inner [] = loop as
567+
inner (x:rest) = Step (x :> inner rest)
568+
in inner (Foldable.toList lst)
569+
{-# INLINABLE concat #-}
570+
-- The above hand-written loop is ~20% faster than the 'for' implementation
571+
-- concat str = for str each
562572

563573
{-| The natural @cons@ for a @Stream (Of a)@.
564574

0 commit comments

Comments
 (0)