changeset 1:8565803376a4

upgrading source to work with clojure 1.4
author Robert McIntyre <rlm@mit.edu>
date Tue, 28 Feb 2012 13:26:34 -0600
parents 78a630e650d2
children b8bbb0dbda7b
files src/rlm/function_utils.clj src/rlm/ns_rlm.clj src/rlm/rlm_commands.clj
diffstat 3 files changed, 75 insertions(+), 168 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/src/rlm/function_utils.clj	Tue Oct 18 00:57:08 2011 -0700
     1.2 +++ b/src/rlm/function_utils.clj	Tue Feb 28 13:26:34 2012 -0600
     1.3 @@ -4,67 +4,10 @@
     1.4  (ns 
     1.5      rlm.function-utils
     1.6    "Collection of Operators on Pure Functions"
     1.7 -  {:author "Robert McIntyre"}
     1.8 -  (:use [clojure.contrib.profile]))
     1.9 +  {:author "Robert McIntyre"})
    1.10  
    1.11  (def void ::void)
    1.12  
    1.13 -(comment
    1.14 -
    1.15 -  "Please help me out here.  I'm trying to make a higher order function that takes
    1.16 -   pure functions that are mathmaticaly the same but have different times and returns another
    1.17 -   function that runs them both and returns the resut of the one that finishes first."
    1.18 -
    1.19 -
    1.20 -  "here's a repl session:
    1.21 -
    1.22 -mobius> " (time (dotimes [_ 500]((mix-futures + +) 40 3 3 3 3 3))) "
    1.23 -Elapsed time: 85.792995 msecs
    1.24 -nil
    1.25 -mobius> " (time (dotimes [_ 500](+ 40 3 3 3 3 3))) "
    1.26 -Elapsed time: 6.956338 msecs
    1.27 -nil
    1.28 -mobius> " (time (dotimes [_ 500]((mix-threads + +) 40 3 3 3 3 3))) "
    1.29 -Elapsed time: 706.227065 msecs
    1.30 -nil
    1.31 -
    1.32 -
    1.33 -mobius> " (defn fast-five [& args] 5) "
    1.34 -#'mobius/fast-five
    1.35 -
    1.36 -mobius> " (defn slow-five [& args] (Thread/sleep 5000) (println "Oh YEAH!!!!") 5) "
    1.37 -#'mobius/slow-five
    1.38 -
    1.39 -mobius> " (profile (time (dotimes [_ 5000] (thread-five)))) "
    1.40 -\"Elapsed time: 12187.981587 msecs\"
    1.41 -          Name      mean       min       max     count       sum
    1.42 -   create-atom      9621      2025   4433928      5000  48106830
    1.43 -create-threads      5156      2724   2880268      5000  25783796
    1.44 -  kill-threads     91313     27866  11175718      5000  456567066
    1.45 -          loop   2124249     38482  18470781      5000  10621249899
    1.46 -        return      1242       838      5309      5000   6212626
    1.47 - start-threads    252985    110348  12272835      5000  1264927953
    1.48 -nil
    1.49 -
    1.50 -mobius> " (profile (time (dotimes [_ 5000] (future-five)))) "
    1.51 -\"Elapsed time: 607.266671 msecs\"
    1.52 -          Name      mean       min       max     count       sum
    1.53 -   create-atom      1472      1047     31708      5000   7363139
    1.54 -create-futures     30539      2514   1330660      5000  152697998
    1.55 -  kill-threads     54158      2444   3938833      5000  270792897
    1.56 -          loop     81117      8800   6083059      5000  405587516
    1.57 -        return      1215       838    618782      5000   6078146
    1.58 -nil
    1.59 -
    1.60 -
    1.61 -
    1.62 -What can I improve here, and why is the future version sooo much faster than the
    1.63 -thread version? Is there a better way than using loop?
    1.64 -"
    1.65 -
    1.66 -)
    1.67 -
    1.68 -
    1.69  (defn mix 
    1.70    "Takes any number of mathematically equal functions with
    1.71     possibly different run-times and returns a function that
    1.72 @@ -80,44 +23,40 @@
    1.73  	 (dorun (map future-cancel futures))
    1.74  	 answer))))
    1.75  
    1.76 +;; (defn mix-threads
    1.77 +;;   " Takes any number of pure functions that take the same arguments and
    1.78 +;;    compute the same value and returns a function that runs each in a separate
    1.79 +;;    thread, returns the result from the first thread which finshes, and cancels 
    1.80 +;;    the other threads. Explicitly uses nasty Threads.
    1.81  
    1.82 -
    1.83 -
    1.84 -(defn mix-threads
    1.85 -  " Takes any number of pure functions that take the same arguments and
    1.86 -   compute the same value and returns a function that runs each in a separate
    1.87 -   thread, returns the result from the first thread which finshes, and cancels 
    1.88 -   the other threads. Explicitly uses nasty Threads.
    1.89 -
    1.90 -   For example: 
    1.91 -   (do
    1.92 -   (defn fun1 [] (Thread/sleep 5000) 5)
    1.93 -   (defn fun2 [] (Thread/sleep 700000) 5)
    1.94 -   (time ((mix fun1 fun2))))
    1.95 +;;    For example: 
    1.96 +;;    (do
    1.97 +;;    (defn fun1 [] (Thread/sleep 5000) 5)
    1.98 +;;    (defn fun2 [] (Thread/sleep 700000) 5)
    1.99 +;;    (time ((mix fun1 fun2))))
   1.100     
   1.101 -   Returns: 
   1.102 -    | Elapsed time: 5000.66214 msecs
   1.103 -   5"
   1.104 -  [& functions]
   1.105 -  (fn [& args]
   1.106 -    (let [result (prof :create-atom (atom void))
   1.107 -	  threads
   1.108 -	   (prof :create-threads (map 
   1.109 -	   (fn [fun] 
   1.110 -	     (Thread. 
   1.111 -	      (fn [] 
   1.112 -		(try (let [answer (apply fun args)]
   1.113 -		       (reset! result answer))
   1.114 -		     (catch Exception _ nil)))))
   1.115 -	   functions))]
   1.116 +;;    Returns: 
   1.117 +;;     | Elapsed time: 5000.66214 msecs
   1.118 +;;    5"
   1.119 +;;   [& functions]
   1.120 +;;   (fn [& args]
   1.121 +;;     (let [result (prof :create-atom (atom void))
   1.122 +;; 	  threads
   1.123 +;; 	   (prof :create-threads (map 
   1.124 +;; 	   (fn [fun] 
   1.125 +;; 	     (Thread. 
   1.126 +;; 	      (fn [] 
   1.127 +;; 		(try (let [answer (apply fun args)]
   1.128 +;; 		       (reset! result answer))
   1.129 +;; 		     (catch Exception _ nil)))))
   1.130 +;; 	   functions))]
   1.131        
   1.132 -      (prof :start-threads (dorun (map #(.start %) threads)))
   1.133 -      (prof :loop (loop []
   1.134 -	(if (=  (deref result) void)
   1.135 -	  (recur)
   1.136 -	  (do (prof :kill-threads (dorun (map #(.stop %) threads)))
   1.137 -	      (prof :return (deref result)))
   1.138 -	  ))))))
   1.139 +;;       (prof :start-threads (dorun (map #(.start %) threads)))
   1.140 +;;       (prof :loop (loop []
   1.141 +;; 	(if (=  (deref result) void)
   1.142 +;; 	  (recur)
   1.143 +;; 	  (do (prof :kill-threads (dorun (map #(.stop %) threads)))
   1.144 +;; 	      (prof :return (deref result)))))))))
   1.145  
   1.146  (defmacro defmix
   1.147    " Defines a function from any number of pure functions that take the same 
   1.148 @@ -148,10 +87,11 @@
   1.149    (let [doc-string (if (string? (first functions)) (first functions) "")
   1.150  	functions (if (string? (first functions)) (rest functions) functions)
   1.151  	arglists (:arglists (meta (resolve (eval `(quote ~(first functions))))))
   1.152 -	name (with-meta name (assoc (meta name) :arglists `(quote ~arglists) :doc doc-string))]   
   1.153 +	name (with-meta name
   1.154 +               (assoc (meta name) :arglists `(quote ~arglists)
   1.155 +                      :doc doc-string))]   
   1.156      `(def ~name (mix ~@functions))))
   1.157  
   1.158 -
   1.159  (defn runonce
   1.160    "Decorator.  returns a function which will run only once. Inspired
   1.161     by Halloway's version from lancet."
   1.162 @@ -165,21 +105,3 @@
   1.163  	   (reset! result (apply function args)) 
   1.164  	    @result)))))
   1.165  
   1.166 -
   1.167 -
   1.168 -;I'm thinking this will be the docstring for mix eventually.
   1.169 -
   1.170 -  ;; " Takes any number of pure functions that take the same arguments and
   1.171 -  ;;  compute the same value and returns a function that runs each in a separate
   1.172 -  ;;  thread, returns the result from the first thread which finshes, and cancels 
   1.173 -  ;;  the other threads.
   1.174 -
   1.175 -  ;;  For example: 
   1.176 -  ;;  (do
   1.177 -  ;;  (defn fun1 [] (Thread/sleep 5000) 5)
   1.178 -  ;;  (defn fun2 [] (Thread/sleep 700000) 5)
   1.179 -  ;;  (time ((mix fun1 fun2))))
   1.180 -   
   1.181 -  ;;  Returns: 
   1.182 -  ;;   | Elapsed time: 5000.66214 msecs
   1.183 -  ;;  5"
     2.1 --- a/src/rlm/ns_rlm.clj	Tue Oct 18 00:57:08 2011 -0700
     2.2 +++ b/src/rlm/ns_rlm.clj	Tue Feb 28 13:26:34 2012 -0600
     2.3 @@ -1,6 +1,5 @@
     2.4  (ns rlm.ns-rlm
     2.5 -  "A few convienence functions for namespaces."
     2.6 -  (:use [clojure.contrib [ns-utils :only [get-ns]]]))
     2.7 +  "A few convienence functions for namespaces.")
     2.8  
     2.9  (defn ns-clear
    2.10    "completely removes all aliases, interns, and references from the namespace
    2.11 @@ -12,12 +11,13 @@
    2.12      ([] (ns-clear *ns*)))
    2.13  
    2.14  (defn ns-clone-fn
    2.15 -  "the calling namespace (*ns*) gets all the referenced vars as the target namespace
    2.16 -     anything that the target namespace doesn't have, ns won't have.  Anything it does
    2.17 -     have, *ns* will have."
    2.18 +  "the calling namespace (*ns*) gets all the referenced vars as the
    2.19 +   target namespace anything that the target namespace doesn't have,
    2.20 +   ns won't have.  Anything it does have, *ns* will have."
    2.21    [target-ns]
    2.22    (ns-clear *ns*)
    2.23 -  (doall (map (fn [[ns-alias ns]] (alias ns-alias (.name ns))) (ns-aliases target-ns)))
    2.24 +  (doall (map (fn [[ns-alias ns]]
    2.25 +                (alias ns-alias (.name  ns))) (ns-aliases target-ns)))
    2.26    (doall (map (fn [[sym var]]
    2.27  		(cond (var? var)
    2.28  		      (. *ns* (refer sym var))
    2.29 @@ -26,15 +26,15 @@
    2.30  		      ;;(eval `(clojure.core/import*  ~(.getName var)))))
    2.31  		  (ns-map target-ns))) nil)
    2.32  
    2.33 -(defmacro ns-clone
    2.34 - ([target-ns-name]
    2.35 -    `(do
    2.36 -       (require '~target-ns-name)
    2.37 -       (ns-clone-fn (get-ns '~target-ns-name)))))
    2.38 +;; (defmacro ns-clone
    2.39 +;;  ([target-ns-name]
    2.40 +;;     `(do
    2.41 +;;        (require '~target-ns-name)
    2.42 +;;        (ns-clone-fn (get-ns '~target-ns-name)))))
    2.43  
    2.44  (defn ls
    2.45    "lists all the vars defined in the namespace"
    2.46 -  ([ns] (doall (map println (keys (ns-map ns)))) nil)
    2.47 +  ([ns] (doall (map println (keys (ns-interns ns)))) nil)
    2.48    ([] (ls *ns*)))
    2.49  
    2.50  
     3.1 --- a/src/rlm/rlm_commands.clj	Tue Oct 18 00:57:08 2011 -0700
     3.2 +++ b/src/rlm/rlm_commands.clj	Tue Feb 28 13:26:34 2012 -0600
     3.3 @@ -1,27 +1,23 @@
     3.4  (ns rlm.rlm-commands
     3.5 -  (:require  [rlm ns-rlm shell-write]
     3.6 -	     [clojure.contrib [duck-streams :as ds]])
     3.7 -  (:use [clojure.contrib java-utils])
     3.8 +  (:use rlm.ns-rlm)
     3.9    (:use clojure.java.javadoc))
    3.10  
    3.11  
    3.12 -(defn current-directory [] (ds/file-str "/home/r/mobius"))
    3.13 +;; (defn current-directory [] (ds/file-str "/home/r/mobius"))
    3.14  
    3.15 -(defn file-re [regex & {:keys [dir recurse] :or
    3.16 -			{dir (current-directory) recurse false}}]
    3.17 -  (let [dir (ds/file-str dir)]
    3.18 -    (filter (fn [file] (re-matches regex (.getName file)))
    3.19 -	    (if recurse
    3.20 -	      (file-seq dir)
    3.21 -	      (seq (.listFiles dir))))))
    3.22 +;; (defn file-re [regex & {:keys [dir recurse] :or
    3.23 +;; 			{dir (current-directory) recurse false}}]
    3.24 +;;   (let [dir (ds/file-str dir)]
    3.25 +;;     (filter (fn [file] (re-matches regex (.getName file)))
    3.26 +;; 	    (if recurse
    3.27 +;; 	      (file-seq dir)
    3.28 +;; 	      (seq (.listFiles dir))))))
    3.29  
    3.30  (defmacro undef
    3.31    "removes symbol from the current namespace"
    3.32    [& symbols]
    3.33 - 
    3.34    `(dorun (map (partial ns-unmap *ns*) (quote ~symbols))))
    3.35  
    3.36 -
    3.37  (defn ns-reset-fn
    3.38    "unmaps all interned symbols from the current namespace (except in-ns and ns)"
    3.39    ([ns-name]
    3.40 @@ -55,11 +51,6 @@
    3.41  
    3.42  (defmacro reload []
    3.43    `(do
    3.44 -     (rlm.ns-rlm/ns-clear)
    3.45 -     (use :reload-all (quote ~(symbol (str *ns*))))))
    3.46 -
    3.47 -(defmacro reload []
    3.48 -  `(do
    3.49       (rlm.rlm-commands/ns-nuke)
    3.50       (clojure.core/use
    3.51        :reload-all
    3.52 @@ -72,47 +63,41 @@
    3.53       (clojure.core/use :reload
    3.54        (clojure.core/symbol (clojure.core/str clojure.core/*ns*)))))
    3.55  
    3.56 -(defn keymap-clojure []
    3.57 -  (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.clojure"))
    3.58 +;; (defn keymap-clojure []
    3.59 +;;   (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.clojure"))
    3.60  
    3.61 +;; (defn keymap-normal []
    3.62 +;;   (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.normal"))
    3.63  
    3.64 -
    3.65 -(defn keymap-normal []
    3.66 -  (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.normal"))
    3.67 -
    3.68 -
    3.69 -(defn rlm []
    3.70 -  (clojure.core/require 'rlm.light-base)
    3.71 -  (rlm.ns-rlm/ns-clone rlm.light-base))
    3.72 -
    3.73 +;; (defn rlm []
    3.74 +;;   (clojure.core/require 'rlm.light-base)
    3.75 +;;   (rlm.ns-rlm/ns-clone rlm.light-base))
    3.76  
    3.77  (defn help
    3.78    "load a bunch of really useful help functions"
    3.79    []
    3.80    (use
    3.81     '[rlm
    3.82 -     [function-utils :only [mix defmix runonce]]
    3.83 -     [rlm-commands :only [undef ns-reset ns-nuke reload keymap-clojure keymap-normal rlm]]
    3.84 -     [ns-rlm :only [ns-clear ns-clone ls]]
    3.85 -     [play-all :only [play-all-music]]
    3.86 +     [function-utils :only [mix defmix]]
    3.87 +     [rlm-commands :only
    3.88 +      [undef ns-reset ns-nuke reload re]]
    3.89 +     [ns-rlm :only [ls]]
    3.90 +     ;;[play-all :only [play-all-music]]
    3.91       [shell-inspect :only [command-line?]]
    3.92 -     [shell-write :only [sw]]
    3.93 -     [classpath-utils :only [classpath add-to-classpath]]
    3.94 +     ;;[shell-write :only [sw]]
    3.95 +     ;;[classpath-utils :only [classpath add-to-classpath]]
    3.96       [dreams :only [megadef silence]]
    3.97       [map-utils :only [map-keys map-vals filter-keys filter-vals]]
    3.98       [visualize :only [visual]]
    3.99       [identify :only [identify]]]
   3.100 -   '[abomination.no-parens :only [quit]]
   3.101 +
   3.102 +   ;;'[abomination.no-parens :only [quit]]
   3.103     
   3.104 -   
   3.105 -   '[clojure.contrib
   3.106 -     [duck-streams :only [file-str read-lines]]
   3.107 -     [str-utils :only [re-split re-gsub str-join]]
   3.108 -     [repl-utils :only [show expression-info]]]
   3.109 +   ;; TODO find replacement for show
   3.110      '[clojure
   3.111        [repl :only [source]]]
   3.112 -     '[clojure.java
   3.113 -       [javadoc :only [javadoc add-local-javadoc]]])
   3.114 +    '[clojure.java
   3.115 +      [javadoc :only [javadoc add-local-javadoc]]])
   3.116    (clojure.java.javadoc/add-local-javadoc "/home/r/cortex/jme3/dist/javadoc")
   3.117    (clojure.java.javadoc/add-local-javadoc "/home/r/roBin/jdk6-docs/docs/api")
   3.118    (clojure.java.javadoc/add-local-javadoc