Mercurial > rlm
comparison 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 |
comparison
equal
deleted
inserted
replaced
2:c7df1ea6fd71 | 3:c8e35134bf8e |
---|---|
78 (future (deliver result (apply fun args))))) | 78 (future (deliver result (apply fun args))))) |
79 answer @result] | 79 answer @result] |
80 (dorun (map future-cancel futures)) | 80 (dorun (map future-cancel futures)) |
81 answer)))) | 81 answer)))) |
82 | 82 |
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)))) | |
83 | 99 |
84 | 100 |
85 | 101 |
86 (defn mix-threads | 102 (defn mix-threads |
87 " Takes any number of pure functions that take the same arguments and | 103 " Takes any number of pure functions that take the same arguments and |