@@ -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