Skip to content

Instantly share code, notes, and snippets.

@CampingScorpion
Created February 19, 2012 02:15
Show Gist options
  • Save CampingScorpion/1861626 to your computer and use it in GitHub Desktop.
Save CampingScorpion/1861626 to your computer and use it in GitHub Desktop.
Mutable objects!
(defn make-foo [n]
(let [foo (atom n)]
{:update (fn [n] (reset! foo n))
:inc (fn [] (swap! foo inc))
:status (fn [] (print @foo))} ))
(defmacro defobject
"doesn't work yet"
[name field-vec & methods]
(let [methods (partition 3 methods)
methods (into {} (for [[name args body] methods]
[(keyword name) `(fn args body)]))
let-binding (vec (for [field field-vec]
`(~field (atom nil))))]
`(defn ~name
(let [f# nil] ;;~let-binding
~methods))))
(defobject make-foo
[foo (atom n)]
update [n] (reset! foo n)
inc [] (swap! foo inc)
status [] (print @foo))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment