diff 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 diff
     1.1 --- a/src/rlm/function_utils.clj	Tue Dec 27 23:27:22 2011 -0700
     1.2 +++ b/src/rlm/function_utils.clj	Wed Jan 18 05:18:57 2012 -0700
     1.3 @@ -80,6 +80,22 @@
     1.4  	 (dorun (map future-cancel futures))
     1.5  	 answer))))
     1.6  
     1.7 +(defn mix-pred
     1.8 +  "Takes any number of mathematically equal functions with
     1.9 +   possibly different run-times and returns a function that
    1.10 +   runs each in a separate thread, returns the result from
    1.11 +   the first thread which finishes, and cancels the other threads."
    1.12 +  {:author "Robert McIntyre"}
    1.13 +  ([pred & functions]
    1.14 +     (fn [& args]
    1.15 +       (let [result (promise)
    1.16 +	     futures (doall (for [fun functions]
    1.17 +                              (let [answer (apply fun args)]
    1.18 +                                (if (pred answer)
    1.19 +                                  (future (deliver result (apply fun args)))))))
    1.20 +	     answer @result]
    1.21 +	 (dorun (map future-cancel futures))
    1.22 +	 answer))))
    1.23  
    1.24  
    1.25