Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Add fold1 and consume1. Fix groupBy.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcapriotti committed Feb 25, 2012
1 parent 90671e5 commit 1e99195
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions pipes-extra/Control/Pipe/Combinators.hs
Expand Up @@ -4,7 +4,9 @@ module Control.Pipe.Combinators (
fromList, fromList,
nullP, nullP,
fold, fold,
fold1,
consume, consume,
consume1,
take, take,
drop, drop,
pipeList, pipeList,
Expand All @@ -17,6 +19,7 @@ module Control.Pipe.Combinators (
feed, feed,
) where ) where


import Control.Applicative
import Control.Monad import Control.Monad
import Control.Monad.Free import Control.Monad.Free
import Control.Pipe import Control.Pipe
Expand All @@ -43,9 +46,18 @@ fold f = go
where where
go x = tryAwait >>= maybe (return x) (go . f x) go x = tryAwait >>= maybe (return x) (go . f x)


-- | A variation of 'fold' without an initial value for the accumulator. This
-- pipe doesn't return any value if no input values are received.
fold1 :: Monad m => (a -> a -> a) -> Pipe a x m a
fold1 f = tryAwait >>= maybe discard (fold f)

-- | Accumulate all input values into a list. -- | Accumulate all input values into a list.
consume :: Monad m => Pipe a x m [a] consume :: Monad m => Pipe a x m [a]
consume = fold (\xs x -> xs . (x:)) id >>= \xs -> return (xs []) consume = pipe (:) >+> (fold (.) id <*> pure [])

-- | Accumulate all input values into a non-empty list.
consume1 :: Monad m => Pipe a x m [a]
consume1 = pipe (:) >+> (fold1 (.) <*> pure [])


-- | Act as an identity for the first 'n' values, then terminate. -- | Act as an identity for the first 'n' values, then terminate.
take :: Monad m => Int -> Pipe a a m () take :: Monad m => Int -> Pipe a a m ()
Expand Down Expand Up @@ -95,10 +107,7 @@ groupBy p = streaks >+> createGroups
createGroups = forever $ createGroups = forever $
takeWhile_ isJust >+> takeWhile_ isJust >+>
pipe fromJust >+> pipe fromJust >+>
(consume >>= yieldNonNull) (consume1 >>= yield)
yieldNonNull xs
| null xs = return ()
| otherwise = yield xs


-- | Remove values from the stream that don't satisfy the given predicate. -- | Remove values from the stream that don't satisfy the given predicate.
filter :: Monad m => (a -> Bool) -> Pipe a a m r filter :: Monad m => (a -> Bool) -> Pipe a a m r
Expand Down
2 changes: 1 addition & 1 deletion pipes-extra/Tests/tests.hs
Expand Up @@ -93,7 +93,7 @@ main = defaultMain $ [
, testProperty "pipeList == concatMap" prop_pipeList , testProperty "pipeList == concatMap" prop_pipeList
, testProperty "takeWhile" prop_takeWhile , testProperty "takeWhile" prop_takeWhile
, testProperty "dropWhile" prop_dropWhile , testProperty "dropWhile" prop_dropWhile
-- , testProperty "groupBy" prop_groupBy , testProperty "groupBy" prop_groupBy
, testProperty "filter" prop_filter , testProperty "filter" prop_filter
] ]
] ]

0 comments on commit 1e99195

Please sign in to comment.