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 / 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
(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)]
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 '[ :as avl]
'[net.cgrand.xforms :as x])
(defn join [keyfn xforms-map]
(x/multiplex xforms-map)
michalmarczyk / order3.clj
Created Nov 18, 2017
Compute permutation that sorts the input
View order3.clj
;; Written to answer
;; See 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 / order2.clj
Created Nov 18, 2017
Compute permutation that sorts the input
View order2.clj
;; Written to answer
(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:
(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."
(doseq [s (keys (ns-interns ns))
michalmarczyk / locals_loops.clj
Created Jan 17, 2017
Clojure local shadowing, loop semantics
View locals_loops.clj
;;; See the comment threads here:
(loop [x 1
f (fn [] x)]
(if (== 1 x)
(recur 0 f)
;= 1
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 ()
(set (make-local-variable 'lisp-indent-function)
(set (make-local-variable 'lisp-doc-string-elt-property)
(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)]
(>! c (rand))
(>! comm-chan c)
View validate_under.clj
(defn validate-under [attr validator]
(fn [m]
(let [result (validator (get m attr))]
[(empty? result) (v/nest attr result)])))
(def example-v
(validate-under :foo (v/validation-set (v/presence-of :bar)))))
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.