ClojureDocs

Nav

Namespaces

bound-fn*

clojure.core

Available since 1.1 (source)
  • (bound-fn* f)
Returns a function, which will install the same bindings in effect as in
the thread at the time bound-fn* was called and then call f with any given
arguments. This may be used to define a helper function which runs on a
different thread, but needs the same bindings in place.
2 Examples
(def ^:dynamic *some-var* nil)

(defn f [] (println *some-var*))

;; run f without a new binding
user=> (f)
nil
nil

;; run f with a new binding
user=> (binding [*some-var* "hello"]
         (f))
hello
nil

;; run f in a thread with a new binding
user=> (binding [*some-var* "goodbye"]
         (.start (Thread. f)))
nil
nil

;; run a bound f in a thread with a new binding
user=> (binding [*some-var* "goodbye"]
         (.start (Thread. (bound-fn* f))))
goodbye
nil
;; Capture thread local bindings so the lazy sequence can realize outside
;; the binding block

(def ^:dynamic *a* nil)

(binding [*a* 1] (map #(+ % *a*) (range 10)))
;; NullPointerException   clojure.lang.Numbers.ops (Numbers.java:1013)

(binding [*a* 1] (map (bound-fn* #(+ % *a*)) (range 10)))
;; (1 2 3 4 5 6 7 8 9 10)
See Also

Returns a function defined by the given fntail, which will install the same bindings in effect as ...

Added by klauern
0 Notes
No notes for bound-fn*