Skip to content

Instantly share code, notes, and snippets.

@Gozala
Created August 1, 2014 01:14
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 Gozala/fc26deeabac7e05e42c6 to your computer and use it in GitHub Desktop.
Save Gozala/fc26deeabac7e05e42c6 to your computer and use it in GitHub Desktop.
;; Anything you type in here will be executed
;; immediately with the results shown on the
;; right.
(defprotocol IState
(-patch [state delta])
(-diff [from to]))
(defprotocol IDiff
(-apply [diff state]))
(defprotocol IEmpty
(-empty [_]))
(defn patch
[state diff]
(-apply diff state))
(defn diff
[from to]
(-diff from to))
(defmacro defmodel
[id record]
(let [fields (keys record)
defaults (vals record)
attributes (vec (map #(symbol (name %)) fields))]
`(defrecord ~id ~attributes
IEmpty
(-empty [model] (new ~id ~@defaults))
IState
(-patch [state diff]
(let [updated# (set (keys diff))
values# (map (fn [key#]
(if (updated# key#)
(patch (key# state) (key# diff))
(key# state)))
~fields)]
(vector values#))))))
(defmodel Task {:id 0
:description ""
:completed false
:editing false})
;; clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: No such var: user/state, compiling:(null:36:46)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment