Skip to content

Instantly share code, notes, and snippets.

@aratama
Created February 9, 2016 17:23
Show Gist options
  • Save aratama/f40e316be7ad4710fb3a to your computer and use it in GitHub Desktop.
Save aratama/f40e316be7ad4710fb3a to your computer and use it in GitHub Desktop.
module Counter where
import Prelude (Unit(), pure, unit, ($), bind, show, void, (++), (+), (*), (<$>), const, negate, id)
import Data.Functor ((<$))
import Control.Monad.Aff (Aff(), runAff)
import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Exception (throwException)
import Control.Monad.Eff.Class (liftEff)
import Halogen (HalogenEffects(), ComponentDSL(), Natural(), ComponentHTML(), Component(), action, runUI, component, set)
import Halogen.Util (appendToBody, onLoad)
import Halogen.HTML.Indexed (text)
import Signal ((<~), (~>), runSignal, foldp, sampleOn, filter )
import Signal.DOM (CoordinatePair(), mousePos, mouseButton )
import Signal.Time (second, every, now)
type State = Int
data Query a = SetState State a
type Effects = HalogenEffects ()
ui :: Component State Query (Aff Effects)
ui = component render eval
where
render :: State -> ComponentHTML Query
render s = text (show s)
eval :: Natural Query (ComponentDSL State Query (Aff Effects))
eval (SetState s next) = next <$ do
set s
main :: forall eff . Eff Effects Unit
main = runAff throwException pure $ void do
app <- runUI ui 0
let update p = runAff throwException pure $ app.driver (action (SetState p))
onLoad $ appendToBody app.node
liftEff do
let button index delta = do
button <- mouseButton index
pure $ (const delta <~ filter id true button)
plus <- button 0 1
minus <- button 2 (negate 1)
runSignal $ update <~ foldp (+) 0 (plus ++ minus)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment