2{-# LANGUAGE DeriveFunctor #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE LambdaCase #-}
6{-# LANGUAGE RankNTypes #-}
7{-# LANGUAGE StandaloneDeriving #-}
8{-# LANGUAGE TypeFamilies #-}
9{-# LANGUAGE UndecidableInstances #-}
12import Control.Monad.Fix
14import qualified Data.AppendMap as AMap
15import Data.Functor.Misc
17import qualified Data.Map as Map
18import Data.Map.Monoidal (MonoidalMap)
22#if defined(MIN_VERSION_these_lens) || (MIN_VERSION_these(0,8,0) && !MIN_VERSION_these(0,9,0))
27import Data.Patch.MapWithMove
30newtype MyQuery = MyQuery SelectedCount
31 deriving (Show, Read, Eq, Ord, Monoid, Semigroup, Additive, Group)
33instance Query MyQuery where
34 type QueryResult MyQuery = ()
37instance (Ord k, Query a, Eq (QueryResult a), Align (MonoidalMap k)) => Query (Selector k a) where
38 type QueryResult (Selector k a) = Selector k (QueryResult a)
41newtype Selector k a = Selector { unSelector :: MonoidalMap k a }
42 deriving (Show, Read, Eq, Ord, Functor)
44#if !(MIN_VERSION_monoidal_containers(0,4,1))
45deriving instance Ord k => Align (MonoidalMap k)
48instance (Ord k, Eq a, Monoid a, Align (MonoidalMap k)) => Semigroup (Selector k a) where
49 (Selector a) <> (Selector b) = Selector $ fmapMaybe id $ f a b
56 in if z == mempty then Nothing else Just z
58instance (Ord k, Eq a, Monoid a, Align (MonoidalMap k)) => Monoid (Selector k a) where
59 mempty = Selector AMap.empty
62instance (Eq a, Ord k, Group a, Align (MonoidalMap k)) => Group (Selector k a) where
63 negateG = fmap negateG
65instance (Eq a, Ord k, Group a, Align (MonoidalMap k)) => Additive (Selector k a)
69 [0, 1, 1, 0] <- fmap (map fst . concat) $
70 runApp (testQueryT testRunWithReplace) () $ map (Just . That) $
71 [ That (), This (), That () ]
72 [0, 1, 1, 0] <- fmap (map fst . concat) $
73 runApp (testQueryT testSequenceDMapWithAdjust) () $ map (Just . That) $
74 [ That (), This (), That () ]
75 [0, 1, 1, 0] <- fmap (map fst . concat) $
76 runApp (testQueryT testSequenceDMapWithAdjustWithMove) () $ map (Just . That) $
77 [ That (), This (), That () ]
80testQueryT :: (Reflex t, MonadFix m)
81 => (Event t () -> Event t () -> QueryT t (Selector Int MyQuery) m ())
82 -> AppIn t () (These () ())
83 -> m (AppOut t Int Int)
84testQueryT w (AppIn _ pulse) = do
85 let replace = fmapMaybe (^? here) pulse
86 increment = fmapMaybe (^? there) pulse
87 (_, q) <- runQueryT (w replace increment) $ pure mempty
88 let qDyn = head . AMap.keys . unSelector <$> incrementalToDynamic q
90 { _appOut_behavior = current qDyn
91 , _appOut_event = updated qDyn
94testRunWithReplace :: ( Reflex t
98 , MonadQuery t (Selector Int MyQuery) m)
102testRunWithReplace replace increment = do
105 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
106 _ <- runWithReplace w $ w <$ replace
109testSequenceDMapWithAdjust :: ( Reflex t
113 , MonadQuery t (Selector Int MyQuery) m)
117testSequenceDMapWithAdjust replace increment = do
118 _ <- listHoldWithKey (Map.singleton () ()) (Map.singleton () (Just ()) <$ replace) $ \_ _ -> do
120 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
123testSequenceDMapWithAdjustWithMove :: ( Reflex t
127 , MonadQuery t (Selector Int MyQuery) m)
131testSequenceDMapWithAdjustWithMove replace increment = do
132 _ <- listHoldWithKeyWithMove (Map.singleton () ()) (Map.singleton () (Just ()) <$ replace) $ \_ _ -> do
134 queryDyn $ zipDynWith (\x y -> Selector (AMap.singleton (x :: Int) y)) n $ pure $ MyQuery $ SelectedCount 1
137-- scam it out to test traverseDMapWithAdjustWithMove
138listHoldWithKeyWithMove :: 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))
139listHoldWithKeyWithMove m0 m' f = do
140 (n0, n') <- mapMapWithAdjustWithMove f m0 $ ffor m' $ PatchMapWithMove . Map.map (\v -> NodeInfo (maybe From_Delete From_Insert v) Nothing)
141 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