Skip to content

Instantly share code, notes, and snippets.

@yuanmai
yuanmai / gist:5472262
Created April 27, 2013 07:53
point free
(defn map-vals [f coll]
(into (empty coll) (map (juxt key (comp f val)) coll)))
@yuanmai
yuanmai / gist:5472034
Created April 27, 2013 05:49
Record print-method in Clojure
(defmethod print-method RTable [tble ^String out]
"RTables print as SQL92 compliant SQL"
(when *debug*
(doseq [[k v] tble]
(.write out (format "%s\t\t\t\t%s\n" (str k) (str v)))))
(.write out (-> tble (compile nil) interpolate-sql)))
ko.extenders.invalidatedBy = function(target, which) {
which.subscribe(function(newValue) {
target("");
});
return target;
};
@yuanmai
yuanmai / gist:5362688
Created April 11, 2013 11:34
recompile el
C-u 0 M-x byte-recompile-directory
@yuanmai
yuanmai / entropy.clj
Last active December 14, 2015 04:19
Shannon Entropy
(defn shannon-entropy [col]
(let [xs (vals (frequencies col))
n (apply + xs)
ln2 (Math/log 2)]
(-
(apply +
(for [x xs]
(let [a (/ x n)]
(* a (/ (Math/log a) ln2))))))))
@yuanmai
yuanmai / project.clj
Created February 5, 2013 08:21
Got "The input line is too long." while executing "lein.bat trampoline run". It happened right after copying resource files using lein-resource plugin
(defproject foo "0.1.0-SNAPSHOT"
:description "Foo website"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.springframework.security/spring-security-web "3.1.0.RELEASE"]
[org.springframework.security/spring-security-config "3.1.0.RELEASE"]
[org.clojure/clojure "1.4.0"]
[ring "1.1.6"]
[org.slf4j/slf4j-api "1.6.1"]
sudo tcpdump -i en0 -n -s 0 -w - | grep -a -o -E "Host\: .*|GET \/.*"
#!/bin/sh
kill $(lsof -t -i :8080)
@yuanmai
yuanmai / gist:4586501
Created January 21, 2013 14:36
deliver / promise, look ma no callbacks
(defn check-item [p id]
(if-let [item (find-item id)]
(do (deliver p item)
nil)
(status 404 "Item not found")))
(defn check-access [item]
(when-not (can-user-access? item)
(status 403 "Go away")))
@yuanmai
yuanmai / gist:4467322
Created January 6, 2013 14:00
clone-for does not work well with single letter tag
user> (use 'net.cgrand.enlive-html)
nil
user> (sniptest "<tr><bb><div></div></bb></tr>"
[:tr] (clone-for [option-value-map [1]]
[:div] (content "a")))
"<tr><bb><div>a</div></bb></tr>"
user> (sniptest "<tr><b><div></div></b></tr>"
[:tr] (clone-for [option-value-map [1]]
[:div] (content "a")))
"<tr></tr><b></b><div><b></b></div><b></b>"