Skip to content

Instantly share code, notes, and snippets.

@whamtet
Last active June 15, 2016 04:09
Show Gist options
  • Save whamtet/6d8430fe9b706990874303ec8708b756 to your computer and use it in GitHub Desktop.
Save whamtet/6d8430fe9b706990874303ec8708b756 to your computer and use it in GitHub Desktop.
(defonce persisted-frames (atom {}))
(defn persist [k v]
(swap! persisted-frames assoc k v)
v)
(defn split-until [f s]
(let [[a b] (split-with #(not (f %)) s)]
[(concat a (take 1 b)) (rest b)]))
(defmacro with-persists2 [df done todo]
(let [
[to-persist todo] (split-until #(= '.persist %) todo)
persist-statement `(-> ~df ~@to-persist)
k (concat done to-persist)
]
(if (not= '.persist (last to-persist))
persist-statement
`(let [
persisted-frame# (or
(@persisted-frames '~k)
(persist '~k ~persist-statement))
]
(with-persists2 persisted-frame# ~k ~todo)))))
(defmacro with-persists [df & statements]
`(with-persists2 ~df [~df] ~statements))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment