Mercurial > cortex
diff org/util.org @ 164:c33a8e5fe7bc
removed hearing-init-fns requirement, changed names
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 03:29:59 -0700 |
parents | 33278bf028e7 |
children | deac7b708750 |
line wrap: on
line diff
1.1 --- a/org/util.org Sat Feb 04 02:15:32 2012 -0700 1.2 +++ b/org/util.org Sat Feb 04 03:29:59 2012 -0700 1.3 @@ -202,6 +202,19 @@ 1.4 keeping the keys the same." 1.5 [f m] (zipmap (keys m) (map f (vals m)))) 1.6 1.7 +(defn runonce 1.8 + "Decorator. returns a function which will run only once. 1.9 + Inspired by Halloway's version from Lancet." 1.10 + {:author "Robert McIntyre"} 1.11 + [function] 1.12 + (let [sentinel (Object.) 1.13 + result (atom sentinel)] 1.14 + (fn [& args] 1.15 + (locking sentinel 1.16 + (if (= @result sentinel) 1.17 + (reset! result (apply function args)) 1.18 + @result))))) 1.19 + 1.20 1.21 #+end_src 1.22