1{-# LANGUAGE DeriveFunctor #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE LambdaCase #-}
5{-# LANGUAGE RankNTypes #-}
6{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE UndecidableInstances #-}
10import Control.Monad.Fix
12import Data.AppendMap () -- for the Align instance
13import qualified Data.AppendMap as AMap
14import Data.Functor.Misc
16import qualified Data.Map as Map
17import Data.Map.Monoidal (MonoidalMap)
22import Reflex.Patch.MapWithMove
25newtype MyQuery = MyQuery SelectedCount
26 deriving (Show, Read, Eq, Ord, Monoid, Semigroup, Additive, Group)
28instance Query MyQuery where
29 type QueryResult MyQuery = ()
32instance (Ord k, Query a, Eq (QueryResult a), Align (MonoidalMap k)) => Query (Selector k a) where
33 type QueryResult (Selector k a) = Selector k (QueryResult a)
36newtype Selector k a = Selector { unSelector :: MonoidalMap k a }
37 deriving (Show, Read, Eq, Ord, Functor)
39instance (Ord k, Eq a, Monoid a, Align (MonoidalMap k)) => Semigroup (Selector k a) where
40 (Selector a) <> (Selector b) = Selector $ fmapMaybe id $ f a b
47 in if z == mempty then Nothing else Just z
49instance (Ord k, Eq a, Monoid a, Align (MonoidalMap k)) => Monoid (Selector k a) where
50 mempty = Selector AMap.empty
53instance (Eq a, Ord k, Group a, Align (MonoidalMap k)) => Group (Selector k a) where
54 negateG = fmap negateG
56instance (Eq a, Ord k, Group a, Align (MonoidalMap k)) => Additive (Selector k a)
60 [0, 1, 1, 0] <- fmap (map fst . concat) $
61 runApp (testQueryT testRunWithReplace) () $ map (Just . That) $
62 [ That (), This (), That () ]
63 [0, 1, 1, 0] <- fmap (map fst . concat) $
64 runApp (testQueryT testSequenceDMapWithAdjust) () $ map (Just . That) $
65 [ That (), This (), That () ]
66 [0, 1, 1, 0] <- fmap (map fst . concat) $
67 runApp (testQueryT testSequenceDMapWithAdjustWithMove) () $ map (Just . That) $
68 [ That (), This (), That () ]
71testQueryT :: (Reflex t, MonadFix m)
72 => (Event t () -> Event t () -> QueryT t (Selector Int MyQuery) m ())
73 -> AppIn t () (These () ())
74 -> m (AppOut t Int Int)
75testQueryT w (AppIn _ pulse) = do
76 let replace = fmapMaybe (^? here) pulse
77 increment = fmapMaybe (^? there) pulse
78 (_, q) <- runQueryT (w replace increment) $ pure mempty
79 let qDyn = head . AMap.keys . unSelector <$> incrementalToDynamic q
81 { _appOut_behavior = current qDyn
82 , _appOut_event = updated qDyn
85testRunWithReplace :: ( Reflex t
89 , MonadQuery t (Selector Int MyQuery) m)
93testRunWithReplace replace increment = do
96 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
97 _ <- runWithReplace w $ w <$ replace
100testSequenceDMapWithAdjust :: ( Reflex t
104 , MonadQuery t (Selector Int MyQuery) m)
108testSequenceDMapWithAdjust replace increment = do
109 _ <- listHoldWithKey (Map.singleton () ()) (Map.singleton () (Just ()) <$ replace) $ \_ _ -> do
111 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
114testSequenceDMapWithAdjustWithMove :: ( Reflex t
118 , MonadQuery t (Selector Int MyQuery) m)
122testSequenceDMapWithAdjustWithMove replace increment = do
123 _ <- listHoldWithKeyWithMove (Map.singleton () ()) (Map.singleton () (Just ()) <$ replace) $ \_ _ -> do
125 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
128-- scam it out to test traverseDMapWithAdjustWithMove
129listHoldWithKeyWithMove :: forall t m k v a. (Ord k, MonadHold t m, Adjustable t m) => Map k v -> Event t (Map k (Maybe v)) -> (k -> v -> m a) -> m (Dynamic t (Map k a))
130listHoldWithKeyWithMove m0 m' f = do
131 (n0, n') <- mapMapWithAdjustWithMove f m0 $ ffor m' $ PatchMapWithMove . Map.map (\v -> NodeInfo (maybe From_Delete From_Insert v) Nothing)
132 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