@@ -39,6 +39,8 @@ module Reflex.Dynamic
39 , traceDynWith
39 , traceDynWith
40 , splitDynPure
40 , splitDynPure
41 , distributeDMapOverDynPure
41 , distributeDMapOverDynPure
42+ , distributeMapOverDynPure
43+ , distributeListOverDynPure
@@ -151,16 +153,25 @@ switchPromptlyDyn de =
151splitDynPure :: Reflex t => Dynamic t (a, b) -> (Dynamic t a, Dynamic t b)
153splitDynPure :: Reflex t => Dynamic t (a, b) -> (Dynamic t a, Dynamic t b)
152splitDynPure d = (fmap fst d, fmap snd d)
154splitDynPure d = (fmap fst d, fmap snd d)
154−-- | Create a 'Dynamic' with a 'DMap' of values out of a 'DMap' of 'Dynamic'
156+-- | Convert a 'Map' with 'Dynamic' elements into a 'Dynamic' of a 'Map' with
155−-- values. The resulting 'Dynamic' will change whenever one of the input
156−-- 'Dynamic's changes.
157+-- non-'Dynamic' elements.
158−-- This function's implementation is more efficient than simply using
159−-- 'zipDynWith' or applicative syntax to achieve the same result, especially for
160−-- maps with many elements.
161distributeMapOverDynPure :: (Reflex t, Ord k) => Map k (Dynamic t v) -> Dynamic t (Map k v)
158distributeMapOverDynPure :: (Reflex t, Ord k) => Map k (Dynamic t v) -> Dynamic t (Map k v)
162distributeMapOverDynPure = fmap dmapToMap . distributeDMapOverDynPure . mapWithFunctorToDMap
159distributeMapOverDynPure = fmap dmapToMap . distributeDMapOverDynPure . mapWithFunctorToDMap
161+-- | Convert a list with 'Dynamic' elements into a 'Dynamic' of a list with
162+-- non-'Dynamic' elements, preserving the order of the elements.
163+distributeListOverDynPure :: Reflex t => [Dynamic t v] -> Dynamic t [v]
164+distributeListOverDynPure =
165+ fmap (map fromDSum . DMap.toAscList) .
166+ distributeDMapOverDynPure .
167+ DMap.fromDistinctAscList .
168+ zipWith toDSum [0..]
170+ toDSum :: Int -> Dynamic t a -> DSum (Const2 Int a) (Dynamic t)
171+ toDSum k v = Const2 k :=> v
172+ fromDSum :: DSum (Const2 Int a) Identity -> a
173+ fromDSum (Const2 _ :=> Identity v) = v
164--TODO: Generalize this to functors other than Maps
175--TODO: Generalize this to functors other than Maps
165-- | Combine a 'Dynamic' of a 'Map' of 'Dynamic's into a 'Dynamic'
176-- | Combine a 'Dynamic' of a 'Map' of 'Dynamic's into a 'Dynamic'
166-- with the current values of the 'Dynamic's in a map.
177-- with the current values of the 'Dynamic's in a map.