Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / new-trace-ns.clj
Created February 17, 2012 00:46
Mass tracing utilities for Clojure
;;; See the inspirational SO question: http://stackoverflow.com/questions/3346382
;;; My old Gist with my first take on this: https://gist.github.com/492764
;;; Don Jackson's Gist based on the above: https://gist.github.com/1846993
;;; Licence: EPLv1, the same as Clojure
(in-ns 'clojure.tools.trace)
(defn trace-var
"If the specified Var holds an IFn and is not marked as a macro, its
@michalmarczyk
michalmarczyk / phm.js
Created April 29, 2012 02:35
ClojureScript map impl perf tests
function c(a){throw a;}var g=!0,k=null,l=!1;function aa(){return function(a){return a}}function m(a){return function(){return this[a]}}function n(a){return function(){return a}}var o,ba=this;
function q(a){var b=typeof a;if("object"==b)if(a){if(a instanceof Array)return"array";if(a instanceof Object)return b;var d=Object.prototype.toString.call(a);if("[object Window]"==d)return"object";if("[object Array]"==d||"number"==typeof a.length&&"undefined"!=typeof a.splice&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if("[object Function]"==d||"undefined"!=typeof a.call&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("call"))return"function"}else return"null";
else if("function"==b&&"undefined"==typeof a.call)return"object";return b}function s(a){return void 0!==a}function ca(a){return"string"==typeof a}function da(a){return a[ea]||(a[ea]=++fa)}var ea="closure_uid_"+Math.floor(2147483648*Math.random()).toString(36),fa=0;function t(a,b){var d=a.split(
@michalmarczyk
michalmarczyk / phm.js
Created April 30, 2012 05:35
ClojureScript perf tests (large map conversion threshold)
function c(a){throw a;}var g=!0,k=null,l=!1;function aa(){return function(a){return a}}function m(a){return function(){return this[a]}}function n(a){return function(){return a}}var o,ba=this;
function q(a){var b=typeof a;if("object"==b)if(a){if(a instanceof Array)return"array";if(a instanceof Object)return b;var d=Object.prototype.toString.call(a);if("[object Window]"==d)return"object";if("[object Array]"==d||"number"==typeof a.length&&"undefined"!=typeof a.splice&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if("[object Function]"==d||"undefined"!=typeof a.call&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("call"))return"function"}else return"null";
else if("function"==b&&"undefined"==typeof a.call)return"object";return b}function r(a){return void 0!==a}function ca(a){return"string"==typeof a}function da(a){return a[ea]||(a[ea]=++fa)}var ea="closure_uid_"+Math.floor(2147483648*Math.random()).toString(36),fa=0;function s(a,b){var d=a.split(
@michalmarczyk
michalmarczyk / merge-sorted.clj
Created May 1, 2012 03:28
Multiway merge of sorted sequences
(defn merge-sorted [comparator & xss]
(let [xss (into-array Object (remove empty? xss))
pq (java.util.PriorityQueue.
(count xss) #(comparator (val %1) (val %2)))]
(dotimes [i (count xss)]
(let [xs (aget xss i)]
(.add pq (pair i (first xs)))
(aset xss i (next xs))))
((fn go []
(lazy-seq
@michalmarczyk
michalmarczyk / timings.cljs
Created May 4, 2012 23:53
Benchmarks for CLJS protocol dispatch
(ns timings)
(defn -main []
(println ";;; satisfies?")
(println "(satisfies? ISeq (list 1 2 3))")
(let [coll (list 1 2 3)] (time (dotimes [_ 10000000] (satisfies? ISeq coll))))
(println "(satisfies? ISeq [1 2 3])")
(let [coll [1 2 3]] (time (dotimes [_ 10000000] (satisfies? ISeq coll))))
(println)
@michalmarczyk
michalmarczyk / same_fringe.clj
Last active December 19, 2015 10:09
Same Fringe with core.async
(defn same-fringe
"http://c2.com/cgi/wiki?SameFringeProblem"
[t1 t2]
(letfn [(walk [t c]
(go (if (seq? t)
(doseq [t' t]
(<! (walk t' c)))
(>! c t))))]
(let [c1 (chan)
c2 (chan)]
;; [1 2 3] -> [1 [2 [3]]]
(defn f [xs]
(if (next xs)
[(first xs) (vec (f (next xs)))]
(vec xs)))
(defn g [v]
(reduce (fn [acc x]
[x acc])
;; for now, consumer starts producer
;; -- must make sure that output chan already on comm-chan before producer starts
(defn producer [comm-chan]
(go (loop []
(if-let [c (<! comm-chan)]
(do
(>! c (rand))
(>! comm-chan c)
(recur))