@@ -133,8 +133,9 @@ module Reflex.Class
133133 -- * Unsafe functions
134134 , unsafeDynamic
135135 , unsafeMapIncremental
136− -- * 'FunctorMaybe'
137− , FunctorMaybe (..)
136+ -- * 'Filterable' convenience functions
137+ , FunctorMaybe -- fmapMaybe is purposely not exported from deprecated 'FunctorMaybe' and the new alias is exported instead
138+ , fmapMaybe
138139 , fforMaybe
139140 , ffilter
140141 , filterLeft
@@ -147,6 +148,7 @@ module Reflex.Class
147148 , appendEvents
148149 , onceE
149150 , sequenceThese
151+ , mapMaybeCheap
150152 , fmapMaybeCheap
151153 , fmapCheap
152154 , fforCheap
@@ -193,7 +195,10 @@ import qualified Data.Some as Some
193195import Data.String
194196import Data.These
195197import Data.Type.Coercion
196−import Reflex.FunctorMaybe
198+import Data.Witherable (Filterable(..))
199+import qualified Data.Witherable as W
200+import Reflex.FunctorMaybe (FunctorMaybe)
201+import qualified Reflex.FunctorMaybe
197202import Reflex.Patch
198203import qualified Reflex.Patch.MapWithMove as PatchMapWithMove
199204
@@ -388,8 +393,8 @@ mapAccumMaybeMIncremental f z e = do
388393 return $ case result of
389394 (Nothing, Nothing) -> Nothing
390395 _ -> Just result
391− d' <- holdIncremental z $ fmapMaybe fst e'
392− return (d', fmapMaybe snd e')
396+ d' <- holdIncremental z $ mapMaybe fst e'
397+ return (d', mapMaybe snd e')
393398
394399slowHeadE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a)
395400slowHeadE e = do
@@ -399,14 +404,14 @@ slowHeadE e = do
399404
400405-- | An 'EventSelector' allows you to efficiently 'select' an 'Event' based on a
401406-- key. This is much more efficient than filtering for each key with
402−-- 'fmapMaybe'.
407+-- 'mapMaybe'.
403408newtype EventSelector t k = EventSelector
404409 { -- | Retrieve the 'Event' for the given key. The type of the 'Event' is
405410 -- determined by the type of the key, so this can be used to fan-out
406411 -- 'Event's whose sub-'Event's have different types.
407412 --
408413 -- Using 'EventSelector's and the 'fan' primitive is far more efficient than
409− -- (but equivalent to) using 'fmapMaybe' to select only the relevant
414+ -- (but equivalent to) using 'mapMaybe' to select only the relevant
410415 -- occurrences of an 'Event'.
411416 select :: forall a. k a -> Event t a
412417 }
@@ -550,22 +555,25 @@ instance (Reflex t, Semigroup a) => Semigroup (Behavior t a) where
550555 times1p n = fmap $ times1p n
551556#endif
552557
553−-- | Flipped version of 'fmapMaybe'.
554−fforMaybe :: FunctorMaybe f => f a -> (a -> Maybe b) -> f b
555−fforMaybe = flip fmapMaybe
558+-- | Alias for 'mapMaybe'
559+fmapMaybe :: Filterable f => (a -> Maybe b) -> f a -> f b
560+fmapMaybe = mapMaybe
561+
562+-- | Flipped version of 'mapMaybe'.
563+fforMaybe :: Filterable f => f a -> (a -> Maybe b) -> f b
564+fforMaybe = flip mapMaybe
556565
557566-- | Filter 'f a' using the provided predicate.
558−-- Relies on 'fforMaybe'.
559−ffilter :: FunctorMaybe f => (a -> Bool) -> f a -> f a
560−ffilter f = fmapMaybe $ \x -> if f x then Just x else Nothing
567+ffilter :: Filterable f => (a -> Bool) -> f a -> f a
568+ffilter = W.filter
561569
562570-- | Filter 'Left's from 'f (Either a b)' into 'a'.
563−filterLeft :: FunctorMaybe f => f (Either a b) -> f a
564−filterLeft = fmapMaybe (either Just (const Nothing))
571+filterLeft :: Filterable f => f (Either a b) -> f a
572+filterLeft = mapMaybe (either Just (const Nothing))
565573
566574-- | Filter 'Right's from 'f (Either a b)' into 'b'.
567−filterRight :: FunctorMaybe f => f (Either a b) -> f b
568−filterRight = fmapMaybe (either (const Nothing) Just)
575+filterRight :: Filterable f => f (Either a b) -> f b
576+filterRight = mapMaybe (either (const Nothing) Just)
569577
570578-- | Left-biased event union (prefers left event on simultaneous
571579-- occurrence).
@@ -583,13 +591,18 @@ instance Reflex t => Bind (Event t) where
583591
584592instance Reflex t => Functor (Event t) where
585593 {-# INLINE fmap #-}
586− fmap f = fmapMaybe $ Just . f
594+ fmap f = mapMaybe $ Just . f
587595 {-# INLINE (<$) #-}
588596 x <$ e = fmapCheap (const x) e
589597
598+-- TODO Remove this instance
590599instance Reflex t => FunctorMaybe (Event t) where
591600 {-# INLINE fmapMaybe #-}
592− fmapMaybe f = push $ return . f
601+ fmapMaybe = mapMaybe
602+
603+instance Reflex t => Filterable (Event t) where
604+ {-# INLINE mapMaybe #-}
605+ mapMaybe f = push $ return . f
593606
594607-- | Never: @'zero' = 'never'@.
595608instance Reflex t => Plus (Event t) where
@@ -807,7 +820,7 @@ fanEither :: Reflex t => Event t (Either a b) -> (Event t a, Event t b)
807820fanEither e =
808821 let justLeft = either Just (const Nothing)
809822 justRight = either (const Nothing) Just
810− in (fmapMaybe justLeft e, fmapMaybe justRight e)
823+ in (mapMaybe justLeft e, mapMaybe justRight e)
811824
812825-- | Split the event into separate events for 'This' and 'That' values,
813826-- allowing them to fire simultaneously when the input value is 'These'.
@@ -819,7 +832,7 @@ fanThese e =
819832 that (That y) = Just y
820833 that (These _ y) = Just y
821834 that _ = Nothing
822− in (fmapMaybe this e, fmapMaybe that e)
835+ in (mapMaybe this e, mapMaybe that e)
823836
824837-- | Split the event into an 'EventSelector' that allows efficient selection of
825838-- the individual 'Event's.
@@ -1002,9 +1015,11 @@ difference = alignEventWithMaybe $ \case
10021015
10031016alignEventWithMaybe :: Reflex t => (These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
10041017alignEventWithMaybe f ea eb =
1005− fmapMaybe (f <=< dmapToThese)
1018+ mapMaybe (f <=< dmapToThese)
10061019 $ merge
10071020 $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
1021+alignEventWithMaybe f ea eb = mapMaybe (f <=< dmapToThese) $
1022+ merge $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
10081023
10091024filterEventKey
10101025 :: forall t m k v a.
@@ -1100,8 +1115,8 @@ mapAccumMaybeMDyn f z e = do
11001115 return $ case result of
11011116 (Nothing, Nothing) -> Nothing
11021117 _ -> Just result
1103− d' <- holdDyn z $ fmapMaybe fst e'
1104− return (d', fmapMaybe snd e')
1118+ d' <- holdDyn z $ mapMaybe fst e'
1119+ return (d', mapMaybe snd e')
11051120
11061121{-# INLINE accumB #-}
11071122accumB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> a) -> a -> Event t b -> m (Behavior t a)
@@ -1139,8 +1154,8 @@ mapAccumMaybeMB f z e = do
11391154 return $ case result of
11401155 (Nothing, Nothing) -> Nothing
11411156 _ -> Just result
1142− d' <- hold z $ fmapMaybe fst e'
1143− return (d', fmapMaybe snd e')
1157+ d' <- hold z $ mapMaybe fst e'
1158+ return (d', mapMaybe snd e')
11441159
11451160-- | Accumulate occurrences of an 'Event', producing an output occurrence each
11461161-- time. Discard the underlying 'Accumulator'.
@@ -1288,13 +1303,19 @@ infixl 4 <@
12881303pushAlwaysCheap :: Reflex t => (a -> PushM t b) -> Event t a -> Event t b
12891304pushAlwaysCheap f = pushCheap (fmap Just . f)
12901305
1306+{-# INLINE mapMaybeCheap #-}
1307+mapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
1308+mapMaybeCheap f = pushCheap $ return . f
1309+
1310+-- | An alias for 'mapMaybeCheap'
12911311{-# INLINE fmapMaybeCheap #-}
12921312fmapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
1293−fmapMaybeCheap f = pushCheap $ return . f
1313+fmapMaybeCheap = mapMaybeCheap
1314+
12941315
12951316{-# INLINE fforMaybeCheap #-}
12961317fforMaybeCheap :: Reflex t => Event t a -> (a -> Maybe b) -> Event t b
1297−fforMaybeCheap = flip fmapMaybeCheap
1318+fforMaybeCheap = flip mapMaybeCheap
12981319
12991320{-# INLINE fforCheap #-}
13001321fforCheap :: Reflex t => Event t a -> (a -> b) -> Event t b