Skip to content

Instantly share code, notes, and snippets.

View swannodette's full-sized avatar

David Nolen swannodette

View GitHub Profile
(defn everyo [pred coll]
(if (empty? coll)
s#
(all
(pred (first coll))
(everyo pred (rest coll)))))
(def timing
(for [seq-len [100 10000 1000000]
:let [v (vec (range seq-len))]
[name [f combine]] {:map [map reduce]
:r/map [r/map r/reduce]
:fold [r/map r/fold]}
num-layers [0 2 5 10 25]]
{:f name,
:len seq-len,
:layers num-layers,
(def users [{:name "Brian" :age 22} {:name "Ben" :age 19}])
;; Takes a "path", returns a function that takes an "object" and
;; returns a "costate"
(defn lens [p]
(fn [o]
{:get (get-in o p)
:set #(assoc-in o p %1)}))
(def myAgeLens (lens [0 :age]))
// based off of http://labs.unwieldy.net/moocirclepack/
org.polymaps.packer = function() {
var packer = {},
nodes = [],
elements = [],
timesToPack = 50;
packer.elements = function(e) {
if (!arguments.length) {
return elements;
(let [{user "MONGO_USER" pass "MONGO_PASS"} (System/getenv)]
(authenticate conn user pass)
(set-connection! conn))
@swannodette
swannodette / example-user.clj
Created May 4, 2012 11:39 — forked from nijikokun/example-user.js
Beautiful Validation... Why have I never thought of this before?!
(defn validates-credentials [username password]
(let [uc (count username)
pc (count password)]
(match [username uc password pc]
[(:or nil "") _ _ _] {:error "No username given" :field "name"}
[_ _ (:or nil "") _] {:error "No password given" :field "pass"}
[_ (_ :guard #(< % 3)) _ _] {:error "Username less than 3 characters" :field "pass"}
[_ _ _ (_ :guard #(< % 4))] {:error "Password less than 4 characters" :field "pass"}
[#"^([a-z0-9-_]+)$" _ _ _] {:error "Username contains invalid characters" :field "name"}
:else true)))
@swannodette
swannodette / gist:2400319
Created April 16, 2012 17:53 — forked from jamii/gist:2400297
Very crude (monotonic) datalog interpreter
(ns mist.logic.datalog
(:use clojure.core.logic
[clojure.set :only [union, difference]])
(:require clojure.walk))
(defne allo [goal args]
([_ ()])
([_ [arg . rest]]
(goal arg)
(allo goal rest)))
@swannodette
swannodette / anything-org-mode.el
Created March 13, 2012 14:09
anything-org-mode.el
(defun anything-org-todo-list (arg)
(interactive "P")
(org-compile-prefix-format 'todo)
(org-set-sorting-strategy 'todo)
;;(org-prepare-agenda "TODO")
(if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
(let* ((today (time-to-days (current-time)))
(date (calendar-gregorian-from-absolute today))
(kwds org-todo-keywords-for-agenda)
(completion-ignore-case t)
@swannodette
swannodette / curly.clj
Created March 1, 2012 19:41 — forked from jaley/curly.clj
Remove text surrounded by curly braces.
(defn remove-curlied
[^String text]
(let [sb (StringBuilder.)
end (int (count text))]
(loop [state (int 0)
pos (int 0)]
(cond
(= pos end) (.toString sb)
(= (int (.charAt text pos)) (int \{)) (recur (inc state) (inc pos))
(= (int (.charAt text pos)) (int \})) (recur (dec state) (inc pos))
@swannodette
swannodette / gist:1893724
Created February 23, 2012 16:52 — forked from fogus/gist:1893062
pretty-lambdas for elisp
(defun pretty-lambdas ()
"Show glyph for lower-case Greek lambda (λ) wherever 'lambda' appears."
(font-lock-add-keywords
nil
`(("(\\(lambda\\>\\)"
(0
(progn
(compose-region
(match-beginning 1)
(match-end 1)