on master help sign in
Merge pull request #25 from Abhiroop/last
f5a21bcHarendra Kumar
Changelog.md+2−1
@@ -5,7 +5,8 @@
5* Improve performance of some stream operations (`foldl`, `dropWhile`)
5* Improve performance of some stream operations (`foldl`, `dropWhile`)
6
6
7### Bug Fixes
7### Bug Fixes
8−* Fix the `product` operation. Earlier, it always returned 0 due to a bug.
8+* Fix the `product` operation. Earlier, it always returned 0 due to a bug
9+* Fix the `last` operation, which returned `Nothing` for singleton streams
9
10
10## 0.1.0
11## 0.1.0
11
12
src/Streamly/Prelude.hs+2−8
@@ -87,7 +87,6 @@ import qualified System.IO as IO
87
87
88import Streamly.Core
88import Streamly.Core
89import Streamly.Streams
89import Streamly.Streams
90−
91------------------------------------------------------------------------------
90------------------------------------------------------------------------------
92-- Construction
91-- Construction
93------------------------------------------------------------------------------
92------------------------------------------------------------------------------
@@ -334,14 +333,9 @@ head m =
334 in (runStream (toStream m)) Nothing stop yield
333 in (runStream (toStream m)) Nothing stop yield
335
334
336-- | Extract the last element of the stream, if any.
335-- | Extract the last element of the stream, if any.
336+{-# INLINE last #-}
337last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
337last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
338−last m = go (toStream m)
338+last = foldl (\_ y -> Just y) Nothing id
339− where
340− go m1 =
341− let stop = return Nothing
342− yield a Nothing = return (Just a)
343− yield _ (Just x) = go x
344− in (runStream m1) Nothing stop yield
345
339
346-- | Determine whether an element is present in the stream.
340-- | Determine whether an element is present in the stream.
347elem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool
341elem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool