History variables: Difference between revisions

Adds Clojure solution
(added autohotkey L)
(Adds Clojure solution)
Line 278:
 
<pre>foobar <- foo <- 5
</pre>
 
=={{header|Clojure}}==
Clojure does not have history variables, but it can be accomplished via a watcher function that can track changes on a variable.
 
<lang clojure>
(def a (ref 0))
(def a-history (atom [@a])) ; define a history vector to act as a stack for changes on variable a
(add-watch a :hist (fn [key ref old new] (swap! a-history conj new)))
</lang>
 
{{out|Sample Output}}
<pre>user=> (dosync (ref-set a 1))
1
user=> (dosync (ref-set a 2))
2
user=> (dosync (ref-set a 3))
3
user=> @a-history
[0 1 2 3]
user=> (dotimes [n 3] (println (peek @a-history)) (swap! a-history pop))
3
2
1
nil
user=> @a-history
[0]
</pre>
 
Anonymous user