Guest User

Untitled

a guest
Jun 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. ;; hit-and-run swap!
  2.  
  3. (defn swap-and-run! [atom f & args]
  4. (swap! atom (fn [x] (delay (apply f @x args)))))
  5.  
  6. ;;;;;;;;;;;;;;
  7.  
  8. (defprotocol LazySwap
  9. (lazyswap [x f]))
  10.  
  11. (deftype LazyAtom [atom]
  12. clojure.lang.IDeref
  13. (deref [x]
  14. @@atom)
  15. LazySwap
  16. (lazyswap [x f]
  17. (swap! atom f)))
  18.  
  19. (defn lazy-swap!
  20. "Atomically swaps the value to be (apply f current-value args). The evaluation
  21. is suspended until deref. Returns a delay on the value."
  22. [lazy-atom f & args]
  23. (lazyswap lazy-atom (fn [d] (delay (apply f @d args)))))
  24.  
  25. (defn lazy-atom [x]
  26. (LazyAtom. (atom (delay x))))
Add Comment
Please, Sign In to add comment