Skip to content

Commit 5feea42

Browse files
committed
Distribute more generally
1 parent 8d8ceec commit 5feea42

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/Reflex/Class.hs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
10931094
distributeDMapOverDynPure :: 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

0 commit comments

Comments
 (0)