Created
August 1, 2014 01:14
-
-
Save Gozala/fc26deeabac7e05e42c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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