on master help sign in
Fix the buggy implementation of last with a foldl version
commitc4412524402c091128ca5cd126d59385a7cfa494 authorAbhiroop Sarkar <asiamgenius@gmail.com> dateTue 13 Mar 2018, 23:19 (9 years ago) parenta7c0640
2 files changed+4−9
Changelog.md+2−1 src/Streamly/Prelude.hs+2−8
Changelog.md+2−1
@@ -5,7 +5,8 @@
55* Improve performance of some stream operations (`foldl`, `dropWhile`)
66
77### 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
910
1011## 0.1.0
1112
src/Streamly/Prelude.hs+2−8
@@ -87,7 +87,6 @@ import qualified System.IO as IO
8787
8888import Streamly.Core
8989import Streamly.Streams
90−
9190------------------------------------------------------------------------------
9291-- Construction
9392------------------------------------------------------------------------------
@@ -334,14 +333,9 @@ head m =
334333 in (runStream (toStream m)) Nothing stop yield
335334
336335-- | Extract the last element of the stream, if any.
336+{-# INLINE last #-}
337337last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
338−last m = go (toStream m)
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
338+last = foldl (\_ y -> Just y) Nothing id
345339
346340-- | Determine whether an element is present in the stream.
347341elem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool