on develop help sign in
Add distributeListOverDynWith
27b2558Ali Abrar
src/Reflex/Class.hs+10−1
@@ -674,7 +674,7 @@ zipDynWith f da db =
674 in unsafeBuildDynamic (f <$> sample (current da) <*> sample (current db)) ec
674 in unsafeBuildDynamic (f <$> sample (current da) <*> sample (current db)) ec
675
675
676instance (Reflex t, Monoid a) => Monoid (Dynamic t a) where
676instance (Reflex t, Monoid a) => Monoid (Dynamic t a) where
677− mconcat = fmap (mconcat . map (\(Const2 _ :=> Identity v) -> v) . DMap.toList) . distributeDMapOverDynPure . DMap.fromList . map (\(k, v) -> Const2 k :=> v) . zip [0 :: Int ..]
677+ mconcat = distributeListOverDynWith mconcat
678 mempty = constDyn mempty
678 mempty = constDyn mempty
679 mappend = zipDynWith mappend
679 mappend = zipDynWith mappend
680
680
@@ -693,6 +693,15 @@ distributeDMapOverDynPure dm = case DMap.toList dm of
693 return $ DMap.unionWithKey (\_ _ new -> new) olds news
693 return $ DMap.unionWithKey (\_ _ new -> new) olds news
694 in result
694 in result
695
695
696+-- | Convert a list of 'Dynamic's into a 'Dynamic' list.
697+distributeListOverDyn :: Reflex t => [Dynamic t a] -> Dynamic t [a]
698+distributeListOverDyn = distributeListOverDynWith id
699+
700+-- | Create a new 'Dynamic' by applying a combining function to a list of 'Dynamic's
701+distributeListOverDynWith :: Reflex t => ([a] -> b) -> [Dynamic t a] -> Dynamic t b
702+distributeListOverDynWith f = fmap (f . map (\(Const2 _ :=> Identity v) -> v) . DMap.toList) . distributeDMapOverDynPure . DMap.fromList . map (\(k, v) -> Const2 k :=> v) . zip [0 :: Int ..]
703+
704+
696--------------------------------------------------------------------------------
705--------------------------------------------------------------------------------
697-- Accumulator
706-- Accumulator
698--------------------------------------------------------------------------------
707--------------------------------------------------------------------------------