Skip to content

Instantly share code, notes, and snippets.

View dgrnbrg's full-sized avatar

David Greenberg dgrnbrg

View GitHub Profile
(when-not (has-root-binding? (var name))
(def name expr))
@dgrnbrg
dgrnbrg / gist:1674683
Created January 25, 2012 04:19
dynamic var
(def #^:dynamic foo nil)
(def ^:dynamic foo 0)
((binding [foo 22]
(bound-fn [name]
(println "Hi" name "foo is" foo))) "David")
(assert (= foo 0))
(my-bound-fn "David")
(assert (= foo 0))
;Prints "Hi David foo is 22" but called outside the binding!
(def #^:dynamic foo 0)
(def my-bound-fn
(binding [foo 22]
(bound-fn [name]
(println "Hi" name "foo is" foo)))
(assert (= foo 0))
(my-bound-fn "David")
(assert (= foo 0))
;Prints "Hi David foo is 22" but called outside the binding!
(def ^:dynamic foo nil)
(defmacro defn-errors
"Like defn, but allows any number of the
argument to throw errors, using join-errors"
[name & args]
(let [[name [params & body]] (name-with-attributes
name args)
param-sym (gensym "params")]
`(defmacro ~name [& ~param-sym]
`(apply (fn [~@'~params] ~@'~body)
(join-errors
(->> (iterate (partial prewalk-replace expansions) unexpanded)
(mapcat #(vector % %))
drop
(partition 2)
(take-while (fn [[x y]] (not= x y))))
@dgrnbrg
dgrnbrg / gist:1996228
Created March 7, 2012 21:02
Rate limit clojure fn
(defn rate-limit [interval s]
(let [cache (atom {})]
(->> s
(map #(vector % (System/currentTimeMillis)))
(filter (fn [[x t]] (< (+ (get @cache x 0) interval) t)))
(map (fn [[x t]] (swap! cache assoc x t) x)))))
@dgrnbrg
dgrnbrg / gist:2321318
Created April 6, 2012 16:58
guzheng ast rewrite
(do
(ns guzheng.sample)
(defn
do-hello
[]
(println "hello")
(if
true
(clojure.core/let
[count__782__auto__
@dgrnbrg
dgrnbrg / gist:2323127
Created April 6, 2012 21:36
guzheng lein
(ns leiningen.guzheng
(:use [leiningen.compile :only [eval-in-project]])
(:use guzheng.core))
(defn guzheng [project & nses]
(eval-in-project
project `(do
(clojure.test/run-tests 'guzheng.test.core))
'(require 'guzheng.core 'clojure.test)))
(comment(guzheng.core/run-test-instrumented