2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE LambdaCase #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE NoMonomorphismRestriction #-}
9{-# LANGUAGE PolyKinds #-}
10{-# LANGUAGE RankNTypes #-}
11{-# LANGUAGE RecursiveDo #-}
12{-# LANGUAGE ScopedTypeVariables #-}
13{-# LANGUAGE StandaloneDeriving #-}
14{-# LANGUAGE TypeFamilies #-}
15{-# LANGUAGE TypeOperators #-}
16{-# LANGUAGE UndecidableInstances #-}
17#ifdef USE_REFLEX_OPTIMIZER
18{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
20-- | This module contains various functions for working with 'Dynamic' values.
21-- 'Dynamic' and its primitives have been moved to the 'Reflex' class.
24 Dynamic -- Abstract so we can preserve the law that the current value is always equal to the most recent update
37 , attachPromptlyDynWith
38 , attachPromptlyDynWithMaybe
55 , distributeMapOverDynPure
56 , distributeDMapOverDynPure
57 , distributeListOverDynPure
62 -- Things that probably aren't very useful:
66 , RebuildSortedHList (..)
70 , distributeFHListOverDynPure
73 -- * Deprecated functions
80 , distributeDMapOverDyn
81 , distributeFHListOverDyn
94import Data.Functor.Compose
95import Data.Functor.Misc
98import Control.Applicative ((<*>))
100import Control.Monad.Fix
101import Control.Monad.Identity
103import Data.Dependent.Map (DMap)
104import qualified Data.Dependent.Map as DMap
105import Data.Dependent.Sum (DSum (..))
106import Data.Functor.Product
107import Data.GADT.Compare ((:~:) (..), GCompare (..), GEq (..), GOrdering (..))
110import Data.Monoid hiding (Product)
115-- | Map a sampling function over a 'Dynamic'.
116mapDynM :: forall t m a b. (Reflex t, MonadHold t m) => (forall m'. MonadSample t m' => a -> m' b) -> Dynamic t a -> m (Dynamic t b)
117mapDynM f d = buildDynamic (f =<< sample (current d)) $ pushAlways f (updated d)
119-- | Flipped version of 'mapDynM'
120forDynM :: forall t m a b. (Reflex t, MonadHold t m) => Dynamic t a -> (forall m'. MonadSample t m' => a -> m' b) -> m (Dynamic t b)
121forDynM d f = mapDynM f d
123-- | Create a new 'Dynamic' that only signals changes if the values actually
125holdUniqDyn :: (Reflex t, MonadHold t m, MonadFix m, Eq a) => Dynamic t a -> m (Dynamic t a)
126holdUniqDyn = holdUniqDynBy (==)
128-- | Create a new 'Dynamic' that changes only when the underlying 'Dynamic'
129-- changes and the given function returns 'False' when given both the old and
131holdUniqDynBy :: (Reflex t, MonadHold t m, MonadFix m) => (a -> a -> Bool) -> Dynamic t a -> m (Dynamic t a)
132holdUniqDynBy eq = scanDynMaybe id (\new old -> if new `eq` old then Nothing else Just new)
134-- | @/Dynamic Maybe/@ that can only update from @/Nothing/@ to @/Just/@ or @/Just/@ to @/Just/@ (i.e., cannot revert to @/Nothing/@)
135improvingMaybe :: (Reflex t, MonadHold t m, MonadFix m) => Dynamic t (Maybe a) -> m (Dynamic t (Maybe a))
136improvingMaybe = scanDynMaybe id (\new _ -> if isJust new then Just new else Nothing)
138-- | Create a 'Dynamic' that accumulates values from another 'Dynamic'. This
139-- function does not force its input 'Dynamic' until the output 'Dynamic' is
141scanDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b) -> (a -> b -> b) -> Dynamic t a -> m (Dynamic t b)
142scanDyn z f = scanDynMaybe z (\a b -> Just $ f a b)
144-- | Like 'scanDyn', but the the accumulator function may decline to update the
145-- result 'Dynamic''s value.
146scanDynMaybe :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b) -> (a -> b -> Maybe b) -> Dynamic t a -> m (Dynamic t b)
147scanDynMaybe z f d = do
148 rec d' <- buildDynamic (z <$> sample (current d)) $ flip push (updated d) $ \a -> do
149 b <- sample $ current d'
153-- | Create a 'Dynamic' using the initial value and change it each time the
154-- 'Event' occurs using a folding function on the previous value and the value
156foldDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> b) -> b -> Event t a -> m (Dynamic t b)
157foldDyn = accumDyn . flip
159-- | Like 'foldDyn', but the combining function is a 'PushM' action, so it
160-- can 'sample' existing 'Behaviors' and 'hold' new ones.
161foldDynM :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t b) -> b -> Event t a -> m (Dynamic t b)
162foldDynM = accumMDyn . flip
164-- | Create a 'Dynamic' using the provided initial value and change it each time
165-- the provided 'Event' occurs, using a function to combine the old value with
166-- the 'Event''s value. If the function returns 'Nothing', the value is not
167-- changed; this is distinct from returning 'Just' the old value, since the
168-- 'Dynamic''s 'updated' 'Event' will fire in the 'Just' case, and will not fire
169-- in the 'Nothing' case.
170foldDynMaybe :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> Maybe b) -> b -> Event t a -> m (Dynamic t b)
171foldDynMaybe = accumMaybeDyn . flip
173-- | Like 'foldDynMaybe', but the combining function is a 'PushM' action, so it
174-- can 'sample' existing 'Behaviors' and 'hold' new ones.
175foldDynMaybeM :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe b)) -> b -> Event t a -> m (Dynamic t b)
176foldDynMaybeM = accumMaybeMDyn . flip
178-- | Create a new 'Dynamic' that counts the occurrences of the 'Event'.
179count :: (Reflex t, MonadHold t m, MonadFix m, Num b) => Event t a -> m (Dynamic t b)
180count e = holdDyn 0 =<< zipListWithEvent const (iterate (+1) 1) e
182-- | Create a new 'Dynamic' using the initial value that flips its
183-- value every time the 'Event' occurs.
184toggle :: (Reflex t, MonadHold t m, MonadFix m) => Bool -> Event t a -> m (Dynamic t Bool)
185toggle = foldDyn (const not)
187-- | Switches to the new 'Event' whenever it receives one. Only the old event is
188-- considered the moment a new one is switched in; the output event will fire at
189-- that moment if only if the old event does.
191-- Prefer this to 'switchPromptlyDyn' where possible. The lack of doing double
192-- work when the outer and (new) inner fires means this imposes fewer "timing
193-- requirements" and thus is far more easy to use without introducing fresh
194-- failure cases. 'switchDyn' is also more performant.
195switchDyn :: forall t a. Reflex t => Dynamic t (Event t a) -> Event t a
196switchDyn d = switch (current d)
198-- | Switches to the new 'Event' whenever it receives one. Switching occurs
199-- __before__ the inner 'Event' fires - so if the 'Dynamic' changes and both the
200-- old and new inner Events fire simultaneously, the output will fire with the
201-- value of the __new__ 'Event'.
203-- Prefer 'switchDyn' to this where possible. The timing requirements that
204-- switching before imposes are likely to bring down your app unless you are
205-- very careful. 'switchDyn' is also more performant.
206switchPromptlyDyn :: forall t a. Reflex t => Dynamic t (Event t a) -> Event t a
207switchPromptlyDyn de =
208 let eLag = switch $ current de
209 eCoincidences = coincidence $ updated de
210 in leftmost [eCoincidences, eLag]
212-- | Split a 'Dynamic' pair into a pair of 'Dynamic's
213splitDynPure :: Reflex t => Dynamic t (a, b) -> (Dynamic t a, Dynamic t b)
214splitDynPure d = (fmap fst d, fmap snd d)
216-- | Convert a 'Map' with 'Dynamic' elements into a 'Dynamic' of a 'Map' with
217-- non-'Dynamic' elements.
218distributeMapOverDynPure :: (Reflex t, Ord k) => Map k (Dynamic t v) -> Dynamic t (Map k v)
219distributeMapOverDynPure = fmap dmapToMap . distributeDMapOverDynPure . mapWithFunctorToDMap
221-- | Convert a list with 'Dynamic' elements into a 'Dynamic' of a list with
222-- non-'Dynamic' elements, preserving the order of the elements.
223{-# DEPRECATED distributeListOverDynPure "Use 'distributeListOverDyn' instead" #-}
224distributeListOverDynPure :: Reflex t => [Dynamic t v] -> Dynamic t [v]
225distributeListOverDynPure = distributeListOverDyn
227--TODO: Generalize this to functors other than Maps
228-- | Combine a 'Dynamic' of a 'Map' of 'Dynamic's into a 'Dynamic'
229-- with the current values of the 'Dynamic's in a map.
230joinDynThroughMap :: forall t k a. (Reflex t, Ord k) => Dynamic t (Map k (Dynamic t a)) -> Dynamic t (Map k a)
231joinDynThroughMap = joinDyn . fmap distributeMapOverDynPure
233-- | Print the value of the 'Dynamic' when it is first read and on each
234-- subsequent change that is observed (as 'traceEvent'), prefixed with the
235-- provided string. This should /only/ be used for debugging.
237-- Note: Just like Debug.Trace.trace, the value will only be shown if something
238-- else in the system is depending on it.
239traceDyn :: (Reflex t, Show a) => String -> Dynamic t a -> Dynamic t a
240traceDyn s = traceDynWith $ \x -> s <> ": " <> show x
242-- | Print the result of applying the provided function to the value
243-- of the 'Dynamic' when it is first read and on each subsequent change
244-- that is observed (as 'traceEvent'). This should /only/ be used for
247-- Note: Just like Debug.Trace.trace, the value will only be shown if something
248-- else in the system is depending on it.
249traceDynWith :: Reflex t => (a -> String) -> Dynamic t a -> Dynamic t a
251 let e' = traceEventWith f $ updated d
253 x <- sample $ current d
254 trace (f x) $ return x
255 in unsafeBuildDynamic getV0 e'
257-- | Replace the value of the 'Event' with the current value of the 'Dynamic'
258-- each time the 'Event' occurs.
260-- Note: @/tagPromptlyDyn d e/@ differs from @/tag (current d) e/@ in the case that @/e/@ is firing
261-- at the same time that @/d/@ is changing. With @/tagPromptlyDyn d e/@, the __new__ value of @/d/@
262-- will replace the value of @/e/@, whereas with @/tag (current d) e/@, the __old__ value
263-- will be used, since the 'Behavior' won't be updated until the end of the frame.
264-- Additionally, this means that the output 'Event' may not be used to directly change
265-- the input 'Dynamic', because that would mean its value depends on itself. __When creating__
266-- __cyclic data flows, generally @/tag (current d) e/@ is preferred.__
267tagPromptlyDyn :: Reflex t => Dynamic t a -> Event t b -> Event t a
268tagPromptlyDyn = attachPromptlyDynWith const
270-- | Attach the current value of the 'Dynamic' to the value of the
271-- 'Event' each time it occurs.
273-- Note: @/attachPromptlyDyn d/@ is not the same as @/attach (current d)/@. See 'tagPromptlyDyn' for details.
274attachPromptlyDyn :: Reflex t => Dynamic t a -> Event t b -> Event t (a, b)
275attachPromptlyDyn = attachPromptlyDynWith (,)
277-- | Combine the current value of the 'Dynamic' with the value of the
278-- 'Event' each time it occurs.
280-- Note: @/attachPromptlyDynWith f d/@ is not the same as @/attachWith f (current d)/@. See 'tagPromptlyDyn' for details.
281attachPromptlyDynWith :: Reflex t => (a -> b -> c) -> Dynamic t a -> Event t b -> Event t c
282attachPromptlyDynWith f = attachPromptlyDynWithMaybe $ \a b -> Just $ f a b
284-- | Create a new 'Event' by combining the value at each occurrence with the
285-- current value of the 'Dynamic' value and possibly filtering if the combining
286-- function returns 'Nothing'.
288-- Note: @/attachPromptlyDynWithMaybe f d/@ is not the same as @/attachWithMaybe f (current d)/@. See 'tagPromptlyDyn' for details.
289attachPromptlyDynWithMaybe :: Reflex t => (a -> b -> Maybe c) -> Dynamic t a -> Event t b -> Event t c
290attachPromptlyDynWithMaybe f d e =
291 let e' = attach (current d) e
292 in fforMaybe (align e' $ updated d) $ \case
293 This (a, b) -> f a b -- Only the tagging event is firing, so use that
294 These (_, b) a -> f a b -- Both events are firing, so use the newer value
295 That _ -> Nothing -- The tagging event isn't firing, so don't fire
297-- | Factor a @/Dynamic t (Maybe a)/@ into a @/Dynamic t (Maybe (Dynamic t a))/@,
298-- such that the outer 'Dynamic' is updated only when the "Maybe"'s constructor
299-- chages from 'Nothing' to 'Just' or vice-versa. Whenever the constructor
300-- becomes 'Just', an inner 'Dynamic' will be provided, whose value will track
301-- the 'a' inside the 'Just'; when the constructor becomes 'Nothing', the
302-- existing inner 'Dynamic' will become constant, and will not change when the
303-- outer constructor changes back to 'Nothing'.
304maybeDyn :: forall t a m. (Reflex t, MonadFix m, MonadHold t m) => Dynamic t (Maybe a) -> m (Dynamic t (Maybe (Dynamic t a)))
305maybeDyn = fmap (fmap unpack) . eitherDyn . fmap pack
313eitherDyn :: forall t a b m. (Reflex t, MonadFix m, MonadHold t m) => Dynamic t (Either a b) -> m (Dynamic t (Either (Dynamic t a) (Dynamic t b)))
314eitherDyn = fmap (fmap unpack) . factorDyn . fmap eitherToDSum
315 where unpack :: DSum (EitherTag a b) (Compose (Dynamic t) Identity) -> Either (Dynamic t a) (Dynamic t b)
317 LeftTag :=> Compose a -> Left $ coerceDynamic a
318 RightTag :=> Compose b -> Right $ coerceDynamic b
320factorDyn :: forall t m k v. (Reflex t, MonadHold t m, GEq k)
321 => Dynamic t (DSum k v) -> m (Dynamic t (DSum k (Compose (Dynamic t) v)))
322factorDyn d = buildDynamic (sample (current d) >>= holdKey) update where
323 update :: Event t (DSum k (Compose (Dynamic t) v))
324 update = flip push (updated d) $ \(newKey :=> newVal) -> do
325 (oldKey :=> _) <- sample (current d)
326 case newKey `geq` oldKey of
327 Just Refl -> return Nothing
328 Nothing -> Just <$> holdKey (newKey :=> newVal)
330 holdKey (k :=> v) = do
331 inner' <- filterEventKey k (updated d)
332 inner <- holdDyn v inner'
333 return $ k :=> Compose inner
334--------------------------------------------------------------------------------
336--------------------------------------------------------------------------------
338-- | Represents a time changing value together with an 'EventSelector' that can
339-- efficiently detect when the underlying 'Dynamic' has a particular value.
340-- This is useful for representing data like the current selection of a long
345-- > demuxed (demux d) k === fmap (== k) d
347-- However, the when getDemuxed is used multiple times, the complexity is only
348-- /O(log(n))/, rather than /O(n)/ for fmap.
349data Demux t k = Demux { demuxValue :: Behavior t k
350 , demuxSelector :: EventSelector t (Const2 k Bool)
353-- | Demultiplex an input value to a 'Demux' with many outputs. At any given
354-- time, whichever output is indicated by the given 'Dynamic' will be 'True'.
355demux :: (Reflex t, Ord k) => Dynamic t k -> Demux t k
356demux k = Demux (current k)
357 (fan $ attachWith (\k0 k1 -> if k0 == k1
359 else DMap.fromList [Const2 k0 :=> Identity False,
360 Const2 k1 :=> Identity True])
361 (current k) (updated k))
363-- | Select a particular output of the 'Demux'; this is equivalent to (but much
364-- faster than) mapping over the original 'Dynamic' and checking whether it is
365-- equal to the given key.
366demuxed :: (Reflex t, Eq k) => Demux t k -> k -> Dynamic t Bool
368 let e = select (demuxSelector d) (Const2 k)
369 in unsafeBuildDynamic (fmap (==k) $ sample $ demuxValue d) e
371--------------------------------------------------------------------------------
373--------------------------------------------------------------------------------
375--TODO: This whole section is badly in need of cleanup
377-- | A heterogeneous list whose type and length are fixed statically. This is
378-- reproduced from the 'HList' package due to integration issues, and because
379-- very little other functionality from that library is needed.
380data HList (l::[*]) where
382 HCons :: e -> HList l -> HList (e ': l)
386type family HRevApp (l1 :: [k]) (l2 :: [k]) :: [k]
387type instance HRevApp '[] l = l
388type instance HRevApp (e ': l) l' = HRevApp l (e ': l')
390hRevApp :: HList l1 -> HList l2 -> HList (HRevApp l1 l2)
392hRevApp (HCons x l) l' = hRevApp l (HCons x l')
394hReverse :: HList l -> HList (HRevApp l '[])
395hReverse l = hRevApp l HNil
397hBuild :: (HBuild' '[] r) => r
400class HBuild' l r where
401 hBuild' :: HList l -> r
403instance (l' ~ HRevApp l '[])
404 => HBuild' l (HList l') where
407instance HBuild' (a ': l) r
408 => HBuild' l (a->r) where
409 hBuild' l x = hBuild' (HCons x l)
411-- | Like 'HList', but with a functor wrapping each element.
413 FHNil :: FHList f '[]
414 FHCons :: f e -> FHList f l -> FHList f (e ': l)
416instance GEq (HListPtr l) where
417 HHeadPtr `geq` HHeadPtr = Just Refl
418 HHeadPtr `geq` HTailPtr _ = Nothing
419 HTailPtr _ `geq` HHeadPtr = Nothing
420 HTailPtr a `geq` HTailPtr b = a `geq` b
422instance GCompare (HListPtr l) where -- Warning: This ordering can't change, dmapTo*HList will break
423 HHeadPtr `gcompare` HHeadPtr = GEQ
424 HHeadPtr `gcompare` HTailPtr _ = GLT
425 HTailPtr _ `gcompare` HHeadPtr = GGT
426 HTailPtr a `gcompare` HTailPtr b = a `gcompare` b
428-- | A typed index into a typed heterogeneous list.
429data HListPtr l a where
430 HHeadPtr :: HListPtr (h ': t) h
431 HTailPtr :: HListPtr t a -> HListPtr (h ': t) a
433deriving instance Eq (HListPtr l a)
434deriving instance Ord (HListPtr l a)
436fhlistToDMap :: forall (f :: * -> *) l. FHList f l -> DMap (HListPtr l) f
437fhlistToDMap = DMap.fromList . go
438 where go :: forall l'. FHList f l' -> [DSum (HListPtr l') f]
441 FHCons h t -> (HHeadPtr :=> h) : map (\(p :=> v) -> HTailPtr p :=> v) (go t)
443-- | This class allows 'HList's and 'FHlist's to be built from regular lists;
444-- they must be contiguous and sorted.
445class RebuildSortedHList l where
446 rebuildSortedFHList :: [DSum (HListPtr l) f] -> FHList f l
447 rebuildSortedHList :: [DSum (HListPtr l) Identity] -> HList l
449instance RebuildSortedHList '[] where
450 rebuildSortedFHList l = case l of
452 _ : _ -> error "rebuildSortedFHList{'[]}: empty list expected"
453 rebuildSortedHList l = case l of
455 _ : _ -> error "rebuildSortedHList{'[]}: empty list expected"
457instance RebuildSortedHList t => RebuildSortedHList (h ': t) where
458 rebuildSortedFHList l = case l of
459 ((HHeadPtr :=> h) : t) -> FHCons h . rebuildSortedFHList . map (\(HTailPtr p :=> v) -> p :=> v) $ t
460 _ -> error "rebuildSortedFHList{h':t}: non-empty list with HHeadPtr expected"
461 rebuildSortedHList l = case l of
462 ((HHeadPtr :=> Identity h) : t) -> HCons h . rebuildSortedHList . map (\(HTailPtr p :=> v) -> p :=> v) $ t
463 _ -> error "rebuildSortedHList{h':t}: non-empty list with HHeadPtr expected"
465dmapToHList :: forall l. RebuildSortedHList l => DMap (HListPtr l) Identity -> HList l
466dmapToHList = rebuildSortedHList . DMap.toList
468-- | Collect a hetereogeneous list whose elements are all 'Dynamic's into a
469-- single 'Dynamic' whose value represents the current values of all of the
471distributeFHListOverDynPure :: (Reflex t, RebuildSortedHList l) => FHList (Dynamic t) l -> Dynamic t (HList l)
472distributeFHListOverDynPure l = fmap dmapToHList $ distributeDMapOverDynPure $ fhlistToDMap l
474-- | Indicates that all elements in a type-level list are applications of the
476class AllAreFunctors (f :: a -> *) (l :: [a]) where
477 type FunctorList f l :: [*]
478 toFHList :: HList (FunctorList f l) -> FHList f l
479 fromFHList :: FHList f l -> HList (FunctorList f l)
481instance AllAreFunctors f '[] where
482 type FunctorList f '[] = '[]
483 toFHList l = case l of
485#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
486 _ -> error "toFHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
488 fromFHList FHNil = HNil
490instance AllAreFunctors f t => AllAreFunctors f (h ': t) where
491 type FunctorList f (h ': t) = f h ': FunctorList f t
492 toFHList l = case l of
493 a `HCons` b -> a `FHCons` toFHList b
494#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
495 _ -> error "toFHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
497 fromFHList (a `FHCons` b) = a `HCons` fromFHList b
499-- | Convert a datastructure whose constituent parts are all 'Dynamic's into a
500-- single 'Dynamic' whose value represents all the current values of the input's
501-- consitutent 'Dynamic's.
502collectDynPure :: ( RebuildSortedHList (HListElems b)
503 , IsHList a, IsHList b
504 , AllAreFunctors (Dynamic t) (HListElems b)
506 , HListElems a ~ FunctorList (Dynamic t) (HListElems b)
507 ) => a -> Dynamic t b
508collectDynPure ds = fmap fromHList $ distributeFHListOverDynPure $ toFHList $ toHList ds
510-- | Poor man's 'Generic's for product types only.
512 type HListElems a :: [*]
513 toHList :: a -> HList (HListElems a)
514 fromHList :: HList (HListElems a) -> a
516instance IsHList (a, b) where
517 type HListElems (a, b) = [a, b]
518 toHList (a, b) = hBuild a b
519 fromHList l = case l of
520 a `HCons` b `HCons` HNil -> (a, b)
521#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
522 _ -> error "fromHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
525instance IsHList (a, b, c, d) where
526 type HListElems (a, b, c, d) = [a, b, c, d]
527 toHList (a, b, c, d) = hBuild a b c d
528 fromHList l = case l of
529 a `HCons` b `HCons` c `HCons` d `HCons` HNil -> (a, b, c, d)
530#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
531 _ -> error "fromHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
534instance IsHList (a, b, c, d, e, f) where
535 type HListElems (a, b, c, d, e, f) = [a, b, c, d, e, f]
536 toHList (a, b, c, d, e, f) = hBuild a b c d e f
537 fromHList l = case l of
538 a `HCons` b `HCons` c `HCons` d `HCons` e `HCons` f `HCons` HNil -> (a, b, c, d, e, f)
539#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
540 _ -> error "fromHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
543--------------------------------------------------------------------------------
544-- Deprecated functions
545--------------------------------------------------------------------------------
547-- | Map a function over a 'Dynamic'.
548{-# DEPRECATED mapDyn "Use 'return . fmap f' instead of 'mapDyn f'; consider eliminating monadic style" #-}
549mapDyn :: (Reflex t, Monad m) => (a -> b) -> Dynamic t a -> m (Dynamic t b)
550mapDyn f = return . fmap f
552-- | Flipped version of 'mapDyn'.
553{-# DEPRECATED forDyn "Use 'return . ffor a' instead of 'forDyn a'; consider eliminating monadic style" #-}
554forDyn :: (Reflex t, Monad m) => Dynamic t a -> (a -> b) -> m (Dynamic t b)
555forDyn a = return . ffor a
557-- | Split the 'Dynamic' into two 'Dynamic's, each taking the respective value
559{-# DEPRECATED splitDyn "Use 'return . splitDynPure' instead; consider eliminating monadic style" #-}
560splitDyn :: (Reflex t, Monad m) => Dynamic t (a, b) -> m (Dynamic t a, Dynamic t b)
561splitDyn = return . splitDynPure
563-- | Merge the 'Dynamic' values using their 'Monoid' instance.
564{-# DEPRECATED mconcatDyn "Use 'return . mconcat' instead; consider eliminating monadic style" #-}
565mconcatDyn :: forall t m a. (Reflex t, Monad m, Monoid a) => [Dynamic t a] -> m (Dynamic t a)
566mconcatDyn = return . mconcat
568-- | This function no longer needs to be monadic; see 'distributeMapOverDynPure'.
569{-# DEPRECATED distributeDMapOverDyn "Use 'return . distributeDMapOverDynPure' instead; consider eliminating monadic style" #-}
570distributeDMapOverDyn :: (Reflex t, Monad m, GCompare k) => DMap k (Dynamic t) -> m (Dynamic t (DMap k Identity))
571distributeDMapOverDyn = return . distributeDMapOverDynPure
573-- | Merge two 'Dynamic's into a new one using the provided function. The new
574-- 'Dynamic' changes its value each time one of the original 'Dynamic's changes
576{-# DEPRECATED combineDyn "Use 'return (zipDynWith f a b)' instead of 'combineDyn f a b'; consider eliminating monadic style" #-}
577combineDyn :: forall t m a b c. (Reflex t, Monad m) => (a -> b -> c) -> Dynamic t a -> Dynamic t b -> m (Dynamic t c)
578combineDyn f a b = return $ zipDynWith f a b
580-- | A psuedo applicative version of ap for 'Dynamic'. Example useage:
583-- > person <- Person `mapDyn` dynFirstName
584-- > `apDyn` dynListName
586-- > `apDyn` dynAddress
587{-# DEPRECATED apDyn "Use 'ffor m (<*> a)' instead of 'apDyn m a'; consider eliminating monadic style, since Dynamics are now Applicative and can be used with applicative style directly" #-}
588#ifdef USE_TEMPLATE_HASKELL
589{-# ANN apDyn "HLint: ignore Use fmap" #-}
591apDyn :: forall t m a b. (Reflex t, Monad m)
592 => m (Dynamic t (a -> b))
595apDyn m a = fmap (<*> a) m
597--TODO: The pattern of using hold (sample b0) can be reused in various places as a safe way of building certain kinds of Dynamics; see if we can factor this out
598-- | This function no longer needs to be monadic, so it has been replaced by
599-- 'demuxed', which is pure.
600{-# DEPRECATED getDemuxed "Use 'return . demuxed d' instead of 'getDemuxed d'; consider eliminating monadic style" #-}
601getDemuxed :: (Reflex t, Monad m, Eq k) => Demux t k -> k -> m (Dynamic t Bool)
602getDemuxed d = return . demuxed d
604-- | This function no longer needs to be monadic, so it has been replaced by
605-- 'distributeFHListOverDynPure', which is pure.
606{-# DEPRECATED distributeFHListOverDyn "Use 'return . distributeFHListOverDynPure' instead; consider eliminating monadic style" #-}
607distributeFHListOverDyn :: forall t m l. (Reflex t, Monad m, RebuildSortedHList l) => FHList (Dynamic t) l -> m (Dynamic t (HList l))
608distributeFHListOverDyn = return . distributeFHListOverDynPure
610-- | This function no longer needs to be monadic, so it has been replaced by
611-- 'collectDynPure', which is pure.
612{-# DEPRECATED collectDyn "Use 'return . collectDynPure' instead; consider eliminating monadic style" #-}
613collectDyn :: ( RebuildSortedHList (HListElems b)
614 , IsHList a, IsHList b
615 , AllAreFunctors (Dynamic t) (HListElems b)
617 , HListElems a ~ FunctorList (Dynamic t) (HListElems b)
618 ) => a -> m (Dynamic t b)
619collectDyn = return . collectDynPure
621-- | This function has been renamed to 'tagPromptlyDyn' to clarify its
623{-# DEPRECATED tagDyn "Use 'tagPromptlyDyn' instead" #-}
624tagDyn :: Reflex t => Dynamic t a -> Event t b -> Event t a
625tagDyn = tagPromptlyDyn
627-- | This function has been renamed to 'attachPromptlyDyn' to clarify its
629{-# DEPRECATED attachDyn "Use 'attachPromptlyDyn' instead" #-}
630attachDyn :: Reflex t => Dynamic t a -> Event t b -> Event t (a, b)
631attachDyn = attachPromptlyDyn
633-- | This function has been renamed to 'attachPromptlyDynWith' to clarify its
635{-# DEPRECATED attachDynWith "Use 'attachPromptlyDynWith' instead" #-}
636attachDynWith :: Reflex t => (a -> b -> c) -> Dynamic t a -> Event t b -> Event t c
637attachDynWith = attachPromptlyDynWith
639-- | This function has been renamed to 'attachPromptlyDynWithMaybe' to clarify
641{-# DEPRECATED attachDynWithMaybe "Use 'attachPromptlyDynWithMaybe' instead" #-}
642attachDynWithMaybe :: Reflex t => (a -> b -> Maybe c) -> Dynamic t a -> Event t b -> Event t c
643attachDynWithMaybe = attachPromptlyDynWithMaybe
645-- | Combine an inner and outer 'Dynamic' such that the resulting 'Dynamic''s
646-- current value will always be equal to the current value's current value, and
647-- will change whenever either the inner or the outer (or both) values change.
648{-# DEPRECATED joinDyn "Use 'join' instead" #-}
649joinDyn :: Reflex t => Dynamic t (Dynamic t a) -> Dynamic t a
652-- | WARNING: This function is only pure if @a@'s 'Eq' instance tests
653-- representational equality. Use 'holdUniqDyn' instead, which is pure in all
654-- circumstances. Also, note that, unlike 'nub', this function does not prevent
655-- all recurrences of a value, only consecutive recurrences.
656{-# DEPRECATED nubDyn "Use 'holdUniqDyn' instead; note that it returns a MonadHold action rather than a pure Dynamic" #-}
657nubDyn :: (Reflex t, Eq a) => Dynamic t a -> Dynamic t a
660-- | WARNING: This function is only pure if @a@'s 'Eq' instance tests
661-- representational equality. Use 'holdUniqDyn' instead, which is pure in all
663{-# DEPRECATED uniqDyn "Use 'holdUniqDyn' instead; note that it returns a MonadHold action rather than a pure Dynamic" #-}
664uniqDyn :: (Reflex t, Eq a) => Dynamic t a -> Dynamic t a
665uniqDyn = uniqDynBy (==)
667-- | WARNING: This function is impure. Use 'holdUniqDynBy' instead.
668{-# DEPRECATED uniqDynBy "Use 'holdUniqDynBy' instead; note that it returns a MonadHold action rather than a pure Dynamic" #-}
669uniqDynBy :: Reflex t => (a -> a -> Bool) -> Dynamic t a -> Dynamic t a
671 let e' = attachWithMaybe (\x x' -> if x' `eq` x then Nothing else Just x') (current d) (updated d)
672 in unsafeDynamic (current d) e'
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list