Skip to content

Instantly share code, notes, and snippets.

@Elvecent
Created October 25, 2021 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elvecent/96520e4a8fd51a8d6a3aba0753e33a0f to your computer and use it in GitHub Desktop.
Save Elvecent/96520e4a8fd51a8d6a3aba0753e33a0f to your computer and use it in GitHub Desktop.
Reflex collection of counters
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