Skip to content

Instantly share code, notes, and snippets.

Michał Marczyk michalmarczyk

Block or report user

Report or block michalmarczyk

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@michalmarczyk
michalmarczyk / delegating-proxy.clj
Created Feb 1, 2012
A ridiculous proxy macro which delegates calls to methods which have not been explicitly implemented to a specified object.
View delegating-proxy.clj
;;; 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 / join_xform.clj
Created Mar 6, 2018
data.avl-based join transducer as discussed in the hallway track of Clojure/conj 2018
View join_xform.clj
(alembic/distill '[org.clojure/data.avl "0.0.17"])
(alembic/distill '[net.cgrand/xforms "0.12.1"])
(require '[clojure.data.avl :as avl]
'[net.cgrand.xforms :as x])
(defn join [keyfn xforms-map]
(comp
(x/multiplex xforms-map)
@michalmarczyk
michalmarczyk / order3.clj
Created Nov 18, 2017
Compute permutation that sorts the input
View order3.clj
;; Written to answer https://stackoverflow.com/questions/47254742/sort-primitive-array-with-custom-comparator-on-clojure
;; See https://gist.github.com/michalmarczyk/11bbfd0b19b6357f533b192bf9da84ac for the single-threaded version
(defn order3 [xs]
(let [rnd (java.util.Random.)
a1 (double-array xs)
a2 (long-array (alength a1))]
(dotimes [i (alength a2)]
(aset a2 i i))
(letfn [(quicksort [^long l ^long h]
@michalmarczyk
michalmarczyk / order2.clj
Created Nov 18, 2017
Compute permutation that sorts the input
View order2.clj
;; Written to answer https://stackoverflow.com/questions/47254742/sort-primitive-array-with-custom-comparator-on-clojure
(defn order2 [xs]
(let [rnd (java.util.Random.)
a1 (double-array xs)
a2 (long-array (alength a1))]
(dotimes [i (alength a2)]
(aset a2 i i))
(letfn [(quicksort [^long l ^long h]
(if (< l h)
View trace-ns.clj
;;; 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 / locals_loops.clj
Created Jan 17, 2017
Clojure local shadowing, loop semantics
View locals_loops.clj
;;; See the comment threads here:
;;; http://stackoverflow.com/questions/41677617/are-all-variables-in-clojure-constant
(loop [x 1
f (fn [] x)]
(if (== 1 x)
(recur 0 f)
(f)))
;= 1
@michalmarczyk
michalmarczyk / clojure-font-lock-setup.el
Created Mar 19, 2010
coloured SLIME REPL for Clojure
View clojure-font-lock-setup.el
;;; all code in this function lifted from the clojure-mode function
;;; from clojure-mode.el
(defun clojure-font-lock-setup ()
(interactive)
(set (make-local-variable 'lisp-indent-function)
'clojure-indent-function)
(set (make-local-variable 'lisp-doc-string-elt-property)
'clojure-doc-string-elt)
(set (make-local-variable 'font-lock-multiline) t)
View producer_consumer.clj
;; 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))
View validate_under.clj
(defn validate-under [attr validator]
(fn [m]
(let [result (validator (get m attr))]
[(empty? result) (v/nest attr result)])))
(comment
(def example-v
(v/validation-set
(validate-under :foo (v/validation-set (v/presence-of :bar)))))
@michalmarczyk
michalmarczyk / thread_or.clj
Created Jul 14, 2013
Short-circuiting logical disjunction of several futures' results
View thread_or.clj
(defn thread-or
"Call each of the fs on a separate thread. Return logical
disjunction of the results. Short-circuit (and cancel the calls to
remaining fs) on first truthy value returned."
[& fs]
(let [ret (promise)
fps (promise)]
(deliver fps
(doall (for [f fs]
(let [p (promise)]
You can’t perform that action at this time.