Created
October 25, 2021 08:37
-
-
Save Elvecent/96520e4a8fd51a8d6a3aba0753e33a0f to your computer and use it in GitHub Desktop.
Reflex collection of counters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data AddRemove = Add Int | Remove Int deriving Show | |
currentId :: AddRemove -> Int | |
currentId = \case | |
Add n -> n | |
Remove n -> n | |
toMap :: AddRemove -> M.Map Int (Maybe ()) | |
toMap = \case | |
Add n -> M.singleton n (Just ()) | |
Remove n -> M.singleton (n + 1) Nothing | |
counter :: _ => m () | |
counter = do | |
down <- button "<" | |
up <- button ">" | |
c <- foldDyn id (0 :: Int) $ leftmost | |
[ (+1) <$ up | |
, (subtract 1) <$ down | |
] | |
dynText (T.pack . show <$> c) | |
main = do | |
add <- button "Add" | |
rem <- button "Remove" | |
listLen <- foldDyn (\case | |
Add n -> \acc -> Add (currentId acc + n) | |
Remove n -> \acc -> Remove (currentId acc - n) | |
) | |
(Add 0) $ leftmost | |
[ Add 1 <$ add | |
, Remove 1 <$ rem | |
] | |
dynText (T.pack . show <$> listLen) | |
el "div" $ void $ | |
listHoldWithKey mempty (toMap <$> updated listLen) (\_ _ -> counter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment