@@ -107,6 +107,9 @@ module Reflex.Class
108 , headTailE
108 , headTailE
109 , takeWhileE
109 , takeWhileE
112+ , takeDropWhileJustE
110 , switcher
113 , switcher
111 -- * Debugging functions
114 -- * Debugging functions
112 , traceEvent
115 , traceEvent
@@ -566,19 +569,84 @@ headTailE e = do
566 be <- hold never $ fmap (const e) eHead
569 be <- hold never $ fmap (const e) eHead
567 return (eHead, switch be)
570 return (eHead, switch be)
572+-- | Take the streak of occurrences starting at the current time for which the
573+-- event returns 'True'.
569−-- | Starting at the current time, fire all the occurrences of the 'Event' for
575+-- Starting at the current time, fire all the occurrences of the 'Event' for
570−-- which the given predicate returns 'True'. When 'False' is returned, do not
576+-- which the given predicate returns 'True'. When 'False' first is returned,
571−-- fire, and permanently stop firing, even if 'True' values are encountered
577+-- do not fire, and permanently stop firing, even if 'True' values would have
572−-- later.
578+-- been encountered later.
573−takeWhileE :: forall t m a. (Reflex t, MonadFix m, MonadHold t m) => (a -> Bool) -> Event t a -> m (Event t a)
581+ . (Reflex t, MonadFix m, MonadHold t m)
574−takeWhileE f e = do
582+ => (a -> Bool)
585+takeWhileE f e = takeWhileJustE (\v -> guard (f v) $> v) e
587+-- | Take the streak of occurrences starting at the current time for which the
588+-- event returns 'Just b'.
590+-- Starting at the current time, fire all the occurrences of the 'Event' for
591+-- which the given predicate returns 'Just b'. When 'Nothing' first is returned,
592+-- do not fire, and permanently stop firing, even if 'Just b' values would have
593+-- been encountered later.
596+ . (Reflex t, MonadFix m, MonadHold t m)
600+takeWhileJustE f e = do
575− rec let (eFalse, eTrue) = fanEither $ ffor e' $ \a -> if f a
601+ rec let (eBad, eTrue) = fanEither $ ffor e' $ \a -> case f a of
577− else Left never
602+ Nothing -> Left never
578− be <- hold e eFalse
603+ Just b -> Right b
579− let e' = switch be
604+ eFirstBad <- headE eBad
605+ e' <- switchHold e eFirstBad
580 return eTrue
606 return eTrue
608+-- | Drop the streak of occurrences starting at the current time for which the
609+-- event returns 'True'.
611+-- Starting at the current time, do not fire all the occurrences of the 'Event'
612+-- for which the given predicate returns 'True'. When 'False' is first
613+-- returned, do fire, and permanently continue firing, even if 'True' values
614+-- would have been encountered later.
617+ . (Reflex t, MonadFix m, MonadHold t m)
621+dropWhileE f e = snd <$> takeDropWhileJustE (\v -> guard (f v) $> v) e
623+-- | Both take and drop the streak of occurrences starting at the current time
624+-- for which the event returns 'Just b'.
626+-- For the left event, starting at the current time, fire all the occurrences
627+-- of the 'Event' for which the given function returns 'Just b'. When
628+-- 'Nothing' is returned, do not fire, and permanently stop firing, even if
629+-- 'Just b' values would have been encountered later.
631+-- For the right event, do not fire until the first occurrence where the given
632+-- function returns 'Nothing', and fire that one and all subsequent
633+-- occurrences. Even if the function would have again returned 'Just b', keep
637+ . (Reflex t, MonadFix m, MonadHold t m)
640+ -> m (Event t b, Event t a)
641+takeDropWhileJustE f e = do
642+ rec let (eBad, eGood) = fanEither $ ffor e' $ \a -> case f a of
645+ eFirstBad <- headE eBad
646+ e' <- switchHold e (never <$ eFirstBad)
647+ eRest <- switchHoldPromptOnly never (e <$ eFirstBad)
648+ return (eGood, eRest)
582-- | Split the supplied 'Event' into two individual 'Event's occurring at the
650-- | Split the supplied 'Event' into two individual 'Event's occurring at the
583-- same time with the respective values from the tuple.
651-- same time with the respective values from the tuple.
584splitE :: Reflex t => Event t (a, b) -> (Event t a, Event t b)
652splitE :: Reflex t => Event t (a, b) -> (Event t a, Event t b)