Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
package leak;
import clojure.lang.IFn;
import clojure.lang.RT;
import clojure.lang.Symbol;
public class Klass {
static {
RT.var("clojure.core", "require").invoke(Symbol.intern("leak.leaky"));
}
@cgrand
cgrand / van-laarhoven-lenses.clj
Created January 28, 2015 02:04
Van Laarhoven lenses in Clojure
;; lenses in Clojure again, van Laarhoven-style this time
(defprotocol Lens
(through [lens x cont]))
(extend-protocol Lens
clojure.lang.Keyword
(through [k x cont]
(cont #(assoc x k %) (k x))))
@cgrand
cgrand / connect.clj
Created February 11, 2015 08:54
for Nic Ferrier and Arnaud Bailly
;; no general let rec in clojure (there's its limited form letfn)
;; let s = connect arg (\data -> handle s data)
(let [s (promise)]
(deliver s (connect (fn [data] (handle @s data))))
@s)
;; you can tie the knot in other places (and other ways too: promises are not the only way)
@cgrand
cgrand / numbers.clj
Created February 11, 2015 14:16
functional numerals
;; a number is a function of 3 args: f0, f1 and init.
;; f0 and f1 are functions of one arg: the half of the current number
;; init is returned only by zero
;; I could have done with only one f taking two arguments (the bit and the half).
;; I also prefer this to a true right fold representation.
(defn zero [f0 f1 init] init) ; zero returns init
(defn one [f0 f1 init] (f1 zero)) ; one calls f1 on its half (zero)
(defn x2
(cond-> (remix-wrapper-doc doc body-tag a-charte appli)
(nil? menu) (doto (-> (.select ".barretitretitre") (.attr "style" "padding-top:30px"))))
@cgrand
cgrand / transmogrify.clj
Last active August 29, 2015 14:18
transmogrify->> rewrites last-threaded forms to use transducers.
(defmulti transmogrify
"Rewrites the last form of a thread-last to use transducer (if possible)."
(fn [f xform src & args] f))
(defmacro transmogrify->>
"Like ->> but uses transducers"
([x] x)
([src & xs]
(let [end (last xs)
xforms (butlast xs)
;; is a macro really called for? when evaluating defs?
;;
;; for M2M instead of a macro woudn't it be better to upgrade your own repl (capture all streams and don't let the real repl handle this)
;;
;; also for M2M you can choose to pretty print on the client side and use tagged literals to make abbreviated datastructure (*print-level* and *print-length*)
;; readable so that further printing can be done on demand while the user browse the data.
(defmacro rpc [& body]
`(let [out-eval# (java.io.StringWriter.)
err-eval# (java.io.StringWriter.)
out-print# (java.io.StringWriter.)
@cgrand
cgrand / kvbitmap.clj
Created July 6, 2015 15:28
creating maps
;; my branch
ser=> Map of size 16
WARNING: Final GC required 8.58679825463792 % of runtime
Evaluation count : 289770 in 6 samples of 48295 calls.
Execution time mean : 2,112323 µs
Execution time std-deviation : 122,879960 ns
Execution time lower quantile : 2,003930 µs ( 2,5%)
Execution time upper quantile : 2,295635 µs (97,5%)
Overhead used : 2,012747 ns
@cgrand
cgrand / all.tsv
Last active August 29, 2015 14:24
kvbitmap vs master
We can make this file beautiful and searchable if this error is corrected: It looks like row 5 should actually have 1 column, instead of 6. in line 4.
PersistentHashMap benchmark: kvbitmap vs master
results are kvbitmap runtime / master runtime
below 100% kvbitmap is faster
above 100% master is faster
size assoc! assoc get dissoc dissoc!
16 84,64% 91,21% 96,56% 99,84% 89,91%
64 80,12% 91,66% 98,66% 104,97% 92,78%
256 89,37% 99,33% 97,59% 111,69% 98,20%
@cgrand
cgrand / hilbert.clj
Created August 25, 2015 11:59
to and from 2d coordiante to distance on the hilbert curve
(defn hilbert-distance [n x y]
(loop [s (bit-shift-right n 1) d 0 x (long x) y (long y)]
(if (pos? s)
(let [rx (bit-and x s)
ry (bit-and y s)
d (+ d (* s (bit-xor (* 3 rx) ry)))
s' (bit-shift-right s 1)]
(if (zero? ry)
(if (zero? rx)
(recur s' d y x)