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
61 -- Things that probably aren't very useful:
65 , RebuildSortedHList (..)
69 , distributeFHListOverDynPure
72 -- * Deprecated functions
79 , distributeDMapOverDyn
80 , distributeFHListOverDyn
93import Data.Functor.Compose
94import Data.Functor.Misc
97import Control.Applicative ((<*>))
99import Control.Monad.Fix
100import Control.Monad.Identity
102import Data.Dependent.Map (DMap)
103import qualified Data.Dependent.Map as DMap
104import Data.Dependent.Sum (DSum (..))
105import Data.Functor.Product
106import Data.GADT.Compare ((:~:) (..), GCompare (..), GEq (..), GOrdering (..))
109import Data.Monoid hiding (Product)
114-- | Map a sampling function over a 'Dynamic'.
115mapDynM :: 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)
116mapDynM f d = buildDynamic (f =<< sample (current d)) $ pushAlways f (updated d)
118-- | Flipped version of 'mapDynM'
119forDynM :: 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)
120forDynM d f = mapDynM f d
122-- | Create a new 'Dynamic' that only signals changes if the values actually
124holdUniqDyn :: (Reflex t, MonadHold t m, MonadFix m, Eq a) => Dynamic t a -> m (Dynamic t a)
125holdUniqDyn = holdUniqDynBy (==)
127-- | Create a new 'Dynamic' that changes only when the underlying 'Dynamic'
128-- changes and the given function returns 'False' when given both the old and
130holdUniqDynBy :: (Reflex t, MonadHold t m, MonadFix m) => (a -> a -> Bool) -> Dynamic t a -> m (Dynamic t a)
131holdUniqDynBy eq = scanDynMaybe id (\new old -> if new `eq` old then Nothing else Just new)
133-- | Dynamic Maybe that can only update from Nothing to Just or Just to Just (i.e., cannot revert to Nothing)
134improvingMaybe :: (Reflex t, MonadHold t m, MonadFix m) => Dynamic t (Maybe a) -> m (Dynamic t (Maybe a))
135improvingMaybe = scanDynMaybe id (\new _ -> if isJust new then Just new else Nothing)
137-- | Create a 'Dynamic' that accumulates values from another 'Dynamic'. This
138-- function does not force its input 'Dynamic' until the output 'Dynamic' is
140scanDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b) -> (a -> b -> b) -> Dynamic t a -> m (Dynamic t b)
141scanDyn z f = scanDynMaybe z (\a b -> Just $ f a b)
143-- | Like 'scanDyn', but the the accumulator function may decline to update the
144-- result 'Dynamic''s value.
145scanDynMaybe :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b) -> (a -> b -> Maybe b) -> Dynamic t a -> m (Dynamic t b)
146scanDynMaybe z f d = do
147 rec d' <- buildDynamic (z <$> sample (current d)) $ flip push (updated d) $ \a -> do
148 b <- sample $ current d'
152-- | Create a 'Dynamic' using the initial value and change it each time the
153-- 'Event' occurs using a folding function on the previous value and the value
155foldDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> b) -> b -> Event t a -> m (Dynamic t b)
156foldDyn = accumDyn . flip
158-- | Like 'foldDyn', but the combining function is a 'PushM' action, so it
159-- can 'sample' existing 'Behaviors' and 'hold' new ones.
160foldDynM :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t b) -> b -> Event t a -> m (Dynamic t b)
161foldDynM = accumMDyn . flip
163-- | Create a 'Dynamic' using the provided initial value and change it each time
164-- the provided 'Event' occurs, using a function to combine the old value with
165-- the 'Event''s value. If the function returns 'Nothing', the value is not
166-- changed; this is distinct from returning 'Just' the old value, since the
167-- 'Dynamic''s 'updated' 'Event' will fire in the 'Just' case, and will not fire
168-- in the 'Nothing' case.
169foldDynMaybe :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> Maybe b) -> b -> Event t a -> m (Dynamic t b)
170foldDynMaybe = accumMaybeDyn . flip
172-- | Like 'foldDynMaybe', but the combining function is a 'PushM' action, so it
173-- can 'sample' existing 'Behaviors' and 'hold' new ones.
174foldDynMaybeM :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe b)) -> b -> Event t a -> m (Dynamic t b)
175foldDynMaybeM = accumMaybeMDyn . flip
177-- | Create a new 'Dynamic' that counts the occurrences of the 'Event'.
178count :: (Reflex t, MonadHold t m, MonadFix m, Num b) => Event t a -> m (Dynamic t b)
179count e = holdDyn 0 =<< zipListWithEvent const (iterate (+1) 1) e
181-- | Create a new 'Dynamic' using the initial value that flips its
182-- value every time the 'Event' occurs.
183toggle :: (Reflex t, MonadHold t m, MonadFix m) => Bool -> Event t a -> m (Dynamic t Bool)
184toggle = foldDyn (const not)
186-- | Switches to the new 'Event' whenever it receives one. Only the old event is
187-- considered the moment a new one is switched in; the output event will fire at
188-- that moment if only if the old event does.
190-- Prefer this to 'switchPromptlyDyn' where possible. The lack of doing double
191-- work when the outer and (new) inner fires means this imposes fewer "timing
192-- requirements" and thus is far more easy to use without introducing fresh
193-- failure cases. 'switchDyn' is also more performant.
194switchDyn :: forall t a. Reflex t => Dynamic t (Event t a) -> Event t a
195switchDyn d = switch (current d)
197-- | Switches to the new 'Event' whenever it receives one. Switching occurs
198-- *before* the inner 'Event' fires - so if the 'Dynamic' changes and both the
199-- old and new inner Events fire simultaneously, the output will fire with the
200-- value of the *new* 'Event'.
202-- Prefer 'switchDyn' to this where possible. The timing requirements that
203-- switching before imposes are likely to bring down your app unless you are
204-- very careful. 'switchDyn' is also more performant.
205switchPromptlyDyn :: forall t a. Reflex t => Dynamic t (Event t a) -> Event t a
206switchPromptlyDyn de =
207 let eLag = switch $ current de
208 eCoincidences = coincidence $ updated de
209 in leftmost [eCoincidences, eLag]
211-- | Split a 'Dynamic' pair into a pair of 'Dynamic's
212splitDynPure :: Reflex t => Dynamic t (a, b) -> (Dynamic t a, Dynamic t b)
213splitDynPure d = (fmap fst d, fmap snd d)
215-- | Convert a 'Map' with 'Dynamic' elements into a 'Dynamic' of a 'Map' with
216-- non-'Dynamic' elements.
217distributeMapOverDynPure :: (Reflex t, Ord k) => Map k (Dynamic t v) -> Dynamic t (Map k v)
218distributeMapOverDynPure = fmap dmapToMap . distributeDMapOverDynPure . mapWithFunctorToDMap
220-- | Convert a list with 'Dynamic' elements into a 'Dynamic' of a list with
221-- non-'Dynamic' elements, preserving the order of the elements.
222distributeListOverDynPure :: Reflex t => [Dynamic t v] -> Dynamic t [v]
223distributeListOverDynPure =
224 fmap (map fromDSum . DMap.toAscList) .
225 distributeDMapOverDynPure .
226 DMap.fromDistinctAscList .
229 toDSum :: Int -> Dynamic t a -> DSum (Const2 Int a) (Dynamic t)
230 toDSum k v = Const2 k :=> v
231 fromDSum :: DSum (Const2 Int a) Identity -> a
232 fromDSum (Const2 _ :=> Identity v) = v
234--TODO: Generalize this to functors other than Maps
235-- | Combine a 'Dynamic' of a 'Map' of 'Dynamic's into a 'Dynamic'
236-- with the current values of the 'Dynamic's in a map.
237joinDynThroughMap :: forall t k a. (Reflex t, Ord k) => Dynamic t (Map k (Dynamic t a)) -> Dynamic t (Map k a)
238joinDynThroughMap = joinDyn . fmap distributeMapOverDynPure
240-- | Print the value of the 'Dynamic' when it is first read and on each
241-- subsequent change that is observed (as traceEvent), prefixed with the
242-- provided string. This should /only/ be used for debugging.
244-- Note: Just like Debug.Trace.trace, the value will only be shown if something
245-- else in the system is depending on it.
246traceDyn :: (Reflex t, Show a) => String -> Dynamic t a -> Dynamic t a
247traceDyn s = traceDynWith $ \x -> s <> ": " <> show x
249-- | Print the result of applying the provided function to the value
250-- of the 'Dynamic' when it is first read and on each subsequent change
251-- that is observed (as traceEvent). This should /only/ be used for
254-- Note: Just like Debug.Trace.trace, the value will only be shown if something
255-- else in the system is depending on it.
256traceDynWith :: Reflex t => (a -> String) -> Dynamic t a -> Dynamic t a
258 let e' = traceEventWith f $ updated d
260 x <- sample $ current d
261 trace (f x) $ return x
262 in unsafeBuildDynamic getV0 e'
264-- | Replace the value of the 'Event' with the current value of the 'Dynamic'
265-- each time the 'Event' occurs.
267-- Note: `tagPromptlyDyn d e` differs from `tag (current d) e` in the case that `e` is firing
268-- at the same time that `d` is changing. With `tagPromptlyDyn d e`, the *new* value of `d`
269-- will replace the value of `e`, whereas with `tag (current d) e`, the *old* value
270-- will be used, since the 'Behavior' won't be updated until the end of the frame.
271-- Additionally, this means that the output 'Event' may not be used to directly change
272-- the input 'Dynamic', because that would mean its value depends on itself. When creating
273-- cyclic data flows, generally `tag (current d) e` is preferred.
274tagPromptlyDyn :: Reflex t => Dynamic t a -> Event t b -> Event t a
275tagPromptlyDyn = attachPromptlyDynWith const
277-- | Attach the current value of the 'Dynamic' to the value of the
278-- 'Event' each time it occurs.
280-- Note: `attachPromptlyDyn d` is not the same as `attach (current d)`. See 'tagPromptlyDyn' for details.
281attachPromptlyDyn :: Reflex t => Dynamic t a -> Event t b -> Event t (a, b)
282attachPromptlyDyn = attachPromptlyDynWith (,)
284-- | Combine the current value of the 'Dynamic' with the value of the
285-- 'Event' each time it occurs.
287-- Note: `attachPromptlyDynWith f d` is not the same as `attachWith f (current d)`. See 'tagPromptlyDyn' for details.
288attachPromptlyDynWith :: Reflex t => (a -> b -> c) -> Dynamic t a -> Event t b -> Event t c
289attachPromptlyDynWith f = attachPromptlyDynWithMaybe $ \a b -> Just $ f a b
291-- | Create a new 'Event' by combining the value at each occurrence with the
292-- current value of the 'Dynamic' value and possibly filtering if the combining
293-- function returns 'Nothing'.
295-- Note: `attachPromptlyDynWithMaybe f d` is not the same as `attachWithMaybe f
296-- (current d)`. See 'tagPromptlyDyn' for details.
297attachPromptlyDynWithMaybe :: Reflex t => (a -> b -> Maybe c) -> Dynamic t a -> Event t b -> Event t c
298attachPromptlyDynWithMaybe f d e =
299 let e' = attach (current d) e
300 in fforMaybe (align e' $ updated d) $ \case
301 This (a, b) -> f a b -- Only the tagging event is firing, so use that
302 These (_, b) a -> f a b -- Both events are firing, so use the newer value
303 That _ -> Nothing -- The tagging event isn't firing, so don't fire
305-- | Factor a @Dynamic t (Maybe a)@ into a @Dynamic t (Maybe (Dynamic t a))@,
306-- such that the outer 'Dynamic' is updated only when the 'Maybe''s constructor
307-- chages from 'Nothing' to 'Just' or vice-versa. Whenever the constructor
308-- becomes 'Just', an inner 'Dynamic' will be provided, whose value will track
309-- the 'a' inside the 'Just'; when the constructor becomes 'Nothing', the
310-- existing inner 'Dynamic' will become constant, and will not change when the
311-- outer constructor changes back to 'Nothing'.
312maybeDyn :: forall t a m. (Reflex t, MonadFix m, MonadHold t m) => Dynamic t (Maybe a) -> m (Dynamic t (Maybe (Dynamic t a)))
313maybeDyn = fmap (fmap unpack) . eitherDyn . fmap pack
321eitherDyn :: 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)))
322eitherDyn = fmap (fmap unpack) . factorDyn . fmap eitherToDSum
323 where unpack :: DSum (EitherTag a b) (Compose (Dynamic t) Identity) -> Either (Dynamic t a) (Dynamic t b)
325 LeftTag :=> Compose a -> Left $ coerceDynamic a
326 RightTag :=> Compose b -> Right $ coerceDynamic b
328factorDyn :: forall t m k v. (Reflex t, MonadFix m, MonadHold t m, GEq k) => Dynamic t (DSum k v) -> m (Dynamic t (DSum k (Compose (Dynamic t) v)))
330 k0 :=> v0 <- sample $ current d --TODO: Figure out how to get this to run inside something like the first argument to buildDynamic
331 (initialEvent, update) <- factorEvent k0 $ updated d
332 initialD <- holdDyn v0 initialEvent
333 let f (k :=> Pair v (Compose v')) = do
335 pure $ k :=> Compose d'
336 holdDyn (k0 :=> Compose initialD) $ pushAlwaysCheap f update
338--------------------------------------------------------------------------------
340--------------------------------------------------------------------------------
342-- | Represents a time changing value together with an 'EventSelector' that can
343-- efficiently detect when the underlying 'Dynamic' has a particular value.
344-- This is useful for representing data like the current selection of a long
349-- > demuxed (demux d) k === fmap (== k) d
351-- However, the when getDemuxed is used multiple times, the complexity is only
352-- /O(log(n))/, rather than /O(n)/ for fmap.
353data Demux t k = Demux { demuxValue :: Behavior t k
354 , demuxSelector :: EventSelector t (Const2 k Bool)
357-- | Demultiplex an input value to a 'Demux' with many outputs. At any given
358-- time, whichever output is indicated by the given 'Dynamic' will be 'True'.
359demux :: (Reflex t, Ord k) => Dynamic t k -> Demux t k
360demux k = Demux (current k)
361 (fan $ attachWith (\k0 k1 -> if k0 == k1
363 else DMap.fromList [Const2 k0 :=> Identity False,
364 Const2 k1 :=> Identity True])
365 (current k) (updated k))
367-- | Select a particular output of the 'Demux'; this is equivalent to (but much
368-- faster than) mapping over the original 'Dynamic' and checking whether it is
369-- equal to the given key.
370demuxed :: (Reflex t, Eq k) => Demux t k -> k -> Dynamic t Bool
372 let e = select (demuxSelector d) (Const2 k)
373 in unsafeBuildDynamic (fmap (==k) $ sample $ demuxValue d) e
375--------------------------------------------------------------------------------
377--------------------------------------------------------------------------------
379--TODO: This whole section is badly in need of cleanup
381-- | A heterogeneous list whose type and length are fixed statically. This is
382-- reproduced from the 'HList' package due to integration issues, and because
383-- very little other functionality from that library is needed.
384data HList (l::[*]) where
386 HCons :: e -> HList l -> HList (e ': l)
390type family HRevApp (l1 :: [k]) (l2 :: [k]) :: [k]
391type instance HRevApp '[] l = l
392type instance HRevApp (e ': l) l' = HRevApp l (e ': l')
394hRevApp :: HList l1 -> HList l2 -> HList (HRevApp l1 l2)
396hRevApp (HCons x l) l' = hRevApp l (HCons x l')
398hReverse :: HList l -> HList (HRevApp l '[])
399hReverse l = hRevApp l HNil
401hBuild :: (HBuild' '[] r) => r
404class HBuild' l r where
405 hBuild' :: HList l -> r
407instance (l' ~ HRevApp l '[])
408 => HBuild' l (HList l') where
411instance HBuild' (a ': l) r
412 => HBuild' l (a->r) where
413 hBuild' l x = hBuild' (HCons x l)
415-- | Like 'HList', but with a functor wrapping each element.
417 FHNil :: FHList f '[]
418 FHCons :: f e -> FHList f l -> FHList f (e ': l)
420instance GEq (HListPtr l) where
421 HHeadPtr `geq` HHeadPtr = Just Refl
422 HHeadPtr `geq` HTailPtr _ = Nothing
423 HTailPtr _ `geq` HHeadPtr = Nothing
424 HTailPtr a `geq` HTailPtr b = a `geq` b
426instance GCompare (HListPtr l) where -- Warning: This ordering can't change, dmapTo*HList will break
427 HHeadPtr `gcompare` HHeadPtr = GEQ
428 HHeadPtr `gcompare` HTailPtr _ = GLT
429 HTailPtr _ `gcompare` HHeadPtr = GGT
430 HTailPtr a `gcompare` HTailPtr b = a `gcompare` b
432-- | A typed index into a typed heterogeneous list.
433data HListPtr l a where
434 HHeadPtr :: HListPtr (h ': t) h
435 HTailPtr :: HListPtr t a -> HListPtr (h ': t) a
437deriving instance Eq (HListPtr l a)
438deriving instance Ord (HListPtr l a)
440fhlistToDMap :: forall (f :: * -> *) l. FHList f l -> DMap (HListPtr l) f
441fhlistToDMap = DMap.fromList . go
442 where go :: forall l'. FHList f l' -> [DSum (HListPtr l') f]
445 FHCons h t -> (HHeadPtr :=> h) : map (\(p :=> v) -> HTailPtr p :=> v) (go t)
447-- | This class allows 'HList's and 'FHlist's to be built from regular lists;
448-- they must be contiguous and sorted.
449class RebuildSortedHList l where
450 rebuildSortedFHList :: [DSum (HListPtr l) f] -> FHList f l
451 rebuildSortedHList :: [DSum (HListPtr l) Identity] -> HList l
453instance RebuildSortedHList '[] where
454 rebuildSortedFHList l = case l of
456 _ : _ -> error "rebuildSortedFHList{'[]}: empty list expected"
457 rebuildSortedHList l = case l of
459 _ : _ -> error "rebuildSortedHList{'[]}: empty list expected"
461instance RebuildSortedHList t => RebuildSortedHList (h ': t) where
462 rebuildSortedFHList l = case l of
463 ((HHeadPtr :=> h) : t) -> FHCons h . rebuildSortedFHList . map (\(HTailPtr p :=> v) -> p :=> v) $ t
464 _ -> error "rebuildSortedFHList{h':t}: non-empty list with HHeadPtr expected"
465 rebuildSortedHList l = case l of
466 ((HHeadPtr :=> Identity h) : t) -> HCons h . rebuildSortedHList . map (\(HTailPtr p :=> v) -> p :=> v) $ t
467 _ -> error "rebuildSortedHList{h':t}: non-empty list with HHeadPtr expected"
469dmapToHList :: forall l. RebuildSortedHList l => DMap (HListPtr l) Identity -> HList l
470dmapToHList = rebuildSortedHList . DMap.toList
472-- | Collect a hetereogeneous list whose elements are all 'Dynamic's into a
473-- single 'Dynamic' whose value represents the current values of all of the
475distributeFHListOverDynPure :: (Reflex t, RebuildSortedHList l) => FHList (Dynamic t) l -> Dynamic t (HList l)
476distributeFHListOverDynPure l = fmap dmapToHList $ distributeDMapOverDynPure $ fhlistToDMap l
478-- | Indicates that all elements in a type-level list are applications of the
480class AllAreFunctors (f :: a -> *) (l :: [a]) where
481 type FunctorList f l :: [*]
482 toFHList :: HList (FunctorList f l) -> FHList f l
483 fromFHList :: FHList f l -> HList (FunctorList f l)
485instance AllAreFunctors f '[] where
486 type FunctorList f '[] = '[]
487 toFHList l = case l of
489#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
490 _ -> error "toFHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
492 fromFHList FHNil = HNil
494instance AllAreFunctors f t => AllAreFunctors f (h ': t) where
495 type FunctorList f (h ': t) = f h ': FunctorList f t
496 toFHList l = case l of
497 a `HCons` b -> a `FHCons` toFHList b
498#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
499 _ -> error "toFHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
501 fromFHList (a `FHCons` b) = a `HCons` fromFHList b
503-- | Convert a datastructure whose constituent parts are all 'Dynamic's into a
504-- single 'Dynamic' whose value represents all the current values of the input's
505-- consitutent 'Dynamic's.
506collectDynPure :: ( RebuildSortedHList (HListElems b)
507 , IsHList a, IsHList b
508 , AllAreFunctors (Dynamic t) (HListElems b)
510 , HListElems a ~ FunctorList (Dynamic t) (HListElems b)
511 ) => a -> Dynamic t b
512collectDynPure ds = fmap fromHList $ distributeFHListOverDynPure $ toFHList $ toHList ds
514-- | Poor man's 'Generic's for product types only.
516 type HListElems a :: [*]
517 toHList :: a -> HList (HListElems a)
518 fromHList :: HList (HListElems a) -> a
520instance IsHList (a, b) where
521 type HListElems (a, b) = [a, b]
522 toHList (a, b) = hBuild a b
523 fromHList l = case l of
524 a `HCons` b `HCons` HNil -> (a, b)
525#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
526 _ -> error "fromHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
529instance IsHList (a, b, c, d) where
530 type HListElems (a, b, c, d) = [a, b, c, d]
531 toHList (a, b, c, d) = hBuild a b c d
532 fromHList l = case l of
533 a `HCons` b `HCons` c `HCons` d `HCons` HNil -> (a, b, c, d)
534#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
535 _ -> error "fromHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
538instance IsHList (a, b, c, d, e, f) where
539 type HListElems (a, b, c, d, e, f) = [a, b, c, d, e, f]
540 toHList (a, b, c, d, e, f) = hBuild a b c d e f
541 fromHList l = case l of
542 a `HCons` b `HCons` c `HCons` d `HCons` e `HCons` f `HCons` HNil -> (a, b, c, d, e, f)
543#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
544 _ -> error "fromHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
547--------------------------------------------------------------------------------
548-- Deprecated functions
549--------------------------------------------------------------------------------
551-- | Map a function over a 'Dynamic'.
552{-# DEPRECATED mapDyn "Use 'return . fmap f' instead of 'mapDyn f'; consider eliminating monadic style" #-}
553mapDyn :: (Reflex t, Monad m) => (a -> b) -> Dynamic t a -> m (Dynamic t b)
554mapDyn f = return . fmap f
556-- | Flipped version of 'mapDyn'.
557{-# DEPRECATED forDyn "Use 'return . ffor a' instead of 'forDyn a'; consider eliminating monadic style" #-}
558forDyn :: (Reflex t, Monad m) => Dynamic t a -> (a -> b) -> m (Dynamic t b)
559forDyn a = return . ffor a
561-- | Split the 'Dynamic' into two 'Dynamic's, each taking the respective value
563{-# DEPRECATED splitDyn "Use 'return . splitDynPure' instead; consider eliminating monadic style" #-}
564splitDyn :: (Reflex t, Monad m) => Dynamic t (a, b) -> m (Dynamic t a, Dynamic t b)
565splitDyn = return . splitDynPure
567-- | Merge the 'Dynamic' values using their 'Monoid' instance.
568{-# DEPRECATED mconcatDyn "Use 'return . mconcat' instead; consider eliminating monadic style" #-}
569mconcatDyn :: forall t m a. (Reflex t, Monad m, Monoid a) => [Dynamic t a] -> m (Dynamic t a)
570mconcatDyn = return . mconcat
572-- | This function no longer needs to be monadic; see 'distributeMapOverDynPure'.
573{-# DEPRECATED distributeDMapOverDyn "Use 'return . distributeDMapOverDynPure' instead; consider eliminating monadic style" #-}
574distributeDMapOverDyn :: (Reflex t, Monad m, GCompare k) => DMap k (Dynamic t) -> m (Dynamic t (DMap k Identity))
575distributeDMapOverDyn = return . distributeDMapOverDynPure
577-- | Merge two 'Dynamic's into a new one using the provided function. The new
578-- 'Dynamic' changes its value each time one of the original 'Dynamic's changes
580{-# DEPRECATED combineDyn "Use 'return (zipDynWith f a b)' instead of 'combineDyn f a b'; consider eliminating monadic style" #-}
581combineDyn :: forall t m a b c. (Reflex t, Monad m) => (a -> b -> c) -> Dynamic t a -> Dynamic t b -> m (Dynamic t c)
582combineDyn f a b = return $ zipDynWith f a b
584-- | A psuedo applicative version of ap for 'Dynamic'. Example useage:
587-- > person <- Person `mapDyn` dynFirstName
588-- > `apDyn` dynListName
590-- > `apDyn` dynAddress
591{-# 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" #-}
592#ifdef USE_TEMPLATE_HASKELL
593{-# ANN apDyn "HLint: ignore Use fmap" #-}
595apDyn :: forall t m a b. (Reflex t, Monad m)
596 => m (Dynamic t (a -> b))
599apDyn m a = fmap (<*> a) m
601--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
602-- | This function no longer needs to be monadic, so it has been replaced by
603-- 'demuxed', which is pure.
604{-# DEPRECATED getDemuxed "Use 'return . demuxed d' instead of 'getDemuxed d'; consider eliminating monadic style" #-}
605getDemuxed :: (Reflex t, Monad m, Eq k) => Demux t k -> k -> m (Dynamic t Bool)
606getDemuxed d = return . demuxed d
608-- | This function no longer needs to be monadic, so it has been replaced by
609-- 'distributeFHListOverDynPure', which is pure.
610{-# DEPRECATED distributeFHListOverDyn "Use 'return . distributeFHListOverDynPure' instead; consider eliminating monadic style" #-}
611distributeFHListOverDyn :: forall t m l. (Reflex t, Monad m, RebuildSortedHList l) => FHList (Dynamic t) l -> m (Dynamic t (HList l))
612distributeFHListOverDyn = return . distributeFHListOverDynPure
614-- | This function no longer needs to be monadic, so it has been replaced by
615-- 'collectDynPure', which is pure.
616{-# DEPRECATED collectDyn "Use 'return . collectDynPure' instead; consider eliminating monadic style" #-}
617collectDyn :: ( RebuildSortedHList (HListElems b)
618 , IsHList a, IsHList b
619 , AllAreFunctors (Dynamic t) (HListElems b)
621 , HListElems a ~ FunctorList (Dynamic t) (HListElems b)
622 ) => a -> m (Dynamic t b)
623collectDyn = return . collectDynPure
625-- | This function has been renamed to 'tagPromptlyDyn' to clarify its
627{-# DEPRECATED tagDyn "Use 'tagPromptlyDyn' instead" #-}
628tagDyn :: Reflex t => Dynamic t a -> Event t b -> Event t a
629tagDyn = tagPromptlyDyn
631-- | This function has been renamed to 'attachPromptlyDyn' to clarify its
633{-# DEPRECATED attachDyn "Use 'attachPromptlyDyn' instead" #-}
634attachDyn :: Reflex t => Dynamic t a -> Event t b -> Event t (a, b)
635attachDyn = attachPromptlyDyn
637-- | This function has been renamed to 'attachPromptlyDynWith' to clarify its
639{-# DEPRECATED attachDynWith "Use 'attachPromptlyDynWith' instead" #-}
640attachDynWith :: Reflex t => (a -> b -> c) -> Dynamic t a -> Event t b -> Event t c
641attachDynWith = attachPromptlyDynWith
643-- | This function has been renamed to 'attachPromptlyDynWithMaybe' to clarify
645{-# DEPRECATED attachDynWithMaybe "Use 'attachPromptlyDynWithMaybe' instead" #-}
646attachDynWithMaybe :: Reflex t => (a -> b -> Maybe c) -> Dynamic t a -> Event t b -> Event t c
647attachDynWithMaybe = attachPromptlyDynWithMaybe
649-- | Combine an inner and outer 'Dynamic' such that the resulting 'Dynamic''s
650-- current value will always be equal to the current value's current value, and
651-- will change whenever either the inner or the outer (or both) values change.
652{-# DEPRECATED joinDyn "Use 'join' instead" #-}
653joinDyn :: Reflex t => Dynamic t (Dynamic t a) -> Dynamic t a
656-- | WARNING: This function is only pure if @a@'s 'Eq' instance tests
657-- representational equality. Use 'holdUniqDyn' instead, which is pure in all
658-- circumstances. Also, note that, unlike 'nub', this function does not prevent
659-- all recurrences of a value, only consecutive recurrences.
660{-# DEPRECATED nubDyn "Use 'holdUniqDyn' instead; note that it returns a MonadHold action rather than a pure Dynamic" #-}
661nubDyn :: (Reflex t, Eq a) => Dynamic t a -> Dynamic t a
664-- | WARNING: This function is only pure if @a@'s 'Eq' instance tests
665-- representational equality. Use 'holdUniqDyn' instead, which is pure in all
667{-# DEPRECATED uniqDyn "Use 'holdUniqDyn' instead; note that it returns a MonadHold action rather than a pure Dynamic" #-}
668uniqDyn :: (Reflex t, Eq a) => Dynamic t a -> Dynamic t a
669uniqDyn = uniqDynBy (==)
671-- | WARNING: This function is impure. Use 'holdUniqDynBy' instead.
672{-# DEPRECATED uniqDynBy "Use 'holdUniqDynBy' instead; note that it returns a MonadHold action rather than a pure Dynamic" #-}
673uniqDynBy :: Reflex t => (a -> a -> Bool) -> Dynamic t a -> Dynamic t a
675 let e' = attachWithMaybe (\x x' -> if x' `eq` x then Nothing else Just x') (current d) (updated d)
676 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