This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
NewerOlder