1{-# LANGUAGE DeriveFunctor #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE TypeFamilies #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE LambdaCase #-}
6{-# LANGUAGE RankNTypes #-}
9import Control.Monad.Fix
11import Data.AppendMap (AppendMap)
12import qualified Data.AppendMap as AMap
13import Data.Functor.Misc
15import qualified Data.Map as Map
20import Reflex.Patch.MapWithMove
23newtype MyQuery = MyQuery SelectedCount
24 deriving (Show, Read, Eq, Ord, Monoid, Semigroup, Additive, Group)
26instance Query MyQuery where
27 type QueryResult MyQuery = ()
30instance (Ord k, Query a, Eq (QueryResult a)) => Query (Selector k a) where
31 type QueryResult (Selector k a) = Selector k (QueryResult a)
34newtype Selector k a = Selector { unSelector :: AppendMap k a }
35 deriving (Show, Read, Eq, Ord, Functor)
37instance (Ord k, Eq a, Monoid a) => Semigroup (Selector k a) where
38 (Selector a) <> (Selector b) = Selector $ fmapMaybe id $ f a b
45 in if z == mempty then Nothing else Just z
47instance (Ord k, Eq a, Monoid a) => Monoid (Selector k a) where
48 mempty = Selector AMap.empty
51instance (Eq a, Ord k, Group a) => Group (Selector k a) where
52 negateG = fmap negateG
54instance (Eq a, Ord k, Group a) => Additive (Selector k a)
58 [0, 1, 1, 0] <- fmap (map fst . concat) $
59 runApp (testQueryT testRunWithReplace) () $ map (Just . That) $
60 [ That (), This (), That () ]
61 [0, 1, 1, 0] <- fmap (map fst . concat) $
62 runApp (testQueryT testSequenceDMapWithAdjust) () $ map (Just . That) $
63 [ That (), This (), That () ]
64 [0, 1, 1, 0] <- fmap (map fst . concat) $
65 runApp (testQueryT testSequenceDMapWithAdjustWithMove) () $ map (Just . That) $
66 [ That (), This (), That () ]
69testQueryT :: (Reflex t, MonadFix m)
70 => (Event t () -> Event t () -> QueryT t (Selector Int MyQuery) m ())
71 -> AppIn t () (These () ())
72 -> m (AppOut t Int Int)
73testQueryT w (AppIn _ pulse) = do
74 let replace = fmapMaybe (^? here) pulse
75 increment = fmapMaybe (^? there) pulse
76 (_, q) <- runQueryT (w replace increment) $ pure mempty
77 let qDyn = head . AMap.keys . unSelector <$> incrementalToDynamic q
79 { _appOut_behavior = current qDyn
80 , _appOut_event = updated qDyn
83testRunWithReplace :: ( Reflex t
87 , MonadQuery t (Selector Int MyQuery) m)
91testRunWithReplace replace increment = do
94 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
95 _ <- runWithReplace w $ w <$ replace
98testSequenceDMapWithAdjust :: ( Reflex t
102 , MonadQuery t (Selector Int MyQuery) m)
106testSequenceDMapWithAdjust replace increment = do
107 _ <- listHoldWithKey (Map.singleton () ()) (Map.singleton () (Just ()) <$ replace) $ \_ _ -> do
109 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
112testSequenceDMapWithAdjustWithMove :: ( Reflex t
116 , MonadQuery t (Selector Int MyQuery) m)
120testSequenceDMapWithAdjustWithMove replace increment = do
121 _ <- listHoldWithKeyWithMove (Map.singleton () ()) (Map.singleton () (Just ()) <$ replace) $ \_ _ -> do
123 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
126listHoldWithKey :: forall t m k v a. (Ord k, MonadHold t m, MonadAdjust t m) => Map k v -> Event t (Map k (Maybe v)) -> (k -> v -> m a) -> m (Dynamic t (Map k a))
127listHoldWithKey m0 m' f = do
128 let dm0 = mapWithFunctorToDMap $ Map.mapWithKey f m0
129 dm' = fmap (PatchDMap . mapWithFunctorToDMap . Map.mapWithKey (\k v -> ComposeMaybe $ fmap (f k) v)) m'
130 (a0, a') <- sequenceDMapWithAdjust dm0 dm'
131 fmap dmapToMap . incrementalToDynamic <$> holdIncremental a0 a'
134-- scam it out to test traverseDMapWithAdjustWithMove
135listHoldWithKeyWithMove :: forall t m k v a. (Ord k, MonadHold t m, MonadAdjust t m) => Map k v -> Event t (Map k (Maybe v)) -> (k -> v -> m a) -> m (Dynamic t (Map k a))
136listHoldWithKeyWithMove m0 m' f = do
137 (n0, n') <- mapMapWithAdjustWithMove f m0 $ ffor m' $ PatchMapWithMove . Map.map (\v -> NodeInfo (maybe From_Delete From_Insert v) Nothing)
138 incrementalToDynamic <$> holdIncremental n0 n'
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list