Mercurial > rlm
changeset 6:b8bbb0dbda7b
merging
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 01 Mar 2012 05:55:59 -0700 |
parents | fca75c0e8f40 (current diff) 8565803376a4 (diff) |
children | b4395ca99822 |
files | src/rlm/function_utils.clj src/rlm/rlm_commands.clj |
diffstat | 3 files changed, 40 insertions(+), 173 deletions(-) [+] |
line wrap: on
line diff
1.1 --- a/src/rlm/function_utils.clj Thu Mar 01 05:47:37 2012 -0700 1.2 +++ b/src/rlm/function_utils.clj Thu Mar 01 05:55:59 2012 -0700 1.3 @@ -4,68 +4,11 @@ 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 race 1.70 +(defn race 1.71 "Takes any number of mathematically equal functions with 1.72 possibly different run-times and returns a function that 1.73 runs each in a separate thread, returns the result from 1.74 @@ -108,44 +51,6 @@ 1.75 (dorun (map future-cancel futures)) 1.76 answer)))) 1.77 1.78 - 1.79 - 1.80 -(defn mix-threads 1.81 - " Takes any number of pure functions that take the same arguments and 1.82 - compute the same value and returns a function that runs each in a separate 1.83 - thread, returns the result from the first thread which finshes, and cancels 1.84 - the other threads. Explicitly uses nasty Threads. 1.85 - 1.86 - For example: 1.87 - (do 1.88 - (defn fun1 [] (Thread/sleep 5000) 5) 1.89 - (defn fun2 [] (Thread/sleep 700000) 5) 1.90 - (time ((mix fun1 fun2)))) 1.91 - 1.92 - Returns: 1.93 - | Elapsed time: 5000.66214 msecs 1.94 - 5" 1.95 - [& functions] 1.96 - (fn [& args] 1.97 - (let [result (prof :create-atom (atom void)) 1.98 - threads 1.99 - (prof :create-threads (map 1.100 - (fn [fun] 1.101 - (Thread. 1.102 - (fn [] 1.103 - (try (let [answer (apply fun args)] 1.104 - (reset! result answer)) 1.105 - (catch Exception _ nil))))) 1.106 - functions))] 1.107 - 1.108 - (prof :start-threads (dorun (map #(.start %) threads))) 1.109 - (prof :loop (loop [] 1.110 - (if (= (deref result) void) 1.111 - (recur) 1.112 - (do (prof :kill-threads (dorun (map #(.stop %) threads))) 1.113 - (prof :return (deref result))) 1.114 - )))))) 1.115 - 1.116 (defmacro defmix 1.117 " Defines a function from any number of pure functions that take the same 1.118 arguments and compute the same value which: 1.119 @@ -175,10 +80,11 @@ 1.120 (let [doc-string (if (string? (first functions)) (first functions) "") 1.121 functions (if (string? (first functions)) (rest functions) functions) 1.122 arglists (:arglists (meta (resolve (eval `(quote ~(first functions)))))) 1.123 - name (with-meta name (assoc (meta name) :arglists `(quote ~arglists) :doc doc-string))] 1.124 + name (with-meta name 1.125 + (assoc (meta name) :arglists `(quote ~arglists) 1.126 + :doc doc-string))] 1.127 `(def ~name (mix ~@functions)))) 1.128 1.129 - 1.130 (defn runonce 1.131 "Decorator. returns a function which will run only once. Inspired 1.132 by Halloway's version from lancet." 1.133 @@ -192,21 +98,3 @@ 1.134 (reset! result (apply function args)) 1.135 @result))))) 1.136 1.137 - 1.138 - 1.139 -;I'm thinking this will be the docstring for mix eventually. 1.140 - 1.141 - ;; " Takes any number of pure functions that take the same arguments and 1.142 - ;; compute the same value and returns a function that runs each in a separate 1.143 - ;; thread, returns the result from the first thread which finshes, and cancels 1.144 - ;; the other threads. 1.145 - 1.146 - ;; For example: 1.147 - ;; (do 1.148 - ;; (defn fun1 [] (Thread/sleep 5000) 5) 1.149 - ;; (defn fun2 [] (Thread/sleep 700000) 5) 1.150 - ;; (time ((mix fun1 fun2)))) 1.151 - 1.152 - ;; Returns: 1.153 - ;; | Elapsed time: 5000.66214 msecs 1.154 - ;; 5"
2.1 --- a/src/rlm/ns_rlm.clj Thu Mar 01 05:47:37 2012 -0700 2.2 +++ b/src/rlm/ns_rlm.clj Thu Mar 01 05:55:59 2012 -0700 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 Thu Mar 01 05:47:37 2012 -0700 3.2 +++ b/src/rlm/rlm_commands.clj Thu Mar 01 05:55:59 2012 -0700 3.3 @@ -1,27 +1,22 @@ 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 - (:require clojure.java.javadoc)) 3.9 + (:use rlm.ns-rlm) 3.10 + (:use clojure.java.javadoc)) 3.11 3.12 +;; (defn current-directory [] (ds/file-str "/home/r/mobius")) 3.13 3.14 -(defn current-directory [] (ds/file-str "/home/r/mobius")) 3.15 - 3.16 -(defn file-re [regex & {:keys [dir recurse] :or 3.17 - {dir (current-directory) recurse false}}] 3.18 - (let [dir (ds/file-str dir)] 3.19 - (filter (fn [file] (re-matches regex (.getName file))) 3.20 - (if recurse 3.21 - (file-seq dir) 3.22 - (seq (.listFiles dir)))))) 3.23 +;; (defn file-re [regex & {:keys [dir recurse] :or 3.24 +;; {dir (current-directory) recurse false}}] 3.25 +;; (let [dir (ds/file-str dir)] 3.26 +;; (filter (fn [file] (re-matches regex (.getName file))) 3.27 +;; (if recurse 3.28 +;; (file-seq dir) 3.29 +;; (seq (.listFiles dir)))))) 3.30 3.31 (defmacro undef 3.32 "removes symbol from the current namespace" 3.33 [& symbols] 3.34 - 3.35 `(dorun (map (partial ns-unmap *ns*) (quote ~symbols)))) 3.36 3.37 - 3.38 (defn ns-reset-fn 3.39 "unmaps all interned symbols from the current namespace (except in-ns and ns)" 3.40 ([ns-name] 3.41 @@ -55,11 +50,6 @@ 3.42 3.43 (defmacro reload [] 3.44 `(do 3.45 - (rlm.ns-rlm/ns-clear) 3.46 - (use :reload-all (quote ~(symbol (str *ns*)))))) 3.47 - 3.48 -(defmacro reload [] 3.49 - `(do 3.50 (rlm.rlm-commands/ns-nuke) 3.51 (clojure.core/use 3.52 :reload-all 3.53 @@ -72,16 +62,11 @@ 3.54 (clojure.core/use :reload 3.55 (clojure.core/symbol (clojure.core/str clojure.core/*ns*))))) 3.56 3.57 -(defn keymap-clojure [] 3.58 - (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.clojure")) 3.59 +;; (defn keymap-clojure [] 3.60 +;; (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.clojure")) 3.61 3.62 -(defn keymap-normal [] 3.63 - (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.normal")) 3.64 - 3.65 - 3.66 -(defn rlm [] 3.67 - (clojure.core/require 'rlm.light-base) 3.68 - (rlm.ns-rlm/ns-clone rlm.light-base)) 3.69 +;; (defn keymap-normal [] 3.70 +;; (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.normal")) 3.71 3.72 (defn javadoc [target] 3.73 (binding 3.74 @@ -89,31 +74,27 @@ 3.75 "/home/r/proj/repl/get-webpage.pl"] 3.76 (clojure.java.javadoc/javadoc target))) 3.77 3.78 - 3.79 (defn help 3.80 "load a bunch of really useful help functions" 3.81 [] 3.82 (use 3.83 '[rlm 3.84 [function-utils :only [race race-pred defmix]] 3.85 - [rlm-commands :only [undef ns-reset ns-nuke reload keymap-clojure 3.86 - keymap-normal rlm javadoc]] 3.87 - [ns-rlm :only [ns-clear ns-clone ls]] 3.88 - [play-all :only [play-all-music]] 3.89 + [rlm-commands :only 3.90 + [undef ns-reset ns-nuke reload re]] 3.91 + [ns-rlm :only [ls]] 3.92 + ;;[play-all :only [play-all-music]] 3.93 [shell-inspect :only [command-line?]] 3.94 - [shell-write :only [sw]] 3.95 - [classpath-utils :only [classpath add-to-classpath]] 3.96 + ;;[shell-write :only [sw]] 3.97 + ;;[classpath-utils :only [classpath add-to-classpath]] 3.98 [dreams :only [megadef silence]] 3.99 [map-utils :only [map-keys filter-keys filter-vals]] 3.100 [visualize :only [visual]] 3.101 [identify :only [identify]]] 3.102 - '[abomination.no-parens :only [quit]] 3.103 + 3.104 + ;;'[abomination.no-parens :only [quit]] 3.105 3.106 - 3.107 - '[clojure.contrib 3.108 - [duck-streams :only [file-str read-lines]] 3.109 - [str-utils :only [re-split re-gsub str-join]] 3.110 - [repl-utils :only [show expression-info]]] 3.111 + ;; TODO find replacement for show 3.112 '[clojure 3.113 [repl :only [source]]]) 3.114 (clojure.java.javadoc/add-local-javadoc 3.115 @@ -128,8 +109,6 @@ 3.116 "/home/r/proj/jmeCapture/docs") 3.117 (clojure.java.javadoc/add-local-javadoc 3.118 "/home/r/java/tritonus.sourceforge.net/apidoc") 3.119 - 3.120 - 3.121 nil) 3.122 3.123