@@ -21,9 +21,13 @@
2121{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
2222#endif
2323
24−-- | This module contains the Reflex interface, as well as a variety of
25−-- convenience functions for working with 'Event's, 'Behavior's, and other
26−-- signals.
24+-- |
25+-- Module:
26+-- Reflex.Class
27+-- Description:
28+-- This module contains the Reflex interface, as well as a variety of
29+-- convenience functions for working with 'Event's, 'Behavior's, and other
30+-- signals.
2731module Reflex.Class
2832 ( module Reflex.Patch
2933 -- * Primitives
@@ -256,8 +260,7 @@ class ( MonadHold t (PushM t)
256260 -- | Create an 'Event' that will occur whenever the currently-selected input
257261 -- 'Event' occurs
258262 switch :: Behavior t (Event t a) -> Event t a
259− -- | Create an 'Event' that will occur whenever the input event is occurring
260− -- and its occurrence value, another 'Event', is also occurring
263+ -- | Create an 'Event' that will occur whenever the input event is occurring -- and its occurrence value, another 'Event', is also occurring
261264 coincidence :: Event t (Event t a) -> Event t a
262265 -- | Extract the 'Behavior' of a 'Dynamic'.
263266 current :: Dynamic t a -> Behavior t a
@@ -998,14 +1001,16 @@ distributeListOverDynWith f = fmap (f . map (\(Const2 _ :=> Identity v) -> v) .
9981001difference :: Reflex t => Event t a -> Event t b -> Event t a
9991002difference = alignEventWithMaybe $ \case
10001003 This a -> Just a
1001− _ -> Nothing
1004+ _ -> Nothing
10021005
1006+-- | Zips two values by taking the union of their shapes and combining with the provided function.
1007+-- 'Nothing' values are dropped.
10031008alignEventWithMaybe :: Reflex t => (These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
1004−alignEventWithMaybe f ea eb =
1005− fmapMaybe (f <=< dmapToThese)
1006− $ merge
1007− $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
1009+alignEventWithMaybe f ea eb = fmapMaybe (f <=< dmapToThese) $
1010+ merge $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
10081011
1012+-- | Produces an 'Event' that fires only when the input event fires with a 'DSum' key that
1013+-- matches the provided key.
10091014filterEventKey
10101015 :: forall t m k v a.
10111016 ( Reflex t
@@ -1023,7 +1028,10 @@ filterEventKey k kv' = do
10231028 Nothing -> Nothing
10241029 takeWhileJustE f kv'
10251030
1026−
1031+-- | "Factor" the input 'DSum' 'Event' to produce an 'Event' which
1032+-- fires when the 'DSum' key changes and contains both the value of the
1033+-- 'DSum' at switchover and an 'Event' of values produced by subsequent
1034+-- firings of the input 'Event' that do not change the 'DSum' key.
10271035factorEvent
10281036 :: forall t m k v a.
10291037 ( Reflex t
@@ -1073,26 +1081,94 @@ class Reflex t => Accumulator t f | f -> t where
10731081 mapAccumMaybe f = mapAccumMaybeM $ \v o -> return $ f v o
10741082 mapAccumMaybeM :: (MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a, Maybe c)) -> a -> Event t b -> m (f a, Event t c)
10751083
1076−accumDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> a) -> a -> Event t b -> m (Dynamic t a)
1084+-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
1085+-- with the provided function. See 'foldDyn'.
1086+accumDyn
1087+ :: (Reflex t, MonadHold t m, MonadFix m)
1088+ => (a -> b -> a)
1089+ -> a
1090+ -> Event t b
1091+ -> m (Dynamic t a)
10771092accumDyn f = accumMaybeDyn $ \v o -> Just $ f v o
1078−accumMDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t a) -> a -> Event t b -> m (Dynamic t a)
1093+
1094+-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
1095+-- with the provided 'PushM' action.
1096+accumMDyn
1097+ :: (Reflex t, MonadHold t m, MonadFix m)
1098+ => (a -> b -> PushM t a)
1099+ -> a
1100+ -> Event t b
1101+ -> m (Dynamic t a)
10791102accumMDyn f = accumMaybeMDyn $ \v o -> Just <$> f v o
1080−accumMaybeDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> Maybe a) -> a -> Event t b -> m (Dynamic t a)
1103+
1104+-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
1105+-- with the provided function, discarding 'Nothing' results.
1106+accumMaybeDyn
1107+ :: (Reflex t, MonadHold t m, MonadFix m)
1108+ => (a -> b -> Maybe a)
1109+ -> a
1110+ -> Event t b
1111+ -> m (Dynamic t a)
10811112accumMaybeDyn f = accumMaybeMDyn $ \v o -> return $ f v o
1082−accumMaybeMDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
1113+
1114+-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
1115+-- with the provided 'PushM' action, discarding 'Nothing' results.
1116+accumMaybeMDyn
1117+ :: (Reflex t, MonadHold t m, MonadFix m)
1118+ => (a -> b -> PushM t (Maybe a))
1119+ -> a
1120+ -> Event t b
1121+ -> m (Dynamic t a)
10831122accumMaybeMDyn f z e = do
10841123 rec let e' = flip push e $ \o -> do
10851124 v <- sample $ current d'
10861125 f v o
10871126 d' <- holdDyn z e'
10881127 return d'
1089−mapAccumDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> (a, c)) -> a -> Event t b -> m (Dynamic t a, Event t c)
1128+
1129+-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
1130+-- with a function that both accumulates and produces a value to fire
1131+-- as an 'Event'. Returns both the accumulated value and an 'Event'.
1132+mapAccumDyn
1133+ :: (Reflex t, MonadHold t m, MonadFix m)
1134+ => (a -> b -> (a, c))
1135+ -> a
1136+ -> Event t b
1137+ -> m (Dynamic t a, Event t c)
10901138mapAccumDyn f = mapAccumMaybeDyn $ \v o -> bimap Just Just $ f v o
1091−mapAccumMDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (a, c)) -> a -> Event t b -> m (Dynamic t a, Event t c)
1139+
1140+-- | Similar to 'mapAccumDyn' except that the combining function is a
1141+-- 'PushM' action.
1142+mapAccumMDyn
1143+ :: (Reflex t, MonadHold t m, MonadFix m)
1144+ => (a -> b -> PushM t (a, c))
1145+ -> a
1146+ -> Event t b
1147+ -> m (Dynamic t a, Event t c)
10921148mapAccumMDyn f = mapAccumMaybeMDyn $ \v o -> bimap Just Just <$> f v o
1093−mapAccumMaybeDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> (Maybe a, Maybe c)) -> a -> Event t b -> m (Dynamic t a, Event t c)
1149+
1150+-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event' with
1151+-- a function that both optionally accumulates and optionally produces
1152+-- a value to fire as a separate output 'Event'.
1153+-- Note that because 'Nothing's are discarded in both cases, the output
1154+-- 'Event' may fire even though the output 'Dynamic' has not changed, and
1155+-- the output 'Dynamic' may update even when the output 'Event' is not firing.
1156+mapAccumMaybeDyn
1157+ :: (Reflex t, MonadHold t m, MonadFix m)
1158+ => (a -> b -> (Maybe a, Maybe c))
1159+ -> a
1160+ -> Event t b
1161+ -> m (Dynamic t a, Event t c)
10941162mapAccumMaybeDyn f = mapAccumMaybeMDyn $ \v o -> return $ f v o
1095−mapAccumMaybeMDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a, Maybe c)) -> a -> Event t b -> m (Dynamic t a, Event t c)
1163+
1164+-- | Like 'mapAccumMaybeDyn' except that the combining function is a
1165+-- 'PushM' action.
1166+mapAccumMaybeMDyn
1167+ :: (Reflex t, MonadHold t m, MonadFix m)
1168+ => (a -> b -> PushM t (Maybe a, Maybe c))
1169+ -> a
1170+ -> Event t b
1171+ -> m (Dynamic t a, Event t c)
10961172mapAccumMaybeMDyn f z e = do
10971173 rec let e' = flip push e $ \o -> do
10981174 v <- sample $ current d'
@@ -1103,15 +1179,39 @@ mapAccumMaybeMDyn f z e = do
11031179 d' <- holdDyn z $ fmapMaybe fst e'
11041180 return (d', fmapMaybe snd e')
11051181
1182+-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
1183+-- with the provided function.
11061184{-# INLINE accumB #-}
1107−accumB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> a) -> a -> Event t b -> m (Behavior t a)
1185+accumB
1186+ :: (Reflex t, MonadHold t m, MonadFix m)
1187+ => (a -> b -> a)
1188+ -> a
1189+ -> Event t b
1190+ -> m (Behavior t a)
11081191accumB f = accumMaybeB $ \v o -> Just $ f v o
1192+
1193+-- | Like 'accumB' except that the combining function is a 'PushM' action.
11091194{-# INLINE accumMB #-}
1110−accumMB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t a) -> a -> Event t b -> m (Behavior t a)
1195+accumMB
1196+ :: (Reflex t, MonadHold t m, MonadFix m)
1197+ => (a -> b -> PushM t a)
1198+ -> a
1199+ -> Event t b
1200+ -> m (Behavior t a)
11111201accumMB f = accumMaybeMB $ \v o -> Just <$> f v o
1202+
1203+-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
1204+-- with the provided function, discarding 'Nothing' results.
11121205{-# INLINE accumMaybeB #-}
1113−accumMaybeB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> Maybe a) -> a -> Event t b -> m (Behavior t a)
1206+accumMaybeB
1207+ :: (Reflex t, MonadHold t m, MonadFix m)
1208+ => (a -> b -> Maybe a)
1209+ -> a
1210+ -> Event t b
1211+ -> m (Behavior t a)
11141212accumMaybeB f = accumMaybeMB $ \v o -> return $ f v o
1213+
1214+-- | Like 'accumMaybeB' except that the combining function is a 'PushM' action.
11151215{-# INLINE accumMaybeMB #-}
11161216accumMaybeMB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
11171217accumMaybeMB f z e = do
@@ -1120,16 +1220,42 @@ accumMaybeMB f z e = do
11201220 f v o
11211221 d' <- hold z e'
11221222 return d'
1223+
1224+-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
1225+-- with a function that both accumulates and produces a value to fire
1226+-- as an 'Event'. Returns both the accumulated value and an 'Event'.
11231227{-# INLINE mapAccumB #-}
1124−mapAccumB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> (a, c)) -> a -> Event t b -> m (Behavior t a, Event t c)
1228+mapAccumB
1229+ :: (Reflex t, MonadHold t m, MonadFix m)
1230+ => (a -> b -> (a, c))
1231+ -> a
1232+ -> Event t b
1233+ -> m (Behavior t a, Event t c)
11251234mapAccumB f = mapAccumMaybeB $ \v o -> bimap Just Just $ f v o
1235+
1236+-- | Like 'mapAccumB' except that the combining function is a 'PushM' action.
11261237{-# INLINE mapAccumMB #-}
1127−mapAccumMB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (a, c)) -> a -> Event t b -> m (Behavior t a, Event t c)
1238+mapAccumMB
1239+ :: (Reflex t, MonadHold t m, MonadFix m)
1240+ => (a -> b -> PushM t (a, c))
1241+ -> a
1242+ -> Event t b
1243+ -> m (Behavior t a, Event t c)
11281244mapAccumMB f = mapAccumMaybeMB $ \v o -> bimap Just Just <$> f v o
1245+
1246+-- | Accumulate a 'Behavior' by folding occurrences of an 'Event' with
1247+-- a function that both optionally accumulates and optionally produces
1248+-- a value to fire as a separate output 'Event'. 'Nothing's are discarded.
11291249{-# INLINE mapAccumMaybeB #-}
1130−mapAccumMaybeB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> (Maybe a, Maybe c)) -> a -> Event t b -> m (Behavior t a, Event t c)
1250+mapAccumMaybeB
1251+ :: (Reflex t, MonadHold t m, MonadFix m)
1252+ => (a -> b -> (Maybe a, Maybe c))
1253+ -> a
1254+ -> Event t b
1255+ -> m (Behavior t a, Event t c)
11311256mapAccumMaybeB f = mapAccumMaybeMB $ \v o -> return $ f v o
11321257
1258+-- | LIke 'mapAccumMaybeB' except that the combining function is a 'PushM' action.
11331259{-# INLINE mapAccumMaybeMB #-}
11341260mapAccumMaybeMB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a, Maybe c)) -> a -> Event t b -> m (Behavior t a, Event t c)
11351261mapAccumMaybeMB f z e = do
@@ -1284,38 +1410,47 @@ infixl 4 <@
12841410-- Cheap Functions
12851411------------------
12861412
1413+-- | A "cheap" version of 'pushAlways'. See the performance note on 'pushCheap'.
12871414{-# INLINE pushAlwaysCheap #-}
12881415pushAlwaysCheap :: Reflex t => (a -> PushM t b) -> Event t a -> Event t b
12891416pushAlwaysCheap f = pushCheap (fmap Just . f)
12901417
1418+-- | A "cheap" version of 'fmapMaybe'. See the performance note on 'pushCheap'.
12911419{-# INLINE fmapMaybeCheap #-}
12921420fmapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
12931421fmapMaybeCheap f = pushCheap $ return . f
12941422
1423+-- | A "cheap" version of 'fforMaybe'. See the performance note on 'pushCheap'.
12951424{-# INLINE fforMaybeCheap #-}
12961425fforMaybeCheap :: Reflex t => Event t a -> (a -> Maybe b) -> Event t b
12971426fforMaybeCheap = flip fmapMaybeCheap
12981427
1428+-- | A "cheap" version of 'ffor'. See the performance note on 'pushCheap'.
12991429{-# INLINE fforCheap #-}
13001430fforCheap :: Reflex t => Event t a -> (a -> b) -> Event t b
13011431fforCheap = flip fmapCheap
13021432
1433+-- | A "cheap" version of 'fmap'. See the performance note on 'pushCheap'.
13031434{-# INLINE fmapCheap #-}
13041435fmapCheap :: Reflex t => (a -> b) -> Event t a -> Event t b
13051436fmapCheap f = pushCheap $ return . Just . f
13061437
1438+-- | A "cheap" version of 'tag'. See the performance note on 'pushCheap'.
13071439{-# INLINE tagCheap #-}
13081440tagCheap :: Reflex t => Behavior t b -> Event t a -> Event t b
13091441tagCheap b = pushAlwaysCheap $ \_ -> sample b
13101442
1443+-- | A "cheap" version of 'mergeWithCheap'. See the performance note on 'pushCheap'.
13111444{-# INLINE mergeWithCheap #-}
13121445mergeWithCheap :: Reflex t => (a -> a -> a) -> [Event t a] -> Event t a
13131446mergeWithCheap = mergeWithCheap' id
13141447
1448+-- | A "cheap" version of 'mergeWithCheap''. See the performance note on 'pushCheap'.
13151449{-# INLINE mergeWithCheap' #-}
13161450mergeWithCheap' :: Reflex t => (a -> b) -> (b -> b -> b) -> [Event t a] -> Event t b
13171451mergeWithCheap' f g = mergeWithFoldCheap' $ foldl1 g . fmap f
13181452
1453+-- | A "cheap" version of 'mergeWithFoldCheap''. See the performance note on 'pushCheap'.
13191454{-# INLINE mergeWithFoldCheap' #-}
13201455mergeWithFoldCheap' :: Reflex t => (NonEmpty a -> b) -> [Event t a] -> Event t b
13211456mergeWithFoldCheap' f es =