Skip to content

Instantly share code, notes, and snippets.

@robashton
Created June 28, 2012 12:17
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 robashton/3011019 to your computer and use it in GitHub Desktop.
Save robashton/3011019 to your computer and use it in GitHub Desktop.
Keyboard.clj
(defn onKeyDown [e]
(.log js/console "down")
)
(defn onKeyUp [e]
(.log js/console "up")
)
(defn hookKeyboard []
(.addEventListener js/document "keydown" onKeyDown)
(.addEventListener js/document "keyup" onKeyUp)
)
// If left is down, (dec x) else if right is down (inc x)
// How do I get from the above code to being able to write the above code.
// Please note: I don't want to jump ahead to a pure functional solution unless it's really simple and doesn't expose too many new concepts to me - I'll be iterating on this a few times on my blog and can't just 'jump ahead'.
// I'd prefer to start off with shared mutated state (a map of keys to their current state for example) and then move away from that.
@swannodette
Copy link

Make an top level atom, mutate that.

(def x (atom 0))
(swap! x inc)
(println @x) ; => 1
(swap! x dec)
(println @x) ; => 0

@robashton
Copy link
Author

Great! I saw atoms but their use looked more complicated than this - thanks for answering my specific question - hope to improve shortly. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment