Skip to content

Instantly share code, notes, and snippets.

@michalmarczyk
michalmarczyk / delegating-proxy.clj
Created February 1, 2012 07:53
A ridiculous proxy macro which delegates calls to methods which have not been explicitly implemented to a specified object.
;;; Written when pondering
;;; http://stackoverflow.com/questions/9086926/create-a-proxy-for-an-specific-instance-of-an-object-in-clojure
(defmacro delegating-proxy [o class-and-ifaces ctor-args & impls]
(let [oname (gensym)]
(letfn [(delegating-impls [^java.lang.reflect.Method ms]
(let [mname (symbol (.getName ^java.lang.reflect.Method (first ms)))
arity-groups (partition-by #(count (.getParameterTypes ^java.lang.reflect.Method %)) ms)
max-arity (max-key #(count (.getParameterTypes ^java.lang.reflect.Method %)) ms)]
`(~mname
@michalmarczyk
michalmarczyk / core.clj
Created September 1, 2011 21:28
A nestable with-test-tags macro to tag clojure.test tests for use with external tools.
;;; See
;;; http://stackoverflow.com/questions/7240947/is-a-transparent-macrolet-possible
;;; for a discussion.
;;; Also see
;;; https://gist.github.com/1185513
;;; for an earlier approach.
;;; src/deftest-magic/core.clj
@michalmarczyk
michalmarczyk / core.clj
Created September 1, 2011 05:31
A nestable with-test-tags macro to tag clojure.test tests for use with external tools
;;; See
;;; http://stackoverflow.com/questions/7240947/is-a-transparent-macrolet-possible
;;; for a discussion.
;;; src/deftest_magic/core.clj
(ns deftest-magic.core
(:use [clojure.tools.macro :only [macrolet]]))
(comment
;;; See the inspirational SO question: http://stackoverflow.com/questions/3346382
(require 'clojure.contrib.trace)
(defn trace-ns
"Replaces each function from the given namespace with a version wrapped
in a tracing call. Can be undone with untrace-ns. ns should be a namespace
object or a symbol."
[ns]
(doseq [s (keys (ns-interns ns))
@michalmarczyk
michalmarczyk / letrec.clj
Created July 23, 2010 01:21
a letrec macro for Clojure
(defmacro letrec [bindings & body]
(let [bcnt (quot (count bindings) 2)
arrs (gensym "bindings_array")
arrv `(make-array Object ~bcnt)
bprs (partition 2 bindings)
bssl (map first bprs)
bsss (set bssl)
bexs (map second bprs)
arrm (zipmap bssl (range bcnt))
btes (map #(walk/prewalk (fn [f]
(defn unuse [ns]
(doseq [[n v] (ns-refers *ns*)]
(if (= (.. v ns name) ns)
(ns-unmap *ns* n))))
(defn reuse [ns]
(unuse ns)
(remove-ns ns)
(use :reload-all ns))
(comment
(:b (DefaultMap. :foo {:a 1}))
; => :foo
(:a (DefaultMap. :foo {:a 1}))
; => 1
(merge-with conj (DefaultMap. [] {}) {:a 1} {:a 2} {:a 3})
; => {:a [1 2 3]}
)
;;; method implementations basically taken from clojure.core/emit-defrecord
(defn intern-alias
"Interns a Var called alias-sym in the namespace given by ns-or-sym
(which may be a symbol or an actual namespace object; defaults to
the current namespace). The binding of the new Var will be that of
the Var specified as var-or-sym; also, all metadata is copied from
the original to the alias. The original may be specified as either
a Var or a symbol."
([alias-sym var-or-sym]
(intern-alias *ns* alias-sym var-or-sym))
([ns-or-sym alias-sym var-or-sym]
We couldn’t find that file to show.
java -cp "d:/xPrograms/leiningen/1.2.0/leiningen-1.2.0-RC2-standalone.jar;src;classes" leiningen.core uberjar
Exception in thread "main" java.lang.Exception: Unable to resolve symbol: defproject in this context (project.clj:1)
at clojure.lang.Compiler.analyze(Compiler.java:5200)
at clojure.lang.Compiler.analyze(Compiler.java:5146)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3031)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:5366)
at clojure.lang.Compiler.analyze(Compiler.java:5185)
at clojure.lang.Compiler.analyze(Compiler.java:5146)
at clojure.lang.Compiler.eval(Compiler.java:5423)
at clojure.lang.Compiler.load(Compiler.java:5852)