on master help sign in
Add uniq, indexed, indexedR.
commit5fc469ad7774651deef3fdc9a727d39cca349c67 authorPranay Sashank <pranaysashank@gmail.com> dateWed 21 Nov 2018, 14:03 (8 years ago) parent5bb0a49
3 files changed+75−1
Changelog.md+5−1 src/Streamly/Prelude.hs+22 src/Streamly/Streams/StreamD.hs+48
Changelog.md+5−1
@@ -2,7 +2,11 @@
22
33* Add benchmarks to measure composed and iterated operations
44* Performance improvements
5−* Add `(!!)`, `insertBy`, `mergeBy`, `merge`, `splitAt`, `the` to Prelude.
5+* Add `(!!)`, `insertBy`, `mergeBy`, `merge`, `splitAt`, `the`, `maximumBy`,
6+ `minimumBy`, `scanl1`, `scanl1M`, `scanl1'`, `scanl1M'`, `scanl`, `scanlM`,
7+ `deleteBy`, `isPrefixOf`, `isSubsequenceOf`, `stripPrefix`,
8+ `replicate`, `generateM`, `generate`, `concatMap`, `concatMapM`,
9+ `findM`, `uniq`, `indexed`, `indexedR` to Prelude.
610
711## 0.5.2
812
src/Streamly/Prelude.hs+22
@@ -204,6 +204,7 @@ module Streamly.Prelude
204204 -- ** Filtering
205205 , filter
206206 , filterM
207+ , uniq
207208 , take
208209 , takeWhile
209210 , takeWhileM
@@ -235,6 +236,8 @@ module Streamly.Prelude
235236 , elemIndices
236237
237238 -- * Zipping
239+ , indexed
240+ , indexedR
238241 , zipWith
239242 , zipWithM
240243 , Z.zipAsyncWith
@@ -993,6 +996,12 @@ filter = K.filter
993996filterM :: (IsStream t, Monad m) => (a -> m Bool) -> t m a -> t m a
994997filterM p m = fromStreamD $ D.filterM p $ toStreamD m
995998
999+-- | Drop repeated elements that are adjacent to each other.
1000+--
1001+{-# INLINE uniq #-}
1002+uniq :: (Eq a, IsStream t, Monad m) => t m a -> t m a
1003+uniq = fromStreamD . D.uniq . toStreamD
1004+
9961005-- | Take first 'n' elements from the stream and discard the rest.
9971006--
9981007-- @since 0.1.0
@@ -1170,6 +1179,19 @@ deleteBy cmp x m = fromStreamS $ S.deleteBy cmp x (toStreamS m)
11701179-- Zipping
11711180------------------------------------------------------------------------------
11721181
1182+-- | Pair each element in a stream with its index.
1183+--
1184+{-# INLINE indexed #-}
1185+indexed :: (IsStream t, Monad m) => t m a -> t m (Int, a)
1186+indexed = fromStreamD . D.indexed . toStreamD
1187+
1188+-- | Pair each element in a stream with its index, starting from the
1189+-- given index @n@ and counting down.
1190+--
1191+{-# INLINE indexedR #-}
1192+indexedR :: (IsStream t, Monad m) => Int -> t m a -> t m (Int, a)
1193+indexedR n = fromStreamD . D.indexedR n . toStreamD
1194+
11731195-- | Zip two streams serially using a monadic zipping function.
11741196--
11751197-- @since 0.4.0
src/Streamly/Streams/StreamD.hs+48
@@ -123,6 +123,7 @@ module Streamly.Streams.StreamD
123123 -- * Filtering
124124 , filter
125125 , filterM
126+ , uniq
126127 , take
127128 , takeWhile
128129 , takeWhileM
@@ -146,6 +147,8 @@ module Streamly.Streams.StreamD
146147 , mapMaybeM
147148
148149 -- * Zipping
150+ , indexed
151+ , indexedR
149152 , zipWith
150153 , zipWithM
151154
@@ -954,6 +957,25 @@ filterM f (Stream step state) = Stream step' state
954957filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
955958filter f = filterM (return . f)
956959
960+{-# INLINE_NORMAL uniq #-}
961+uniq :: (Eq a, Monad m) => Stream m a -> Stream m a
962+uniq (Stream step state) = Stream step' (Nothing, state)
963+ where
964+ {-# INLINE_LATE step' #-}
965+ step' gst (Nothing, st) = do
966+ r <- step (rstState gst) st
967+ case r of
968+ Yield x s -> return $ Yield x (Just x, s)
969+ Skip s -> return $ Skip (Nothing, s)
970+ Stop -> return Stop
971+ step' gst (Just x, st) = do
972+ r <- step (rstState gst) st
973+ case r of
974+ Yield y s | x == y -> return $ Skip (Just x, s)
975+ | otherwise -> return $ Yield x (Just y, s)
976+ Skip s -> return $ Skip (Just x, s)
977+ Stop -> return Stop
978+
957979------------------------------------------------------------------------------
958980-- Transformation by Mapping
959981------------------------------------------------------------------------------
@@ -1055,6 +1077,32 @@ mapMaybeM f = fmap fromJust . filter isJust . mapM f
10551077-- Zipping
10561078------------------------------------------------------------------------------
10571079
1080+{-# INLINE_NORMAL indexed #-}
1081+indexed :: Monad m => Stream m a -> Stream m (Int, a)
1082+indexed (Stream step state) = Stream step' (state, 0)
1083+ where
1084+ {-# INLINE_LATE step' #-}
1085+ step' gst (st, i) = i `seq` do
1086+ r <- step (rstState gst) st
1087+ case r of
1088+ Yield x s -> return $ Yield (i, x) (s, i+1)
1089+ Skip s -> return $ Skip (s, i)
1090+ Stop -> return Stop
1091+
1092+{-# INLINE_NORMAL indexedR #-}
1093+indexedR :: Monad m => Int -> Stream m a -> Stream m (Int, a)
1094+indexedR m (Stream step state) = Stream step' (state, m)
1095+ where
1096+ {-# INLINE_LATE step' #-}
1097+ step' gst (st, i) = i `seq` do
1098+ r <- step (rstState gst) st
1099+ case r of
1100+ Yield x s -> let i' = i - 1
1101+ in
1102+ return $ Yield (i', x) (s, i')
1103+ Skip s -> return $ Skip (s, i)
1104+ Stop -> return Stop
1105+
10581106{-# INLINE_NORMAL zipWithM #-}
10591107zipWithM :: Monad m
10601108 => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c