@@ -97,8 +97,11 @@ module Streamly.Internal.Data.StreamK.Type
97 -- ** Specific Folds
97 -- ** Specific Folds
103 -- * Mapping
106 -- * Mapping
@@ -1811,14 +1814,42 @@ iterateMWith cns step = go
1811 !next <- s
1814 !next <- s
1812 foldStreamShared st stp sng yld (return next `cns` go (step next))
1815 foldStreamShared st stp sng yld (return next `cns` go (step next))
1814−{-# INLINE headPartial #-}
1817+-- | head for non-empty streams, fails for empty stream case.
1819+{-# INLINE headNonEmpty #-}
1815−headPartial :: Monad m => StreamK m a -> m a
1820+headNonEmpty :: Monad m => StreamK m a -> m a
1816−headPartial = foldrM (\x _ -> return x) (error "head of nil")
1821+headNonEmpty = foldrM (\x _ -> return x) (error "headNonEmpty: empty stream")
1823+-- | init for non-empty streams, fails for empty stream case.
1825+-- See also 'init' for a non-partial version of this function..
1826+{-# INLINE initNonEmpty #-}
1827+initNonEmpty :: Stream m a -> Stream m a
1832+ go0 m = mkStream $ \st yld sng stp ->
1833+ let stop = error "initNonEmpty: Empty Stream."
1835+ yieldk a r = foldStream st yld sng stp (go1 a r)
1836+ in foldStream st yieldk single stop m
1838+ go1 a r = mkStream $ \st yld sng stp ->
1841+ yieldk a1 r1 = yld a (go1 a1 r1)
1842+ in foldStream st yieldk single stop r
1818−{-# INLINE tailPartial #-}
1844+-- | tail for non-empty streams, fails for empty stream case.
1846+-- See also 'tail' for a non-partial version of this function..
1848+-- Note: this is same as "drop 1" with error on empty stream.
1849+{-# INLINE tailNonEmpty #-}
1819−tailPartial :: StreamK m a -> StreamK m a
1850+tailNonEmpty :: StreamK m a -> StreamK m a
1820−tailPartial m = mkStream $ \st yld sng stp ->
1851+tailNonEmpty m = mkStream $ \st yld sng stp ->
1821− let stop = error "tail of nil"
1852+ let stop = error "tailNonEmpty: empty stream"
1822 single _ = stp
1853 single _ = stp
1823 yieldk _ r = foldStream st yld sng stp r
1854 yieldk _ r = foldStream st yld sng stp r
1824 in foldStream st yieldk single stop m
1855 in foldStream st yieldk single stop m
@@ -1883,10 +1914,10 @@ mfix f = mkStream $ \st yld sng stp ->
1885 -- fix the head element of the stream
1916 -- fix the head element of the stream
1886− xs = fix (f . headPartial)
1917+ xs = fix (f . headNonEmpty)
1888 -- now fix the tail recursively
1919 -- now fix the tail recursively
1889− ys = mfix (tailPartial . f)
1920+ ys = mfix (tailNonEmpty . f)
1891-------------------------------------------------------------------------------
1922-------------------------------------------------------------------------------
1892-- Conversions
1923-- Conversions
@@ -1917,6 +1948,15 @@ uncons m =
1917 yieldk a r = pure (Just (a, r))
1948 yieldk a r = pure (Just (a, r))
1918 in foldStream defState yieldk single stop m
1949 in foldStream defState yieldk single stop m
1951+-- Note that this is not a StreamK -> StreamK because then we cannot handle the
1952+-- empty stream case without making this a partial function.
1954+-- See tailNonEmpty as well above.
1958+-- >>> tail = fmap (fmap snd) StreamK.uncons
1920{-# INLINE tail #-}
1960{-# INLINE tail #-}
1921tail :: Applicative m => StreamK m a -> m (Maybe (StreamK m a))
1961tail :: Applicative m => StreamK m a -> m (Maybe (StreamK m a))
@@ -1925,9 +1965,14 @@ tail =
1925 yieldk _ r = pure $ Just r
1965 yieldk _ r = pure $ Just r
1926 in foldStream defState yieldk single stop
1966 in foldStream defState yieldk single stop
1928−-- | Extract all but the last element of the stream, if any.
1968+-- Note that this is not a StreamK -> StreamK because then we cannot handle the
1969+-- empty stream case without making this a partial function.
1930−-- Note: This will end up buffering the entire stream.
1971+-- XXX How do we implement unsnoc? Make StreamK a monad and return the
1972+-- remaining stream as a result value in the monad?
1974+-- | Extract all but the last element of the stream, if any. This will end up
1975+-- evaluating the last element as well to find out that it is last.
1932-- /Pre-release/
1977-- /Pre-release/
1933{-# INLINE init #-}
1978{-# INLINE init #-}