aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/singleton.clj
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2009-04-09 16:53:55 +0000
committerStuart Sierra <mail@stuartsierra.com>2009-04-09 16:53:55 +0000
commit23eec91a3202a7c673d958c8a77a84b5397908af (patch)
treee1f32b517a8537365aad20ef54a45cae6fa16d9a /src/clojure/contrib/singleton.clj
parent72336027585b54254ddc17ad9c90541e2a09c789 (diff)
singleton.clj: new lib for initializing global singleton instances
Diffstat (limited to 'src/clojure/contrib/singleton.clj')
-rw-r--r--src/clojure/contrib/singleton.clj27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/clojure/contrib/singleton.clj b/src/clojure/contrib/singleton.clj
new file mode 100644
index 00000000..09664835
--- /dev/null
+++ b/src/clojure/contrib/singleton.clj
@@ -0,0 +1,27 @@
+;;; singleton.clj: singleton functions
+
+;; by Stuart Sierra, http://stuartsierra.com/
+;; April 9, 2009
+
+;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
+;; and distribution terms for this software are covered by the Eclipse
+;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+;; which can be found in the file epl-v10.html at the root of this
+;; distribution. By using this software in any fashion, you are
+;; agreeing to be bound by the terms of this license. You must not
+;; remove this notice, or any other, from this software.
+
+
+(ns singleton)
+
+(defn singleton
+ "Returns a memoized version of a function with no arguments. The
+ memoized version caches the function's return value.
+
+ This is useful for lazily creating global objects that are expensive
+ to initialize. Warning: Make sure you really want a single global
+ instance, and not one instance per thread."
+ [f]
+ (let [instance (atom nil)
+ make-instance (fn [_] (f))]
+ (fn [] (or @instance (swap! instance make-instance)))))