@@ -10,6 +10,7 @@
1010module StreamDOps where
1111
1212import Control.Monad (when)
13+import Data.Maybe (isJust)
1314import Prelude
1415 (Monad, Int, (+), ($), (.), return, (>), even, (<=),
1516 subtract, undefined, Maybe(..), not, mapM_, (>>=))
@@ -25,7 +26,8 @@ maxValue = value + 1000
2526-------------------------------------------------------------------------------
2627
2728{-# INLINE uncons #-}
28−{-# INLINE nullHeadTail #-}
29+{-# INLINE nullTail #-}
30+{-# INLINE headTail #-}
2931{-# INLINE scan #-}
3032{-# INLINE map #-}
3133{-# INLINE filterEven #-}
@@ -43,7 +45,7 @@ maxValue = value + 1000
4345{-# INLINE composeAllInFilters #-}
4446{-# INLINE composeAllOutFilters #-}
4547{-# INLINE composeMapAllInFilter #-}
46−uncons, nullHeadTail, map, scan, filterEven, filterAllOut,
48+uncons, nullTail, headTail, map, scan, filterEven, filterAllOut,
4749 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
4850 -- concat,
4951 composeAllInFilters, composeAllOutFilters, composeMapAllInFilter
@@ -109,16 +111,27 @@ runStream :: Monad m => Stream m a -> m ()
109111runStream = S.runStream
110112
111113toNull = runStream
114+
112115uncons s = do
113116 r <- S.uncons s
114117 case r of
115118 Nothing -> return ()
116119 Just (_, t) -> uncons t
117−nullHeadTail s = do
120+
121+{-# INLINE tail #-}
122+tail :: Monad m => Stream m a -> m ()
123+tail s = S.tail s >>= mapM_ tail
124+
125+nullTail s = do
118126 r <- S.null s
119127 when (not r) $ do
120− _ <- S.head s
121− S.tail s >>= mapM_ nullHeadTail
128+ S.tail s >>= mapM_ nullTail
129+
130+headTail s = do
131+ h <- S.head s
132+ when (isJust h) $
133+ S.tail s >>= mapM_ headTail
134+
122135toList = S.toList
123136foldl = S.foldl' (+) 0
124137last = S.last
@@ -163,7 +176,7 @@ composeAllInFilters = compose (S.filter (<= maxValue))
163176composeAllOutFilters = compose (S.filter (> maxValue))
164177composeMapAllInFilter = compose (S.filter (<= maxValue) . S.map (subtract 1))
165178
166−{-# INLINABLE composeScaling #-}
179+{-# INLINE composeScaling #-}
167180composeScaling :: Monad m => Int -> Stream m Int -> m ()
168181composeScaling m =
169182 case m of