Skip to content

Instantly share code, notes, and snippets.

View ericnormand's full-sized avatar

Eric Normand ericnormand

View GitHub Profile
@ericnormand
ericnormand / mklex.clj
Created June 1, 2013 23:26
A slight reworking of the docstring for mkscope plus a new, similar function for creating lexical scopes.
(defn mkscope
"Create a rule which contains the scope of the given rule. Bindings
made in rule do not escape this rule's scope.
<code>mkscope</code> creates a scope protector around a rule so that
bindings that the given rule creates do not leak into the current
scope. This function should be used around your own rules.
Example:
@ericnormand
ericnormand / autocurry.js
Last active December 18, 2015 13:58
A Javascript function for automatically creating a currying version of a function. It takes the number of arguments of the function (must be fixed) and the function.
function autoCurry(n, f) {
return function() {
var args1 = Array.prototype.slice.apply(arguments),
len = args1.length;
if(len === n) {
return f.apply(this, args1);
} else if(len > n) {
throw 'Too many arguments.';
} else {
return autoCurry(n - len, function() {
@ericnormand
ericnormand / channel-macro.clj
Created July 16, 2013 15:21
A macro for turning a function with a callback into an async channel.
(defn sub
[s form]
(cond
(= '<&> form) s
(seq? form) (map (partial sub s) form)
(list? form) (map (partial sub s) form)
(vector? form) (mapv (partial sub s) form)
(map? form) (into {} (for [[k v] form] [(sub s k) (sub s v)]))
(set? form) (into #{} (map (partial sub s) form))
:else form))
@ericnormand
ericnormand / arities.clj
Created August 16, 2013 02:26
A function to tell you the possible arities of a function.
(defn arities [f]
(sort (for [m (.getDeclaredMethods (class f))
:when (= "invoke" (.getName m))]
(alength (.getParameterTypes m)))))
@ericnormand
ericnormand / version.clj
Created March 6, 2014 17:46
Insert a version string into ClojureScript output
(def ts (time/formatters :date-time-no-ms))
(defn git-hash []
(let [p (.exec (Runtime/getRuntime) (into-array String ["/bin/sh" "-c"
"git rev-list --format=format:'%ct' --max-count=1 `git rev-parse HEAD`"]))]
(.waitFor p)
(let [s (-> (.getInputStream p)
slurp
string/trim)
_ (prn s)
@ericnormand
ericnormand / wrap-logging.clj
Last active August 29, 2015 14:02
A middleware to log Ring request/response to a file.
(def logger (agent nil))
(def logfile (java.io.FileWriter. "output.edn"))
(defn log [a x]
(binding [*out* logfile
*print-length* 200]
(prn x)))
(defn wrap-logging [hdlr]
(fn [req]
@ericnormand
ericnormand / lens.clj
Last active August 29, 2015 14:03
Clojure Lens Implementation
(defn lens [getter setter]
(fn [fmap vf in]
(fmap (partial setter in)
(vf (getter in)))))
(defn lupdate [lens f in]
(lens (fn [f a] (f a))
f
in))
@ericnormand
ericnormand / unconditional.clj
Created October 14, 2014 04:31
Who needs conditionals in Clojure?
(defn- truth [then else] (then))
(defn- falsehood [then else] (else))
(def boolness {false falsehood nil falsehood})
(defn if* [condition then-fn else-fn]
((get boolness condition truth) then-fn else-fn))
(defmacro my-if [test then else]
`(if* ~test (fn [] ~then) (fn [] ~else)))
@ericnormand
ericnormand / EdnTester.java
Created November 28, 2014 21:27
Examples how to handle EDN messages to/from sente
package edntester;
import us.bpsm.edn.Keyword;
import us.bpsm.edn.parser.Parseable;
import us.bpsm.edn.parser.Parser;
import us.bpsm.edn.parser.Parsers;
import us.bpsm.edn.printer.Printers;
import java.util.ArrayList;
@ericnormand
ericnormand / mapping.json
Created February 19, 2015 16:14
Elasticsearch Help
{
"big-feed-index": {
"mappings": {
"listings": {
"properties": {
"asin": {
"type": "string",
"index": "not_analyzed"
},
"categories": {