1 Sep 2008 02:12
remove-ns before loading lib?
Stephen C. Gilardi <squeegee <at> mac.com>
2008-09-01 00:12:07 GMT
2008-09-01 00:12:07 GMT
One of the downsides of interactive development and testing using the REPL is the fact that an old definitions in a lib's namespace can linger in memory after its source code has been deleted from the lib. One can relaunch Clojure to be sure of clearing that situation, but that often loses some desirable context. I'm proposing a change to make relaunching Clojure for that purpose unnecessary.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Clojure" group.
To post to this group, send email to clojure <at> googlegroups.com
To unsubscribe from this group, send email to clojure+unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---
With the new clojure/require and clojure/use, we've introduced a suggested, preferred, best-supported way of working with Clojure in which there's a one-to-one correspondence between a lib namespace and the lib that defines it. The enclosed patch would ensure that any old definitions in a lib namespace are discarded before loading the lib: the namespace would be built up from scratch with each load. Putting the 'remove-ns call at the location I suggest would allow ":reload" and ":reload-all" to support this "from scratch" behavior as well. (:reload-all is especially useful from the REPL.)
There may still be situations where other code can hold onto an old definition across a reload but this change will cover the vast majority of cases easily and reliably.
Would this be a good change?
--Steve
Index: boot.clj
===================================================================
--- boot.clj (revision 1013)
+++ boot.clj (working copy)
<at> <at> -3045,6 +3045,7 <at> <at>
namespace exists after loading. If require, records the load so any
duplicate loads can be skipped."
[lib need-ns require]
+ (remove-ns lib)
(load-resources (root-resource lib))
(throw-if (and need-ns (not (find-ns lib)))
"namespace '%s' not found after loading '%s'"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Clojure" group.
To post to this group, send email to clojure <at> googlegroups.com
To unsubscribe from this group, send email to clojure+unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---
I wasn't aware of named anonymous fns – nice.
But i suspect disguising lazy seqs as fns prevents memoization from
happening, which is the benefit of using lazy seqs over naive
recursion. So on each (fibs2) call, a completely new fibs sequence is
built, without reusing values computed earlier, it seems:
With
(def fibs (lazy-cat '(0 1) (map + fibs (drop 1 fibs))))
(defn fib [n]
(let [fibs2 (fn fibs2 [] (lazy-cat '(0 1) (map + (fibs2) (drop 1
(fibs2)))))]
(nth (fibs2) n)))
freshly defined, (nth fibs 10000) finishes in a couple of seconds,
while (fib 10000) might run for ages …
Any ideas very much appreciated!
Thanks again,
kind regards,
achim
RSS Feed