view src/rlm/function_utils.clj @ 3:c8e35134bf8e

working on qotd.
author Robert McIntyre <rlm@mit.edu>
date Wed, 18 Jan 2012 05:18:57 -0700
parents 78a630e650d2
children 12d1367cf1aa
line wrap: on
line source
1 ;; Various Operators on Pure Functions
2 ;;
3 ;; Open source Liscence and all that
4 (ns
5 rlm.function-utils
6 "Collection of Operators on Pure Functions"
7 {:author "Robert McIntyre"}
8 (:use [clojure.contrib.profile]))
10 (def void ::void)
12 (comment
14 "Please help me out here. I'm trying to make a higher order function that takes
15 pure functions that are mathmaticaly the same but have different times and returns another
16 function that runs them both and returns the resut of the one that finishes first."
19 "here's a repl session:
21 mobius> " (time (dotimes [_ 500]((mix-futures + +) 40 3 3 3 3 3))) "
22 Elapsed time: 85.792995 msecs
23 nil
24 mobius> " (time (dotimes [_ 500](+ 40 3 3 3 3 3))) "
25 Elapsed time: 6.956338 msecs
26 nil
27 mobius> " (time (dotimes [_ 500]((mix-threads + +) 40 3 3 3 3 3))) "
28 Elapsed time: 706.227065 msecs
29 nil
32 mobius> " (defn fast-five [& args] 5) "
33 #'mobius/fast-five
35 mobius> " (defn slow-five [& args] (Thread/sleep 5000) (println "Oh YEAH!!!!") 5) "
36 #'mobius/slow-five
38 mobius> " (profile (time (dotimes [_ 5000] (thread-five)))) "
39 \"Elapsed time: 12187.981587 msecs\"
40 Name mean min max count sum
41 create-atom 9621 2025 4433928 5000 48106830
42 create-threads 5156 2724 2880268 5000 25783796
43 kill-threads 91313 27866 11175718 5000 456567066
44 loop 2124249 38482 18470781 5000 10621249899
45 return 1242 838 5309 5000 6212626
46 start-threads 252985 110348 12272835 5000 1264927953
47 nil
49 mobius> " (profile (time (dotimes [_ 5000] (future-five)))) "
50 \"Elapsed time: 607.266671 msecs\"
51 Name mean min max count sum
52 create-atom 1472 1047 31708 5000 7363139
53 create-futures 30539 2514 1330660 5000 152697998
54 kill-threads 54158 2444 3938833 5000 270792897
55 loop 81117 8800 6083059 5000 405587516
56 return 1215 838 618782 5000 6078146
57 nil
61 What can I improve here, and why is the future version sooo much faster than the
62 thread version? Is there a better way than using loop?
63 "
65 )
68 (defn mix
69 "Takes any number of mathematically equal functions with
70 possibly different run-times and returns a function that
71 runs each in a separate thread, returns the result from
72 the first thread which finishes, and cancels the other threads."
73 {:author "Robert McIntyre"}
74 ([& functions]
75 (fn [& args]
76 (let [result (promise)
77 futures (doall (for [fun functions]
78 (future (deliver result (apply fun args)))))
79 answer @result]
80 (dorun (map future-cancel futures))
81 answer))))
83 (defn mix-pred
84 "Takes any number of mathematically equal functions with
85 possibly different run-times and returns a function that
86 runs each in a separate thread, returns the result from
87 the first thread which finishes, and cancels the other threads."
88 {:author "Robert McIntyre"}
89 ([pred & functions]
90 (fn [& args]
91 (let [result (promise)
92 futures (doall (for [fun functions]
93 (let [answer (apply fun args)]
94 (if (pred answer)
95 (future (deliver result (apply fun args)))))))
96 answer @result]
97 (dorun (map future-cancel futures))
98 answer))))
102 (defn mix-threads
103 " Takes any number of pure functions that take the same arguments and
104 compute the same value and returns a function that runs each in a separate
105 thread, returns the result from the first thread which finshes, and cancels
106 the other threads. Explicitly uses nasty Threads.
108 For example:
109 (do
110 (defn fun1 [] (Thread/sleep 5000) 5)
111 (defn fun2 [] (Thread/sleep 700000) 5)
112 (time ((mix fun1 fun2))))
114 Returns:
115 | Elapsed time: 5000.66214 msecs
116 5"
117 [& functions]
118 (fn [& args]
119 (let [result (prof :create-atom (atom void))
120 threads
121 (prof :create-threads (map
122 (fn [fun]
123 (Thread.
124 (fn []
125 (try (let [answer (apply fun args)]
126 (reset! result answer))
127 (catch Exception _ nil)))))
128 functions))]
130 (prof :start-threads (dorun (map #(.start %) threads)))
131 (prof :loop (loop []
132 (if (= (deref result) void)
133 (recur)
134 (do (prof :kill-threads (dorun (map #(.stop %) threads)))
135 (prof :return (deref result)))
136 ))))))
138 (defmacro defmix
139 " Defines a function from any number of pure functions that take the same
140 arguments and compute the same value which:
142 Runs each in a separate thread.
143 Returns the result from the first thread which finshes.
144 Cancels the other threads.
146 Use this whenever you want to combine two pure functions that
147 compute the same thing, but use different algorithms with different
148 run times for various inputs.
150 For example:
151 (do
152 (defn fun1 [] (Thread/sleep 5000) 5)
153 (defn fun2 [] (Thread/sleep 700000) 5)
154 (defmix fun3 \"combination of fun1 and fun2\" fun1 fun2)
155 (time (fun3))
157 Returns:
158 | Elapsed time: 5000.66214 msecs
159 5"
161 {:arglists '([name doc-string? functions*])}
163 [name & functions]
164 (let [doc-string (if (string? (first functions)) (first functions) "")
165 functions (if (string? (first functions)) (rest functions) functions)
166 arglists (:arglists (meta (resolve (eval `(quote ~(first functions))))))
167 name (with-meta name (assoc (meta name) :arglists `(quote ~arglists) :doc doc-string))]
168 `(def ~name (mix ~@functions))))
171 (defn runonce
172 "Decorator. returns a function which will run only once. Inspired
173 by Halloway's version from lancet."
174 {:author "Robert McIntyre"}
175 [function]
176 (let [sentinel (Object.)
177 result (atom sentinel)]
178 (fn [& args]
179 (locking sentinel
180 (if (= @result sentinel)
181 (reset! result (apply function args))
182 @result)))))
186 ;I'm thinking this will be the docstring for mix eventually.
188 ;; " Takes any number of pure functions that take the same arguments and
189 ;; compute the same value and returns a function that runs each in a separate
190 ;; thread, returns the result from the first thread which finshes, and cancels
191 ;; the other threads.
193 ;; For example:
194 ;; (do
195 ;; (defn fun1 [] (Thread/sleep 5000) 5)
196 ;; (defn fun2 [] (Thread/sleep 700000) 5)
197 ;; (time ((mix fun1 fun2))))
199 ;; Returns:
200 ;; | Elapsed time: 5000.66214 msecs
201 ;; 5"