1{-# LANGUAGE DeriveFunctor #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE TypeFamilies #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE LambdaCase #-}
6{-# LANGUAGE OverloadedStrings #-}
7{-# LANGUAGE RankNTypes #-}
10import Control.Monad.Fix
12import Data.AppendMap (AppendMap)
13import qualified Data.AppendMap as AMap
14import Data.Functor.Misc
16import qualified Data.Map as Map
21import Reflex.Patch.MapWithMove
24newtype MyQuery = MyQuery SelectedCount
25 deriving (Show, Read, Eq, Ord, Monoid, Semigroup, Additive, Group)
27instance Query MyQuery where
28 type QueryResult MyQuery = ()
31instance (Ord k, Query a, Eq (QueryResult a)) => Query (Selector k a) where
32 type QueryResult (Selector k a) = Selector k (QueryResult a)
35newtype Selector k a = Selector { unSelector :: AppendMap k a }
36 deriving (Show, Read, Eq, Ord, Functor)
38instance (Ord k, Eq a, Monoid a) => Semigroup (Selector k a) where
39 (Selector a) <> (Selector b) = Selector $ fmapMaybe id $ f a b
46 in if z == mempty then Nothing else Just z
48instance (Ord k, Eq a, Monoid a) => Monoid (Selector k a) where
49 mempty = Selector AMap.empty
52instance (Eq a, Ord k, Group a) => Group (Selector k a) where
53 negateG = fmap negateG
55instance (Eq a, Ord k, Group a) => Additive (Selector k a)
59 [0, 1, 1, 0] <- fmap (map fst . concat) $
60 runApp (testQueryT testRunWithReplace) () $ map (Just . That) $
61 [ That (), This (), That () ]
62 [0, 1, 1, 0] <- fmap (map fst . concat) $
63 runApp (testQueryT testSequenceDMapWithAdjust) () $ map (Just . That) $
64 [ That (), This (), That () ]
65 [0, 1, 1, 0] <- fmap (map fst . concat) $
66 runApp (testQueryT testSequenceDMapWithAdjustWithMove) () $ map (Just . That) $
67 [ That (), This (), That () ]
70testQueryT :: (Reflex t, MonadFix m)
71 => (Event t () -> Event t () -> QueryT t (Selector Int MyQuery) m ())
72 -> AppIn t () (These () ())
73 -> m (AppOut t Int Int)
74testQueryT w (AppIn _ pulse) = do
75 let replace = fmapMaybe (^? here) pulse
76 increment = fmapMaybe (^? there) pulse
77 (_, q) <- runQueryT (w replace increment) $ pure mempty
78 let qDyn = head . AMap.keys . unSelector <$> incrementalToDynamic q
80 { _appOut_behavior = current qDyn
81 , _appOut_event = updated qDyn
84testRunWithReplace :: ( Reflex t
88 , MonadQuery t (Selector Int MyQuery) m)
92testRunWithReplace replace increment = do
95 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
96 _ <- runWithReplace w $ w <$ replace
99testSequenceDMapWithAdjust :: ( Reflex t
103 , MonadQuery t (Selector Int MyQuery) m)
107testSequenceDMapWithAdjust replace increment = do
108 _ <- listHoldWithKey (Map.singleton () ()) (Map.singleton () (Just ()) <$ replace) $ \_ _ -> do
110 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
113testSequenceDMapWithAdjustWithMove :: ( Reflex t
117 , MonadQuery t (Selector Int MyQuery) m)
121testSequenceDMapWithAdjustWithMove replace increment = do
122 _ <- listHoldWithKeyWithMove (Map.singleton () ()) (Map.singleton () (Just ()) <$ replace) $ \_ _ -> do
124 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
127listHoldWithKey :: 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))
128listHoldWithKey m0 m' f = do
129 let dm0 = mapWithFunctorToDMap $ Map.mapWithKey f m0
130 dm' = fmap (PatchDMap . mapWithFunctorToDMap . Map.mapWithKey (\k v -> ComposeMaybe $ fmap (f k) v)) m'
131 (a0, a') <- sequenceDMapWithAdjust dm0 dm'
132 fmap dmapToMap . incrementalToDynamic <$> holdIncremental a0 a'
135-- scam it out to test traverseDMapWithAdjustWithMove
136listHoldWithKeyWithMove :: 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))
137listHoldWithKeyWithMove m0 m' f = do
138 (n0, n') <- mapMapWithAdjustWithMove f m0 $ ffor m' $ PatchMapWithMove . Map.map (\v -> NodeInfo (maybe From_Delete From_Insert v) Nothing)
139 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