# HG changeset patch # User Robert McIntyre # Date 1330457194 21600 # Node ID 8565803376a42f06cb06ec2fdd2551807a06d7ae # Parent 78a630e650d229e4c6f1cd968d9a1e4b56e8a7d7 upgrading source to work with clojure 1.4 diff -r 78a630e650d2 -r 8565803376a4 src/rlm/function_utils.clj --- a/src/rlm/function_utils.clj Tue Oct 18 00:57:08 2011 -0700 +++ b/src/rlm/function_utils.clj Tue Feb 28 13:26:34 2012 -0600 @@ -4,67 +4,10 @@ (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 mix "Takes any number of mathematically equal functions with possibly different run-times and returns a function that @@ -80,44 +23,40 @@ (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. - - -(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)))) +;; 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))] +;; 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))) - )))))) +;; (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 @@ -148,10 +87,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." @@ -165,21 +105,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 78a630e650d2 -r 8565803376a4 src/rlm/ns_rlm.clj --- a/src/rlm/ns_rlm.clj Tue Oct 18 00:57:08 2011 -0700 +++ b/src/rlm/ns_rlm.clj Tue Feb 28 13:26:34 2012 -0600 @@ -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 78a630e650d2 -r 8565803376a4 src/rlm/rlm_commands.clj --- a/src/rlm/rlm_commands.clj Tue Oct 18 00:57:08 2011 -0700 +++ b/src/rlm/rlm_commands.clj Tue Feb 28 13:26:34 2012 -0600 @@ -1,27 +1,23 @@ (ns rlm.rlm-commands - (:require [rlm ns-rlm shell-write] - [clojure.contrib [duck-streams :as ds]]) - (:use [clojure.contrib java-utils]) + (: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 +51,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,47 +63,41 @@ (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 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 rlm [] +;; (clojure.core/require 'rlm.light-base) +;; (rlm.ns-rlm/ns-clone rlm.light-base)) (defn help "load a bunch of really useful help functions" [] (use '[rlm - [function-utils :only [mix defmix runonce]] - [rlm-commands :only [undef ns-reset ns-nuke reload keymap-clojure keymap-normal rlm]] - [ns-rlm :only [ns-clear ns-clone ls]] - [play-all :only [play-all-music]] + [function-utils :only [mix defmix]] + [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 map-vals 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 :only [javadoc add-local-javadoc]]]) + '[clojure.java + [javadoc :only [javadoc add-local-javadoc]]]) (clojure.java.javadoc/add-local-javadoc "/home/r/cortex/jme3/dist/javadoc") (clojure.java.javadoc/add-local-javadoc "/home/r/roBin/jdk6-docs/docs/api") (clojure.java.javadoc/add-local-javadoc