1.2.0 permalink Arrow_down_16x16

global-singleton

clojure.contrib.singleton

  • (global-singleton f)
Returns a global singleton function. f is a function of no
arguments that creates and returns some object. The singleton
function will call f just once, the first time it is needed, and
cache the value for all subsequent calls.

Warning: global singletons are often unsafe in multi-threaded code.
Consider per-thread-singleton instead.

0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/singleton.clj:28 top

(defn global-singleton
  "Returns a global singleton function.  f is a function of no
  arguments that creates and returns some object.  The singleton
  function will call f just once, the first time it is needed, and
  cache the value for all subsequent calls.

  Warning: global singletons are often unsafe in multi-threaded code.
  Consider per-thread-singleton instead."
  [f]
  (let [instance (atom nil)
        make-instance (fn [_] (f))]
    (fn [] (or @instance (swap! instance make-instance)))))
Vars in clojure.contrib.singleton/global-singleton: defn fn let or swap! deref
Used in 0 other vars

Comments top

No comments for global-singleton. Log in to add a comment.