on master help sign in
Add Prelude#find, Prelude#lookup
commit74ce247d71f0a81f6926193c6913ef786342c09c authorVeladus <veladus@web.de> dateMon 23 Jul 2018, 21:04 (8 years ago) parenta39a35c
3 files changed+42−3
src/Streamly/Prelude.hs+14−1 src/Streamly/Streams/StreamK.hs+25−1 test/Prop.hs+3−1
src/Streamly/Prelude.hs+14−1
@@ -105,6 +105,8 @@ module Streamly.Prelude
105105 , minimum
106106 , sum
107107 , product
108+ , lookup
109+ , find
108110
109111 -- *** Indices
110112 , findIndices
@@ -170,7 +172,7 @@ import Prelude
170172 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
171173 foldl, map, mapM, mapM_, sequence, all, any, sum, product, elem,
172174 notElem, maximum, minimum, head, last, tail, length, null,
173− reverse, iterate, init, and, or)
175+ reverse, iterate, init, and, or, lookup)
174176import qualified Prelude
175177import qualified System.IO as IO
176178
@@ -612,6 +614,17 @@ minimum m = S.minimum (toStreamS m)
612614maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
613615maximum m = S.maximum (toStreamS m)
614616
617+-- | Looks the given key up, treating the given stream as an association list.
618+{-# INLINE lookup #-}
619+lookup :: (Monad m, Eq a) => a -> SerialT m (a, b) -> m (Maybe b)
620+lookup = K.lookup
621+
622+-- | Returns the first element of the stream satisfying the given predicate,
623+-- if any.
624+{-# INLINE find #-}
625+find :: Monad m => (a -> Bool) -> SerialT m a -> m (Maybe a)
626+find = K.find
627+
615628-- | Finds all the indices of elements satisfying the given predicate.
616629{-# INLINE findIndices #-}
617630findIndices :: IsStream t => (a -> Bool) -> t m a -> t m Int
src/Streamly/Streams/StreamK.hs+25−1
@@ -90,6 +90,8 @@ module Streamly.Streams.StreamK
9090 , minimum
9191 , maximum
9292 , findIndices
93+ , lookup
94+ , find
9395
9496 -- ** Map and Fold
9597 , mapM_
@@ -143,7 +145,7 @@ import Data.Semigroup (Semigroup(..))
143145import Prelude
144146 hiding (foldl, foldr, last, map, mapM, mapM_, repeat, sequence,
145147 take, filter, all, any, takeWhile, drop, dropWhile, minimum,
146− maximum, elem, notElem, null, head, tail, init, zipWith)
148+ maximum, elem, notElem, null, head, tail, init, zipWith, lookup)
147149import qualified Prelude
148150
149151import Streamly.SVar
@@ -675,6 +677,28 @@ maximum m = go Nothing (toStream m)
675677 else go (Just res) r
676678 in unStream m1 defState stop single yieldk
677679
680+{-# INLINE lookup #-}
681+lookup :: (IsStream t, Monad m, Eq a) => a -> t m (a, b) -> m (Maybe b)
682+lookup e m = go (toStream m)
683+ where
684+ go m1 =
685+ let single (a, b) | a == e = return $ Just b
686+ | otherwise = return Nothing
687+ yieldk (a, b) x | a == e = return $ Just b
688+ | otherwise = go x
689+ in unStream m1 defState (return Nothing) single yieldk
690+
691+{-# INLINE find #-}
692+find :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m (Maybe a)
693+find p m = go (toStream m)
694+ where
695+ go m1 =
696+ let single a | p a = return $ Just a
697+ | otherwise = return Nothing
698+ yieldk a x | p a = return $ Just a
699+ | otherwise = go x
700+ in unStream m1 defState (return Nothing) single yieldk
701+
678702{-# INLINE findIndices #-}
679703findIndices :: IsStream t => (a -> Bool) -> t m a -> t m Int
680704findIndices p = fromStream . go 0 . toStream
test/Prop.hs+3−1
@@ -10,7 +10,7 @@ import Control.Concurrent (MVar, takeMVar, putMVar, newEmptyMVar)
1010import Control.Monad (replicateM, replicateM_)
1111import Data.IORef (readIORef, modifyIORef, newIORef)
1212import Data.List (sort, foldl', scanl', findIndices, findIndex, elemIndices,
13− elemIndex)
13+ elemIndex, find)
1414import Data.Maybe (mapMaybe)
1515import GHC.Word (Word8)
1616
@@ -489,6 +489,8 @@ eliminationOps constr desc t = do
489489 prop (desc ++ " findIndex") $ eliminateOp constr (findIndex odd) $ (S.findIndex odd) . t
490490 prop (desc ++ " elemIndex") $ eliminateOp constr (elemIndex 3) $ (S.elemIndex 3) . t
491491
492+ prop (desc ++ " find") $ eliminateOp constr (find even) $ (S.find even) . t
493+
492494-- head/tail/last may depend on the order in case of parallel streams
493495-- so we test these only for serial streams.
494496serialEliminationOps