Created
April 4, 2010 15:13
-
-
Save pervognsen/355456 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
(deftype LazyRef [ref] | |
clojure.lang.IDeref (deref [] (force @ref))) | |
(defn lazy-ref* [thunk] | |
(LazyRef (ref (delay (thunk))))) | |
(defmacro lazy-ref [form] | |
`(lazy-ref* (fn [] ~form))) | |
(defn lazy-alter [r f & args] | |
(alter (:ref r) (fn [x] (delay (apply f (concat args [(force x)])))))) | |
;; Example | |
(def r1 (lazy-ref (do (println "r1 forced") 42))) | |
(println @r1) | |
(println @r1) | |
(def r2 (lazy-ref (do (println "r2 forced") 42))) | |
(dosync (lazy-alter r2 (fn [x y] (println "r2 alter forced") (+ x y)) 8)) | |
(println "after lazy-alter") | |
(println @r2) | |
(println @r2) |
I've programmed a lot in Haskell and point-free style is often the right answer there. Clojure makes different semantic and syntactic trade-offs, so I find that it's less frequently the right answer. In any case, I consider use of expression threading macros like -> and ->> to be "morally" the same as point-free style.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are some community members advocating point-free style quite heavily. Hard at the limit to point-lessness, IMHO.
Anyway, I was not refering to your code. In fact I like your code. You did something similar to Refs I did to Maps with my lazymap library.