Implement "the" as a Fold
commit323d3382d014ab26d55b3050b0447779aa2cc42b
authorRanjeet Kumar Ranjan <ranjeetr@composewell.com>
dateWed 15 Jun 2022, 22:30 (4 years ago)
parentfe597c2
core/src/Streamly/Internal/Data/Fold.hs+20
@@ -2596,3 +2596,23 @@ nub = Fold step initial extract
25962596 else return $ Partial (Set.insert x set, Just x)
25972597
25982598 extract = return . snd
2599+
2600+{-# INLINE the #-}
2601+the :: (Eq a, Monad m) => Fold m a (Maybe a)
2602+the = Fold step initial extract
2603+
2604+ where
2605+
2606+ initial =
2607+ return $ Partial Nothing
2608+
2609+ step Nothing x =
2610+ return $ Partial (Just x)
2611+
2612+ step old@(Just x0) x =
2613+ return $
2614+ if x0 == x
2615+ then Partial old
2616+ else Done Nothing
2617+
2618+ extract = return