on master help sign in
Add Prelude#init
commite6c75f6aa92db1dc003ddadf94aa2d8dbc204015 authorVeladus <veladus@web.de> dateMon 23 Jul 2018, 16:51 (8 years ago) parent2af3833
3 files changed+28−2
src/Streamly/Prelude.hs+7−1 src/Streamly/Streams/StreamK.hs+16−1 test/Prop.hs+5
src/Streamly/Prelude.hs+7−1
@@ -93,6 +93,7 @@ module Streamly.Prelude
9393 , head
9494 , tail
9595 , last
96+ , init
9697 , elem
9798 , notElem
9899 , length
@@ -160,7 +161,7 @@ import Prelude
160161 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
161162 foldl, map, mapM, mapM_, sequence, all, any, sum, product, elem,
162163 notElem, maximum, minimum, head, last, tail, length, null,
163− reverse, iterate)
164+ reverse, iterate, init)
164165import qualified Prelude
165166import qualified System.IO as IO
166167
@@ -517,6 +518,11 @@ head m = K.head m
517518tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
518519tail m = K.tail (K.adapt m)
519520
521+-- | Extract all but the last element of the stream, if any.
522+{-# INLINE init #-}
523+init :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
524+init m = K.init (K.adapt m)
525+
520526-- | Extract the last element of the stream, if any.
521527--
522528-- @since 0.1.1
src/Streamly/Streams/StreamK.hs+16−1
@@ -81,6 +81,7 @@ module Streamly.Streams.StreamK
8181 , null
8282 , head
8383 , tail
84+ , init
8485 , elem
8586 , notElem
8687 , all
@@ -141,7 +142,7 @@ import Data.Semigroup (Semigroup(..))
141142import Prelude
142143 hiding (foldl, foldr, last, map, mapM, mapM_, repeat, sequence,
143144 take, filter, all, any, takeWhile, drop, dropWhile, minimum,
144− maximum, elem, notElem, null, head, tail, zipWith)
145+ maximum, elem, notElem, null, head, tail, init, zipWith)
145146import qualified Prelude
146147
147148import Streamly.SVar
@@ -570,6 +571,20 @@ tail m =
570571 yieldk _ r = return $ Just $ fromStream r
571572 in unStream (toStream m) defState stop single yieldk
572573
574+{-# INLINE init #-}
575+init :: (IsStream t, Monad m) => t m a -> m (Maybe (t m a))
576+init m = go1 (toStream m)
577+ where
578+ go1 m1 = do
579+ r <- uncons m1
580+ case r of
581+ Nothing -> return Nothing
582+ Just (h, t) -> return . Just . fromStream $ go h t
583+ go p m1 = Stream $ \_ stp sng yld ->
584+ let single _ = sng p
585+ yieldk a x = yld p $ go a x
586+ in unStream m1 defState stp single yieldk
587+
573588{-# INLINE elem #-}
574589elem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
575590elem e m = go (toStream m)
test/Prop.hs+5
@@ -496,6 +496,11 @@ serialEliminationOps constr desc t = do
496496 Nothing -> return Nothing
497497 Just s -> S.toList s >>= return . Just
498498 prop (desc ++ " last") $ eliminateOp constr (wrapMaybe last) $ S.last . t
499+ prop (desc ++ " init") $ eliminateOp constr (wrapMaybe init) $ \x -> do
500+ r <- S.init (t x)
501+ case r of
502+ Nothing -> return Nothing
503+ Just s -> S.toList s >>= return . Just
499504
500505transformOpsWord8
501506 :: ([Word8] -> t IO Word8)