Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; hit-and-run swap!
- (defn swap-and-run! [atom f & args]
- (swap! atom (fn [x] (delay (apply f @x args)))))
- ;;;;;;;;;;;;;;
- (defprotocol LazySwap
- (lazyswap [x f]))
- (deftype LazyAtom [atom]
- clojure.lang.IDeref
- (deref [x]
- @@atom)
- LazySwap
- (lazyswap [x f]
- (swap! atom f)))
- (defn lazy-swap!
- "Atomically swaps the value to be (apply f current-value args). The evaluation
- is suspended until deref. Returns a delay on the value."
- [lazy-atom f & args]
- (lazyswap lazy-atom (fn [d] (delay (apply f @d args)))))
- (defn lazy-atom [x]
- (LazyAtom. (atom (delay x))))
Add Comment
Please, Sign In to add comment