Created
July 21, 2010 06:00
-
-
Save rlm/484131 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns | |
mobius.utils.function-utils | |
"Collection of Operators on Pure Functions" | |
{:author "Robert McIntyre"} | |
(:use [clojure.contrib.profile])) | |
(defn mix | |
" Takes any number of mathematically equal functions with possibly different run-time properties and | |
returns a function that runs each in a separate thread, returns the result from the first thread | |
which finishes, and cancels the other threads." | |
[& functions] | |
(fn [& args] | |
(let [result (prof :create-atom (atom ::void)) | |
futures (prof :create-futures (doall (map | |
(fn [fun](future | |
(reset! result (apply fun args)))) functions)))] | |
(prof :loop (loop [] | |
(if (= (deref result) ::void) | |
(recur) | |
(do (prof :future-cancel (dorun (map future-cancel futures))) | |
(prof :return (deref result))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment