on develop help sign in
Distribute more generally
commit5feea4250ea637e6c1514f0600036156e3b022b9 authorDavid Feuer <David.Feuer@gmail.com> dateSat 13 Jul 2019, 15:54 (7 years ago) parent8d8ceec
1 file changed+14−4
src/Reflex/Class.hs+14−4
src/Reflex/Class.hs+14−4
@@ -88,6 +88,7 @@ module Reflex.Class
8888 , gate
8989 -- ** Combining 'Dynamic's
9090 , distributeDMapOverDynPure
91+ , distributeDMapOverDynPureG
9192 , distributeListOverDyn
9293 , distributeListOverDynWith
9394 , zipDyn
@@ -1091,12 +1092,21 @@ instance (Reflex t, Monoid a) => Monoid (Dynamic t a) where
10911092-- 'Dynamic' 'DMap'. Its implementation is more efficient than doing the same
10921093-- through the use of multiple uses of 'zipDynWith' or 'Applicative' operators.
10931094distributeDMapOverDynPure :: forall t k. (Reflex t, GCompare k) => DMap k (Dynamic t) -> Dynamic t (DMap k Identity)
1094−distributeDMapOverDynPure dm = case DMap.toList dm of
1095+distributeDMapOverDynPure = distributeDMapOverDynPureG coerceDynamic
1096+
1097+-- | This function converts a 'DMap' whose elements are 'Dynamic's into a
1098+-- 'Dynamic' 'DMap'. Its implementation is more efficient than doing the same
1099+-- through the use of multiple uses of 'zipDynWith' or 'Applicative' operators.
1100+distributeDMapOverDynPureG
1101+ :: forall t k q v. (Reflex t, GCompare k)
1102+ => (forall a. q a -> Dynamic t (v a))
1103+ -> DMap k q -> Dynamic t (DMap k v)
1104+distributeDMapOverDynPureG nt dm = case DMap.toList dm of
10951105 [] -> constDyn DMap.empty
1096− [k :=> v] -> fmap (DMap.singleton k . Identity) v
1106+ [k :=> v] -> DMap.singleton k <$> nt v
10971107 _ ->
1098− let getInitial = DMap.traverseWithKey (\_ -> fmap Identity . sample . current) dm
1099− edmPre = merge $ DMap.map updated dm
1108+ let getInitial = DMap.traverseWithKey (\_ -> sample . current . nt) dm
1109+ edmPre = mergeG getCompose $ DMap.map (Compose . updated . nt) dm
11001110 result = unsafeBuildDynamic getInitial $ flip pushAlways edmPre $ \news -> do
11011111 olds <- sample $ current result
11021112 return $ DMap.unionWithKey (\_ _ new -> new) olds news