# HG changeset patch # User Robert McIntyre # Date 1330606559 25200 # Node ID b8bbb0dbda7b2f6aad0cd5a9ad650854a1848d38 # Parent fca75c0e8f40062b3276d94f481b0f16db12886b# Parent 8565803376a42f06cb06ec2fdd2551807a06d7ae merging diff -r fca75c0e8f40 -r b8bbb0dbda7b src/rlm/function_utils.clj --- a/src/rlm/function_utils.clj Thu Mar 01 05:47:37 2012 -0700 +++ b/src/rlm/function_utils.clj Thu Mar 01 05:55:59 2012 -0700 @@ -4,68 +4,11 @@ (ns rlm.function-utils "Collection of Operators on Pure Functions" - {:author "Robert McIntyre"} - (:use [clojure.contrib.profile])) + {:author "Robert McIntyre"}) (def void ::void) -(comment - - "Please help me out here. I'm trying to make a higher order function that takes - pure functions that are mathmaticaly the same but have different times and returns another - function that runs them both and returns the resut of the one that finishes first." - - - "here's a repl session: - -mobius> " (time (dotimes [_ 500]((mix-futures + +) 40 3 3 3 3 3))) " -Elapsed time: 85.792995 msecs -nil -mobius> " (time (dotimes [_ 500](+ 40 3 3 3 3 3))) " -Elapsed time: 6.956338 msecs -nil -mobius> " (time (dotimes [_ 500]((mix-threads + +) 40 3 3 3 3 3))) " -Elapsed time: 706.227065 msecs -nil - - -mobius> " (defn fast-five [& args] 5) " -#'mobius/fast-five - -mobius> " (defn slow-five [& args] (Thread/sleep 5000) (println "Oh YEAH!!!!") 5) " -#'mobius/slow-five - -mobius> " (profile (time (dotimes [_ 5000] (thread-five)))) " -\"Elapsed time: 12187.981587 msecs\" - Name mean min max count sum - create-atom 9621 2025 4433928 5000 48106830 -create-threads 5156 2724 2880268 5000 25783796 - kill-threads 91313 27866 11175718 5000 456567066 - loop 2124249 38482 18470781 5000 10621249899 - return 1242 838 5309 5000 6212626 - start-threads 252985 110348 12272835 5000 1264927953 -nil - -mobius> " (profile (time (dotimes [_ 5000] (future-five)))) " -\"Elapsed time: 607.266671 msecs\" - Name mean min max count sum - create-atom 1472 1047 31708 5000 7363139 -create-futures 30539 2514 1330660 5000 152697998 - kill-threads 54158 2444 3938833 5000 270792897 - loop 81117 8800 6083059 5000 405587516 - return 1215 838 618782 5000 6078146 -nil - - - -What can I improve here, and why is the future version sooo much faster than the -thread version? Is there a better way than using loop? -" - -) - - -(defn race +(defn race "Takes any number of mathematically equal functions with possibly different run-times and returns a function that runs each in a separate thread, returns the result from @@ -108,44 +51,6 @@ (dorun (map future-cancel futures)) answer)))) - - -(defn mix-threads - " Takes any number of pure functions that take the same arguments and - compute the same value and returns a function that runs each in a separate - thread, returns the result from the first thread which finshes, and cancels - the other threads. Explicitly uses nasty Threads. - - For example: - (do - (defn fun1 [] (Thread/sleep 5000) 5) - (defn fun2 [] (Thread/sleep 700000) 5) - (time ((mix fun1 fun2)))) - - Returns: - | Elapsed time: 5000.66214 msecs - 5" - [& functions] - (fn [& args] - (let [result (prof :create-atom (atom void)) - threads - (prof :create-threads (map - (fn [fun] - (Thread. - (fn [] - (try (let [answer (apply fun args)] - (reset! result answer)) - (catch Exception _ nil))))) - functions))] - - (prof :start-threads (dorun (map #(.start %) threads))) - (prof :loop (loop [] - (if (= (deref result) void) - (recur) - (do (prof :kill-threads (dorun (map #(.stop %) threads))) - (prof :return (deref result))) - )))))) - (defmacro defmix " Defines a function from any number of pure functions that take the same arguments and compute the same value which: @@ -175,10 +80,11 @@ (let [doc-string (if (string? (first functions)) (first functions) "") functions (if (string? (first functions)) (rest functions) functions) arglists (:arglists (meta (resolve (eval `(quote ~(first functions)))))) - name (with-meta name (assoc (meta name) :arglists `(quote ~arglists) :doc doc-string))] + name (with-meta name + (assoc (meta name) :arglists `(quote ~arglists) + :doc doc-string))] `(def ~name (mix ~@functions)))) - (defn runonce "Decorator. returns a function which will run only once. Inspired by Halloway's version from lancet." @@ -192,21 +98,3 @@ (reset! result (apply function args)) @result))))) - - -;I'm thinking this will be the docstring for mix eventually. - - ;; " Takes any number of pure functions that take the same arguments and - ;; compute the same value and returns a function that runs each in a separate - ;; thread, returns the result from the first thread which finshes, and cancels - ;; the other threads. - - ;; For example: - ;; (do - ;; (defn fun1 [] (Thread/sleep 5000) 5) - ;; (defn fun2 [] (Thread/sleep 700000) 5) - ;; (time ((mix fun1 fun2)))) - - ;; Returns: - ;; | Elapsed time: 5000.66214 msecs - ;; 5" diff -r fca75c0e8f40 -r b8bbb0dbda7b src/rlm/ns_rlm.clj --- a/src/rlm/ns_rlm.clj Thu Mar 01 05:47:37 2012 -0700 +++ b/src/rlm/ns_rlm.clj Thu Mar 01 05:55:59 2012 -0700 @@ -1,6 +1,5 @@ (ns rlm.ns-rlm - "A few convienence functions for namespaces." - (:use [clojure.contrib [ns-utils :only [get-ns]]])) + "A few convienence functions for namespaces.") (defn ns-clear "completely removes all aliases, interns, and references from the namespace @@ -12,12 +11,13 @@ ([] (ns-clear *ns*))) (defn ns-clone-fn - "the calling namespace (*ns*) gets all the referenced vars as the target namespace - anything that the target namespace doesn't have, ns won't have. Anything it does - have, *ns* will have." + "the calling namespace (*ns*) gets all the referenced vars as the + target namespace anything that the target namespace doesn't have, + ns won't have. Anything it does have, *ns* will have." [target-ns] (ns-clear *ns*) - (doall (map (fn [[ns-alias ns]] (alias ns-alias (.name ns))) (ns-aliases target-ns))) + (doall (map (fn [[ns-alias ns]] + (alias ns-alias (.name ns))) (ns-aliases target-ns))) (doall (map (fn [[sym var]] (cond (var? var) (. *ns* (refer sym var)) @@ -26,15 +26,15 @@ ;;(eval `(clojure.core/import* ~(.getName var))))) (ns-map target-ns))) nil) -(defmacro ns-clone - ([target-ns-name] - `(do - (require '~target-ns-name) - (ns-clone-fn (get-ns '~target-ns-name))))) +;; (defmacro ns-clone +;; ([target-ns-name] +;; `(do +;; (require '~target-ns-name) +;; (ns-clone-fn (get-ns '~target-ns-name))))) (defn ls "lists all the vars defined in the namespace" - ([ns] (doall (map println (keys (ns-map ns)))) nil) + ([ns] (doall (map println (keys (ns-interns ns)))) nil) ([] (ls *ns*))) diff -r fca75c0e8f40 -r b8bbb0dbda7b src/rlm/rlm_commands.clj --- a/src/rlm/rlm_commands.clj Thu Mar 01 05:47:37 2012 -0700 +++ b/src/rlm/rlm_commands.clj Thu Mar 01 05:55:59 2012 -0700 @@ -1,27 +1,22 @@ (ns rlm.rlm-commands - (:require [rlm ns-rlm shell-write] - [clojure.contrib [duck-streams :as ds]]) - (:use [clojure.contrib java-utils]) - (:require clojure.java.javadoc)) + (:use rlm.ns-rlm) + (:use clojure.java.javadoc)) +;; (defn current-directory [] (ds/file-str "/home/r/mobius")) -(defn current-directory [] (ds/file-str "/home/r/mobius")) - -(defn file-re [regex & {:keys [dir recurse] :or - {dir (current-directory) recurse false}}] - (let [dir (ds/file-str dir)] - (filter (fn [file] (re-matches regex (.getName file))) - (if recurse - (file-seq dir) - (seq (.listFiles dir)))))) +;; (defn file-re [regex & {:keys [dir recurse] :or +;; {dir (current-directory) recurse false}}] +;; (let [dir (ds/file-str dir)] +;; (filter (fn [file] (re-matches regex (.getName file))) +;; (if recurse +;; (file-seq dir) +;; (seq (.listFiles dir)))))) (defmacro undef "removes symbol from the current namespace" [& symbols] - `(dorun (map (partial ns-unmap *ns*) (quote ~symbols)))) - (defn ns-reset-fn "unmaps all interned symbols from the current namespace (except in-ns and ns)" ([ns-name] @@ -55,11 +50,6 @@ (defmacro reload [] `(do - (rlm.ns-rlm/ns-clear) - (use :reload-all (quote ~(symbol (str *ns*)))))) - -(defmacro reload [] - `(do (rlm.rlm-commands/ns-nuke) (clojure.core/use :reload-all @@ -72,16 +62,11 @@ (clojure.core/use :reload (clojure.core/symbol (clojure.core/str clojure.core/*ns*))))) -(defn keymap-clojure [] - (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.clojure")) +;; (defn keymap-clojure [] +;; (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.clojure")) -(defn keymap-normal [] - (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.normal")) - - -(defn rlm [] - (clojure.core/require 'rlm.light-base) - (rlm.ns-rlm/ns-clone rlm.light-base)) +;; (defn keymap-normal [] +;; (rlm.shell-write/sw "xmodmap" "/home/r/.xmodmap.normal")) (defn javadoc [target] (binding @@ -89,31 +74,27 @@ "/home/r/proj/repl/get-webpage.pl"] (clojure.java.javadoc/javadoc target))) - (defn help "load a bunch of really useful help functions" [] (use '[rlm [function-utils :only [race race-pred defmix]] - [rlm-commands :only [undef ns-reset ns-nuke reload keymap-clojure - keymap-normal rlm javadoc]] - [ns-rlm :only [ns-clear ns-clone ls]] - [play-all :only [play-all-music]] + [rlm-commands :only + [undef ns-reset ns-nuke reload re]] + [ns-rlm :only [ls]] + ;;[play-all :only [play-all-music]] [shell-inspect :only [command-line?]] - [shell-write :only [sw]] - [classpath-utils :only [classpath add-to-classpath]] + ;;[shell-write :only [sw]] + ;;[classpath-utils :only [classpath add-to-classpath]] [dreams :only [megadef silence]] [map-utils :only [map-keys filter-keys filter-vals]] [visualize :only [visual]] [identify :only [identify]]] - '[abomination.no-parens :only [quit]] + + ;;'[abomination.no-parens :only [quit]] - - '[clojure.contrib - [duck-streams :only [file-str read-lines]] - [str-utils :only [re-split re-gsub str-join]] - [repl-utils :only [show expression-info]]] + ;; TODO find replacement for show '[clojure [repl :only [source]]]) (clojure.java.javadoc/add-local-javadoc @@ -128,8 +109,6 @@ "/home/r/proj/jmeCapture/docs") (clojure.java.javadoc/add-local-javadoc "/home/r/java/tritonus.sourceforge.net/apidoc") - - nil)