@@ -0,0 +1,121 @@
1+{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE RankNTypes #-}
4+{-# LANGUAGE ScopedTypeVariables #-}
5+{-# LANGUAGE RecursiveDo #-}
11+import Control.Monad.Fix
12+import qualified Data.Dependent.Map as DMap
13+import Data.Dependent.Sum
20+pushDyn :: (Reflex t, MonadHold t m) => (a -> PushM t b) -> Dynamic t a -> m (Dynamic t b)
21+pushDyn f d = buildDynamic (sample (current d) >>= f) (pushAlways f (updated d))
23+foldedDyn :: (Reflex t, MonadHold t m) => (a -> a -> a) -> Dynamic t a -> m (Dynamic t a)
24+foldedDyn f d = fmap join $ flip buildDynamic never $ do
25+ a <- sample (current d)
26+ foldDyn f a (updated d)
28+scannedDyn :: (Reflex t, MonadHold t m) => Dynamic t a -> m (Dynamic t [a])
29+scannedDyn = fmap (fmap reverse) . foldedDyn (<>) . fmap pure
31+scanInnerDyns :: (Reflex t, MonadHold t m) => Dynamic t (Dynamic t a) -> m (Dynamic t [a])
33+ scans <- scannedDyn d
34+ return (join (fmap distributeListOverDynPure scans))
40+ r <- last <$> runAppB testInnerHoldsE (Just <$> [1..4])
41+ -- [[[0,1,2],[1,2],[2]]]]
43+ -- r <- runAppB testScan counter
45+ -- r <- runAppB testStrictness (fmap Just [1..9])
52+ counter = fmap Just [1..9] <> [Nothing]
53+ eithers = fmap Just [ Left 1, Left 2, Right 3, Left 4, Left 5, Right 6, Right 7, Right 8, Left 9 ] <> [Nothing]
55+testIdentity :: (Reflex t, MonadHold t m, MonadFix m)
56+ => Event t Int -> m (Behavior t Int)
59+ d' <- pushDyn return d
60+ d'' <- pushDyn return d'
64+testStrictness :: (Reflex t, MonadHold t m, MonadFix m)
65+ => Event t Int -> m (Behavior t Int)
69+ d'' <- pushDyn return d'
70+ d' <- pushDyn return d
76+testScan :: (Reflex t, MonadHold t m, MonadFix m)
77+ => Event t Int -> m (Behavior t [Int])
81+ return (current scan)
83+testFactor :: (Reflex t, MonadHold t m, MonadFix m)
84+ => Event t (Either Int Int) -> m (Behavior t (Either Int Int))
86+ d <- holdDyn (Left 0) e
88+ eithers <- eitherDyn d
89+ return $ current (join (fmap unFactor eithers))
92+ unFactor = either (fmap Left) (fmap Right)
94+testInnerHolds :: (Reflex t, MonadHold t m, MonadFix m)
95+ => Event t Int -> m (Behavior t [Int])
99+ d' <- pushDyn (\a -> foldDyn (:) [a] (updated d)) d
100+ return $ current (join d')
102+testInnerHoldsE :: forall t m. (Reflex t, MonadHold t m, MonadFix m)
103+ => Event t Int -> m (Behavior t [[Int]])
104+testInnerHoldsE e = do
106+ d <- foldDyn snoc [initial] updates
107+ return $ current (join (fmap distributeListOverDynPure d))
109+ f :: forall n. (MonadHold t n, MonadFix n) => Int -> n (Dynamic t [Int])
110+ f a = foldDyn snoc [a] e
111+ updates = pushAlways f e
113+ snoc x xs = xs ++ [x]
115+testInnerHolds1 :: (Reflex t, MonadHold t m, MonadFix m)
116+ => Event t Int -> m (Behavior t [[Int]])
117+testInnerHolds1 e = do
118+ d <- fmap pure <$> holdDyn 0 e
120+ d' <- pushDyn (\a -> foldDyn (flip mappend) a (updated d)) d
121+ current <$> scanInnerDyns d'