Skip to content

Instantly share code, notes, and snippets.

@rlm
Created July 21, 2010 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlm/484131 to your computer and use it in GitHub Desktop.
Save rlm/484131 to your computer and use it in GitHub Desktop.
(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