Skip to content

Instantly share code, notes, and snippets.

(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
@michalmarczyk
michalmarczyk / letrec2.clj
Created May 16, 2014 19:57
letrec for Clojure using tools.macro's symbol-macrolet
;(use '[clojure.tools.macro :only [symbol-macrolet]])
(defmacro letrec
"Like let, but the bindings may be mutually recursive, provided that
the heads of all values can be evaluated independently.
This means that functions, lazy sequences, delays and the like can
refer to other bindings regardless of the order in which they
appear in the letrec form."
[bindings & body]
@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 / join_xform.clj
Created March 6, 2018 02:50
data.avl-based join transducer as discussed in the hallway track of Clojure/conj 2018
(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 November 18, 2017 05:39
Compute permutation that sorts the input
;; 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 November 18, 2017 05:36
Compute permutation that sorts the input
;; 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)
;;; 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 January 17, 2017 00:13
Clojure local shadowing, loop semantics
;;; 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 March 19, 2010 06:02
coloured SLIME REPL for Clojure
;;; 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)
;; 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))