on develop help sign in
A few more sortableList example improvements
776e2faRyan Trinkle
examples/sortableList.hs+15−15
@@ -23,8 +23,8 @@ import System.Random
23
23
24main :: IO ()
24main :: IO ()
25main = mainWidget $ do
25main = mainWidget $ do
26− let f k (a, b, c) = el "tr" $ do
26+ let f _ (a, b, c) = el "tr" $ do
27− mapM_ (el "td" . text . T.pack . show) [k, a, b, c]
27+ mapM_ (el "td" . text . T.pack . show) [a, b, c]
28 _ <- el "td" $ inputElement def
28 _ <- el "td" $ inputElement def
29 return ()
29 return ()
30 let numKeys = 1000 :: Int
30 let numKeys = 1000 :: Int
@@ -33,15 +33,15 @@ main = mainWidget $ do
33 values = zip3 (randoms (mkStdGen 0)) (randoms (mkStdGen 1)) (randoms (mkStdGen 2))
33 values = zip3 (randoms (mkStdGen 0)) (randoms (mkStdGen 1)) (randoms (mkStdGen 2))
34 testMap = Map.fromList $ zip keys values
34 testMap = Map.fromList $ zip keys values
35 resortButtons = el "div" $ leftmost <$> sequence
35 resortButtons = el "div" $ leftmost <$> sequence
36− [ (comparing (view _1) <$) <$> button "1 ASC"
36+ [ (comparing (view _1) <$) <$> button "A ASC"
37− , (flip (comparing (view _1)) <$) <$> button "1 DESC"
37+ , (flip (comparing (view _1)) <$) <$> button "A DESC"
38− , (comparing (view _2) <$) <$> button "2 ASC"
38+ , (comparing (view _2) <$) <$> button "B ASC"
39− , (flip (comparing (view _2)) <$) <$> button "2 DESC"
39+ , (flip (comparing (view _2)) <$) <$> button "B DESC"
40− , (comparing (view _3) <$) <$> button "3 ASC"
40+ , (comparing (view _3) <$) <$> button "C ASC"
41− , (flip (comparing (view _3)) <$) <$> button "3 DESC"
41+ , (flip (comparing (view _3)) <$) <$> button "C DESC"
42 ]
42 ]
43 el "h1" $ text "Improved list sorting"
43 el "h1" $ text "Improved list sorting"
44− el "p" $ text $ "This app shows a table of " <> T.pack (show numKeys) <> " items, and allows you to re-sort by columns a, b, and c, which are full of pseudorandom numbers."
44+ el "p" $ text $ "This app shows a table of " <> T.pack (show numKeys) <> " rows, and allows you to re-sort by columns a, b, and c, which are full of pseudorandom numbers. Each row also includes an element with internal state (a textbox) to demonstrate that the state is preserved, only when using the new way."
45 el "h3" $ text "Re-sort the list the OLD way (by redrawing everything)"
45 el "h3" $ text "Re-sort the list the OLD way (by redrawing everything)"
46 resortSlow <- resortButtons
46 resortSlow <- resortButtons
47 displayRedrawTime resortSlow
47 displayRedrawTime resortSlow
@@ -51,7 +51,6 @@ main = mainWidget $ do
51 el "hr" blank
51 el "hr" blank
52 el "table" $ do
52 el "table" $ do
53 el "tr" $ do
53 el "tr" $ do
54− el "th" $ text "Key"
55 el "th" $ text "A"
54 el "th" $ text "A"
56 el "th" $ text "B"
55 el "th" $ text "B"
57 el "th" $ text "C"
56 el "th" $ text "C"
@@ -76,13 +75,14 @@ mapMapWithAdjustWithMove f m0 m' = do
76
75
77simpleSortableList :: forall t m k v. (MonadHold t m, MonadFix m, MonadAdjust t m, Ord k) => (k -> v -> m ()) -> Map k v -> Event t (v -> v -> Ordering) -> Event t (v -> v -> Ordering) -> m ()
76simpleSortableList :: forall t m k v. (MonadHold t m, MonadFix m, MonadAdjust t m, Ord k) => (k -> v -> m ()) -> Map k v -> Event t (v -> v -> Ordering) -> Event t (v -> v -> Ordering) -> m ()
78simpleSortableList f m0 resortFunc resortSlowFunc = do
77simpleSortableList f m0 resortFunc resortSlowFunc = do
79− rec let resortPatch = attachWith (flip patchThatSortsMapWith) (currentIncremental m) resortFunc
78+ rec let resortPatchFast = attachWith (flip patchThatSortsMapWith) (currentIncremental m) resortFunc
80 redrawPatch :: Map k v -> (v -> v -> Ordering) -> PatchMapWithMove k v
79 redrawPatch :: Map k v -> (v -> v -> Ordering) -> PatchMapWithMove k v
81 redrawPatch d cmp = unsafePatchMapWithMove $ fmap (MapEdit_Insert False) $ Map.fromList $ zip (Map.keys d) (sortBy cmp $ Map.elems d)
80 redrawPatch d cmp = unsafePatchMapWithMove $ fmap (MapEdit_Insert False) $ Map.fromList $ zip (Map.keys d) (sortBy cmp $ Map.elems d)
82 resortPatchSlow = attachWith redrawPatch (currentIncremental m) resortSlowFunc
81 resortPatchSlow = attachWith redrawPatch (currentIncremental m) resortSlowFunc
82+ resortPatch = leftmost
83+ [ resortPatchFast
84+ , resortPatchSlow
85+ ]
83 m <- holdIncremental m0 resortPatch
86 m <- holdIncremental m0 resortPatch
84− _ <- mapMapWithAdjustWithMove f m0 $ leftmost
87+ _ <- mapMapWithAdjustWithMove f m0 resortPatch
85− [ resortPatch
86− , resortPatchSlow
87− ]
88 return ()
88 return ()