@@ -280,6 +280,7 @@ module Streamly.Internal.Data.Fold
284 -- * Deprecated
285 -- * Deprecated
285 , sequence
286 , sequence
@@ -335,7 +336,7 @@ import Streamly.Internal.Data.Fold.Type
335-- >>> :set -package streamly
336-- >>> :set -package streamly
336-- >>> import Prelude hiding (break, map, span, splitAt)
337-- >>> import Prelude hiding (break, map, span, splitAt)
337-- >>> import qualified Streamly.Prelude as Stream
338-- >>> import qualified Streamly.Prelude as Stream
338−-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (parse, foldMany)
339+-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (catMaybes, parse, foldMany)
339-- >>> import qualified Streamly.Data.Fold as Fold
340-- >>> import qualified Streamly.Data.Fold as Fold
340-- >>> import qualified Streamly.Internal.Data.Fold as Fold
341-- >>> import qualified Streamly.Internal.Data.Fold as Fold
341-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
342-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
@@ -2562,3 +2563,26 @@ top = topBy $ flip compare
2562{-# INLINE bottom #-}
2563{-# INLINE bottom #-}
2563bottom :: (MonadIO m, Storable a, Ord a) => Int -> Fold m a (MA.Array a)
2564bottom :: (MonadIO m, Storable a, Ord a) => Int -> Fold m a (MA.Array a)
2564bottom = topBy compare
2565bottom = topBy compare
2567+-- | Delete the duplicate elements from the stream while scanning.
2568+-- It returns (Just a) for new element and Nothing for duplicates ones.
2570+-- >>> stream = Stream.fromList [1::Int, 1, 2, 3, 4, 4, 5, 1, 5, 7]
2571+-- >>> Stream.toList $ Stream.catMaybes $ Stream.postscan Fold.nub stream
2576+nub :: (MonadIO m, Ord a) => Fold m a (Maybe a)
2577+nub = Fold step initial extract
2581+ initial = return $ Partial (Set.empty, Nothing)
2583+ step (set, _) x = do
2584+ if Set.member x set
2585+ then return $ Partial (set, Nothing)
2586+ else return $ Partial (Set.insert x set, Just x)
2588+ extract = return . snd