@@ -133,8 +133,9 @@ module Reflex.Class
133 -- * Unsafe functions
133 -- * Unsafe functions
134 , unsafeDynamic
134 , unsafeDynamic
135 , unsafeMapIncremental
135 , unsafeMapIncremental
136− -- * 'FunctorMaybe'
136+ -- * 'Filterable' convenience functions
137− , FunctorMaybe (..)
137+ , FunctorMaybe -- fmapMaybe is purposely not exported from deprecated 'FunctorMaybe' and the new alias is exported instead
138 , fforMaybe
139 , fforMaybe
139 , ffilter
140 , ffilter
140 , filterLeft
141 , filterLeft
@@ -147,6 +148,7 @@ module Reflex.Class
147 , appendEvents
148 , appendEvents
149 , sequenceThese
150 , sequenceThese
150 , fmapMaybeCheap
152 , fmapMaybeCheap
151 , fmapCheap
153 , fmapCheap
152 , fforCheap
154 , fforCheap
@@ -193,7 +195,10 @@ import qualified Data.Some as Some
193import Data.String
195import Data.String
194import Data.These
196import Data.These
195import Data.Type.Coercion
197import Data.Type.Coercion
198+import Data.Witherable (Filterable(..))
199+import qualified Data.Witherable as W
200+import Reflex.FunctorMaybe (FunctorMaybe)
196−import Reflex.FunctorMaybe
201+import qualified Reflex.FunctorMaybe
197import Reflex.Patch
202import Reflex.Patch
198import qualified Reflex.Patch.MapWithMove as PatchMapWithMove
203import qualified Reflex.Patch.MapWithMove as PatchMapWithMove
@@ -388,8 +393,8 @@ mapAccumMaybeMIncremental f z e = do
388 return $ case result of
393 return $ case result of
389 (Nothing, Nothing) -> Nothing
394 (Nothing, Nothing) -> Nothing
390 _ -> Just result
395 _ -> Just result
391− d' <- holdIncremental z $ fmapMaybe fst e'
396+ d' <- holdIncremental z $ mapMaybe fst e'
392− return (d', fmapMaybe snd e')
397+ return (d', mapMaybe snd e')
394slowHeadE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a)
399slowHeadE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a)
395slowHeadE e = do
400slowHeadE e = do
@@ -399,14 +404,14 @@ slowHeadE e = do
400-- | An 'EventSelector' allows you to efficiently 'select' an 'Event' based on a
405-- | An 'EventSelector' allows you to efficiently 'select' an 'Event' based on a
401-- key. This is much more efficient than filtering for each key with
406-- key. This is much more efficient than filtering for each key with
402−-- 'fmapMaybe'.
407+-- 'mapMaybe'.
403newtype EventSelector t k = EventSelector
408newtype EventSelector t k = EventSelector
404 { -- | Retrieve the 'Event' for the given key. The type of the 'Event' is
409 { -- | Retrieve the 'Event' for the given key. The type of the 'Event' is
405 -- determined by the type of the key, so this can be used to fan-out
410 -- determined by the type of the key, so this can be used to fan-out
406 -- 'Event's whose sub-'Event's have different types.
411 -- 'Event's whose sub-'Event's have different types.
408 -- Using 'EventSelector's and the 'fan' primitive is far more efficient than
413 -- 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
410 -- occurrences of an 'Event'.
415 -- occurrences of an 'Event'.
411 select :: forall a. k a -> Event t a
416 select :: forall a. k a -> Event t a
@@ -550,22 +555,25 @@ instance (Reflex t, Semigroup a) => Semigroup (Behavior t a) where
550 times1p n = fmap $ times1p n
555 times1p n = fmap $ times1p n
558+-- | Alias for 'mapMaybe'
559+fmapMaybe :: Filterable f => (a -> Maybe b) -> f a -> f b
553−-- | Flipped version of 'fmapMaybe'.
562+-- | Flipped version of 'mapMaybe'.
554−fforMaybe :: FunctorMaybe f => f a -> (a -> Maybe b) -> f b
563+fforMaybe :: Filterable f => f a -> (a -> Maybe b) -> f b
555−fforMaybe = flip fmapMaybe
564+fforMaybe = flip mapMaybe
557-- | Filter 'f a' using the provided predicate.
566-- | Filter 'f a' using the provided predicate.
558−-- Relies on 'fforMaybe'.
559−ffilter :: FunctorMaybe f => (a -> Bool) -> f a -> f a
567+ffilter :: Filterable f => (a -> Bool) -> f a -> f a
560−ffilter f = fmapMaybe $ \x -> if f x then Just x else Nothing
568+ffilter = W.filter
562-- | Filter 'Left's from 'f (Either a b)' into 'a'.
570-- | Filter 'Left's from 'f (Either a b)' into 'a'.
563−filterLeft :: FunctorMaybe f => f (Either a b) -> f a
571+filterLeft :: Filterable f => f (Either a b) -> f a
564−filterLeft = fmapMaybe (either Just (const Nothing))
572+filterLeft = mapMaybe (either Just (const Nothing))
566-- | Filter 'Right's from 'f (Either a b)' into 'b'.
574-- | Filter 'Right's from 'f (Either a b)' into 'b'.
567−filterRight :: FunctorMaybe f => f (Either a b) -> f b
575+filterRight :: Filterable f => f (Either a b) -> f b
568−filterRight = fmapMaybe (either (const Nothing) Just)
576+filterRight = mapMaybe (either (const Nothing) Just)
570-- | Left-biased event union (prefers left event on simultaneous
578-- | Left-biased event union (prefers left event on simultaneous
571-- occurrence).
579-- occurrence).
@@ -583,13 +591,18 @@ instance Reflex t => Bind (Event t) where
584instance Reflex t => Functor (Event t) where
592instance Reflex t => Functor (Event t) where
585 {-# INLINE fmap #-}
593 {-# INLINE fmap #-}
586− fmap f = fmapMaybe $ Just . f
594+ fmap f = mapMaybe $ Just . f
587 {-# INLINE (<$) #-}
595 {-# INLINE (<$) #-}
588 x <$ e = fmapCheap (const x) e
596 x <$ e = fmapCheap (const x) e
598+-- TODO Remove this instance
590instance Reflex t => FunctorMaybe (Event t) where
599instance Reflex t => FunctorMaybe (Event t) where
591 {-# INLINE fmapMaybe #-}
600 {-# INLINE fmapMaybe #-}
601+ fmapMaybe = mapMaybe
603+instance Reflex t => Filterable (Event t) where
604+ {-# INLINE mapMaybe #-}
592− fmapMaybe f = push $ return . f
605+ mapMaybe f = push $ return . f
594-- | Never: @'zero' = 'never'@.
607-- | Never: @'zero' = 'never'@.
595instance Reflex t => Plus (Event t) where
608instance Reflex t => Plus (Event t) where
@@ -807,7 +820,7 @@ fanEither :: Reflex t => Event t (Either a b) -> (Event t a, Event t b)
807fanEither e =
820fanEither e =
808 let justLeft = either Just (const Nothing)
821 let justLeft = either Just (const Nothing)
809 justRight = either (const Nothing) Just
822 justRight = either (const Nothing) Just
810− in (fmapMaybe justLeft e, fmapMaybe justRight e)
823+ in (mapMaybe justLeft e, mapMaybe justRight e)
812-- | Split the event into separate events for 'This' and 'That' values,
825-- | Split the event into separate events for 'This' and 'That' values,
813-- allowing them to fire simultaneously when the input value is 'These'.
826-- allowing them to fire simultaneously when the input value is 'These'.
@@ -819,7 +832,7 @@ fanThese e =
819 that (That y) = Just y
832 that (That y) = Just y
820 that (These _ y) = Just y
833 that (These _ y) = Just y
821 that _ = Nothing
834 that _ = Nothing
822− in (fmapMaybe this e, fmapMaybe that e)
835+ in (mapMaybe this e, mapMaybe that e)
824-- | Split the event into an 'EventSelector' that allows efficient selection of
837-- | Split the event into an 'EventSelector' that allows efficient selection of
825-- the individual 'Event's.
838-- the individual 'Event's.
@@ -1002,9 +1015,11 @@ difference = alignEventWithMaybe $ \case
1003alignEventWithMaybe :: Reflex t => (These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
1016alignEventWithMaybe :: Reflex t => (These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
1004alignEventWithMaybe f ea eb =
1017alignEventWithMaybe f ea eb =
1005− fmapMaybe (f <=< dmapToThese)
1018+ mapMaybe (f <=< dmapToThese)
1007 $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
1020 $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
1021+alignEventWithMaybe f ea eb = mapMaybe (f <=< dmapToThese) $
1022+ merge $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
1009filterEventKey
1024filterEventKey
1010 :: forall t m k v a.
1025 :: forall t m k v a.
@@ -1100,8 +1115,8 @@ mapAccumMaybeMDyn f z e = do
1100 return $ case result of
1115 return $ case result of
1101 (Nothing, Nothing) -> Nothing
1116 (Nothing, Nothing) -> Nothing
1102 _ -> Just result
1117 _ -> Just result
1103− d' <- holdDyn z $ fmapMaybe fst e'
1118+ d' <- holdDyn z $ mapMaybe fst e'
1104− return (d', fmapMaybe snd e')
1119+ return (d', mapMaybe snd e')
1106{-# INLINE accumB #-}
1121{-# INLINE accumB #-}
1107accumB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> a) -> a -> Event t b -> m (Behavior t a)
1122accumB :: (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
1139 return $ case result of
1154 return $ case result of
1140 (Nothing, Nothing) -> Nothing
1155 (Nothing, Nothing) -> Nothing
1141 _ -> Just result
1156 _ -> Just result
1142− d' <- hold z $ fmapMaybe fst e'
1157+ d' <- hold z $ mapMaybe fst e'
1143− return (d', fmapMaybe snd e')
1158+ return (d', mapMaybe snd e')
1145-- | Accumulate occurrences of an 'Event', producing an output occurrence each
1160-- | Accumulate occurrences of an 'Event', producing an output occurrence each
1146-- time. Discard the underlying 'Accumulator'.
1161-- time. Discard the underlying 'Accumulator'.
@@ -1288,13 +1303,19 @@ infixl 4 <@
1288pushAlwaysCheap :: Reflex t => (a -> PushM t b) -> Event t a -> Event t b
1303pushAlwaysCheap :: Reflex t => (a -> PushM t b) -> Event t a -> Event t b
1289pushAlwaysCheap f = pushCheap (fmap Just . f)
1304pushAlwaysCheap f = pushCheap (fmap Just . f)
1306+{-# INLINE mapMaybeCheap #-}
1307+mapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
1308+mapMaybeCheap f = pushCheap $ return . f
1310+-- | An alias for 'mapMaybeCheap'
1291{-# INLINE fmapMaybeCheap #-}
1311{-# INLINE fmapMaybeCheap #-}
1292fmapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
1312fmapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
1293−fmapMaybeCheap f = pushCheap $ return . f
1313+fmapMaybeCheap = mapMaybeCheap
1295{-# INLINE fforMaybeCheap #-}
1316{-# INLINE fforMaybeCheap #-}
1296fforMaybeCheap :: Reflex t => Event t a -> (a -> Maybe b) -> Event t b
1317fforMaybeCheap :: Reflex t => Event t a -> (a -> Maybe b) -> Event t b
1297−fforMaybeCheap = flip fmapMaybeCheap
1318+fforMaybeCheap = flip mapMaybeCheap
1299{-# INLINE fforCheap #-}
1320{-# INLINE fforCheap #-}
1300fforCheap :: Reflex t => Event t a -> (a -> b) -> Event t b
1321fforCheap :: Reflex t => Event t a -> (a -> b) -> Event t b