@@ -137,8 +137,10 @@ module Reflex.Class
137 -- * Unsafe functions
137 -- * Unsafe functions
138 , unsafeDynamic
138 , unsafeDynamic
139 , unsafeMapIncremental
139 , unsafeMapIncremental
140− -- * 'FunctorMaybe'
140+ -- * 'Filterable' convenience functions
141− , FunctorMaybe (..)
141+ , FunctorMaybe -- fmapMaybe is purposely not exported from deprecated 'FunctorMaybe' and the new alias is exported instead
142+ , mapMaybe -- Re-exported for convenience
142 , fforMaybe
144 , fforMaybe
143 , ffilter
145 , ffilter
144 , filterLeft
146 , filterLeft
@@ -152,6 +154,7 @@ module Reflex.Class
152 , switchPromptOnly
154 , switchPromptOnly
153 -- * "Cheap" functions
155 -- * "Cheap" functions
154 , fmapMaybeCheap
156 , fmapMaybeCheap
155 , fmapCheap
158 , fmapCheap
156 , fforCheap
159 , fforCheap
157 , fforMaybeCheap
160 , fforMaybeCheap
@@ -195,7 +198,10 @@ import qualified Data.Some as Some
195import Data.String
198import Data.String
196import Data.These
199import Data.These
197import Data.Type.Coercion
200import Data.Type.Coercion
201+import Data.Witherable (Filterable(..))
202+import qualified Data.Witherable as W
203+import Reflex.FunctorMaybe (FunctorMaybe)
198−import Reflex.FunctorMaybe
204+import qualified Reflex.FunctorMaybe
199import Reflex.Patch
205import Reflex.Patch
200import qualified Reflex.Patch.MapWithMove as PatchMapWithMove
206import qualified Reflex.Patch.MapWithMove as PatchMapWithMove
@@ -389,8 +395,8 @@ mapAccumMaybeMIncremental f z e = do
389 return $ case result of
395 return $ case result of
390 (Nothing, Nothing) -> Nothing
396 (Nothing, Nothing) -> Nothing
391 _ -> Just result
397 _ -> Just result
392− d' <- holdIncremental z $ fmapMaybe fst e'
398+ d' <- holdIncremental z $ mapMaybe fst e'
393− return (d', fmapMaybe snd e')
399+ return (d', mapMaybe snd e')
395slowHeadE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a)
401slowHeadE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a)
396slowHeadE e = do
402slowHeadE e = do
@@ -400,14 +406,14 @@ slowHeadE e = do
401-- | An 'EventSelector' allows you to efficiently 'select' an 'Event' based on a
407-- | An 'EventSelector' allows you to efficiently 'select' an 'Event' based on a
402-- key. This is much more efficient than filtering for each key with
408-- key. This is much more efficient than filtering for each key with
403−-- 'fmapMaybe'.
409+-- 'mapMaybe'.
404newtype EventSelector t k = EventSelector
410newtype EventSelector t k = EventSelector
405 { -- | Retrieve the 'Event' for the given key. The type of the 'Event' is
411 { -- | Retrieve the 'Event' for the given key. The type of the 'Event' is
406 -- determined by the type of the key, so this can be used to fan-out
412 -- determined by the type of the key, so this can be used to fan-out
407 -- 'Event's whose sub-'Event's have different types.
413 -- 'Event's whose sub-'Event's have different types.
409 -- Using 'EventSelector's and the 'fan' primitive is far more efficient than
415 -- Using 'EventSelector's and the 'fan' primitive is far more efficient than
410− -- (but equivalent to) using 'fmapMaybe' to select only the relevant
416+ -- (but equivalent to) using 'mapMaybe' to select only the relevant
411 -- occurrences of an 'Event'.
417 -- occurrences of an 'Event'.
412 select :: forall a. k a -> Event t a
418 select :: forall a. k a -> Event t a
@@ -551,22 +557,25 @@ instance (Reflex t, Semigroup a) => Semigroup (Behavior t a) where
551 times1p n = fmap $ times1p n
557 times1p n = fmap $ times1p n
560+-- | Alias for 'mapMaybe'
561+fmapMaybe :: Filterable f => (a -> Maybe b) -> f a -> f b
554−-- | Flipped version of 'fmapMaybe'.
564+-- | Flipped version of 'mapMaybe'.
555−fforMaybe :: FunctorMaybe f => f a -> (a -> Maybe b) -> f b
565+fforMaybe :: Filterable f => f a -> (a -> Maybe b) -> f b
556−fforMaybe = flip fmapMaybe
566+fforMaybe = flip mapMaybe
558-- | Filter 'f a' using the provided predicate.
568-- | Filter 'f a' using the provided predicate.
559−-- Relies on 'fforMaybe'.
560−ffilter :: FunctorMaybe f => (a -> Bool) -> f a -> f a
569+ffilter :: Filterable f => (a -> Bool) -> f a -> f a
561−ffilter f = fmapMaybe $ \x -> if f x then Just x else Nothing
570+ffilter = W.filter
563-- | Filter 'Left's from 'f (Either a b)' into 'a'.
572-- | Filter 'Left's from 'f (Either a b)' into 'a'.
564−filterLeft :: FunctorMaybe f => f (Either a b) -> f a
573+filterLeft :: Filterable f => f (Either a b) -> f a
565−filterLeft = fmapMaybe (either Just (const Nothing))
574+filterLeft = mapMaybe (either Just (const Nothing))
567-- | Filter 'Right's from 'f (Either a b)' into 'b'.
576-- | Filter 'Right's from 'f (Either a b)' into 'b'.
568−filterRight :: FunctorMaybe f => f (Either a b) -> f b
577+filterRight :: Filterable f => f (Either a b) -> f b
569−filterRight = fmapMaybe (either (const Nothing) Just)
578+filterRight = mapMaybe (either (const Nothing) Just)
571-- | Left-biased event union (prefers left event on simultaneous
580-- | Left-biased event union (prefers left event on simultaneous
572-- occurrence).
581-- occurrence).
@@ -584,13 +593,18 @@ instance Reflex t => Bind (Event t) where
585instance Reflex t => Functor (Event t) where
594instance Reflex t => Functor (Event t) where
586 {-# INLINE fmap #-}
595 {-# INLINE fmap #-}
587− fmap f = fmapMaybe $ Just . f
596+ fmap f = mapMaybe $ Just . f
588 {-# INLINE (<$) #-}
597 {-# INLINE (<$) #-}
589 x <$ e = fmapCheap (const x) e
598 x <$ e = fmapCheap (const x) e
600+-- TODO Remove this instance
591instance Reflex t => FunctorMaybe (Event t) where
601instance Reflex t => FunctorMaybe (Event t) where
592 {-# INLINE fmapMaybe #-}
602 {-# INLINE fmapMaybe #-}
603+ fmapMaybe = mapMaybe
605+instance Reflex t => Filterable (Event t) where
606+ {-# INLINE mapMaybe #-}
593− fmapMaybe f = push $ return . f
607+ mapMaybe f = push $ return . f
595-- | Never: @'zero' = 'never'@.
609-- | Never: @'zero' = 'never'@.
596instance Reflex t => Plus (Event t) where
610instance Reflex t => Plus (Event t) where
@@ -808,7 +822,7 @@ fanEither :: Reflex t => Event t (Either a b) -> (Event t a, Event t b)
808fanEither e =
822fanEither e =
809 let justLeft = either Just (const Nothing)
823 let justLeft = either Just (const Nothing)
810 justRight = either (const Nothing) Just
824 justRight = either (const Nothing) Just
811− in (fmapMaybe justLeft e, fmapMaybe justRight e)
825+ in (mapMaybe justLeft e, mapMaybe justRight e)
813-- | Split the event into separate events for 'This' and 'That' values,
827-- | Split the event into separate events for 'This' and 'That' values,
814-- allowing them to fire simultaneously when the input value is 'These'.
828-- allowing them to fire simultaneously when the input value is 'These'.
@@ -820,7 +834,7 @@ fanThese e =
820 that (That y) = Just y
834 that (That y) = Just y
821 that (These _ y) = Just y
835 that (These _ y) = Just y
822 that _ = Nothing
836 that _ = Nothing
823− in (fmapMaybe this e, fmapMaybe that e)
837+ in (mapMaybe this e, mapMaybe that e)
825-- | Split the event into an 'EventSelector' that allows efficient selection of
839-- | Split the event into an 'EventSelector' that allows efficient selection of
826-- the individual 'Event's.
840-- the individual 'Event's.
@@ -1004,7 +1018,7 @@ difference = alignEventWithMaybe $ \case
1004-- | Zips two values by taking the union of their shapes and combining with the provided function.
1018-- | Zips two values by taking the union of their shapes and combining with the provided function.
1005-- 'Nothing' values are dropped.
1019-- 'Nothing' values are dropped.
1006alignEventWithMaybe :: Reflex t => (These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
1020alignEventWithMaybe :: Reflex t => (These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
1007−alignEventWithMaybe f ea eb = fmapMaybe (f <=< dmapToThese) $
1021+alignEventWithMaybe f ea eb = mapMaybe (f <=< dmapToThese) $
1008 merge $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
1022 merge $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
1010-- | Produces an 'Event' that fires only when the input event fires with a 'DSum' key that
1024-- | Produces an 'Event' that fires only when the input event fires with a 'DSum' key that
@@ -1174,8 +1188,8 @@ mapAccumMaybeMDyn f z e = do
1174 return $ case result of
1188 return $ case result of
1175 (Nothing, Nothing) -> Nothing
1189 (Nothing, Nothing) -> Nothing
1176 _ -> Just result
1190 _ -> Just result
1177− d' <- holdDyn z $ fmapMaybe fst e'
1191+ d' <- holdDyn z $ mapMaybe fst e'
1178− return (d', fmapMaybe snd e')
1192+ return (d', mapMaybe snd e')
1180-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
1194-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
1181-- with the provided function.
1195-- with the provided function.
@@ -1263,8 +1277,8 @@ mapAccumMaybeMB f z e = do
1263 return $ case result of
1277 return $ case result of
1264 (Nothing, Nothing) -> Nothing
1278 (Nothing, Nothing) -> Nothing
1265 _ -> Just result
1279 _ -> Just result
1266− d' <- hold z $ fmapMaybe fst e'
1280+ d' <- hold z $ mapMaybe fst e'
1267− return (d', fmapMaybe snd e')
1281+ return (d', mapMaybe snd e')
1269-- | Accumulate occurrences of an 'Event', producing an output occurrence each
1283-- | Accumulate occurrences of an 'Event', producing an output occurrence each
1270-- time. Discard the underlying 'Accumulator'.
1284-- time. Discard the underlying 'Accumulator'.
@@ -1413,15 +1427,21 @@ infixl 4 <@
1413pushAlwaysCheap :: Reflex t => (a -> PushM t b) -> Event t a -> Event t b
1427pushAlwaysCheap :: Reflex t => (a -> PushM t b) -> Event t a -> Event t b
1414pushAlwaysCheap f = pushCheap (fmap Just . f)
1428pushAlwaysCheap f = pushCheap (fmap Just . f)
1416−-- | A "cheap" version of 'fmapMaybe'. See the performance note on 'pushCheap'.
1430+-- | A "cheap" version of 'mapMaybe'. See the performance note on 'pushCheap'.
1431+{-# INLINE mapMaybeCheap #-}
1432+mapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
1433+mapMaybeCheap f = pushCheap $ return . f
1435+-- | An alias for 'mapMaybeCheap'
1417{-# INLINE fmapMaybeCheap #-}
1436{-# INLINE fmapMaybeCheap #-}
1418fmapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
1437fmapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
1419−fmapMaybeCheap f = pushCheap $ return . f
1438+fmapMaybeCheap = mapMaybeCheap
1421-- | A "cheap" version of 'fforMaybe'. See the performance note on 'pushCheap'.
1441-- | A "cheap" version of 'fforMaybe'. See the performance note on 'pushCheap'.
1422{-# INLINE fforMaybeCheap #-}
1442{-# INLINE fforMaybeCheap #-}
1423fforMaybeCheap :: Reflex t => Event t a -> (a -> Maybe b) -> Event t b
1443fforMaybeCheap :: Reflex t => Event t a -> (a -> Maybe b) -> Event t b
1424−fforMaybeCheap = flip fmapMaybeCheap
1444+fforMaybeCheap = flip mapMaybeCheap
1426-- | A "cheap" version of 'ffor'. See the performance note on 'pushCheap'.
1446-- | A "cheap" version of 'ffor'. See the performance note on 'pushCheap'.
1427{-# INLINE fforCheap #-}
1447{-# INLINE fforCheap #-}