Skip to content

Instantly share code, notes, and snippets.

@gfredericks
gfredericks / streams.lists.docs.js
Created March 30, 2013 21:13
couchdb streaming
function(head, req){
if(req.query.include_docs){
var shared = require("share/streaming");
shared.streaming(function(row){return row.doc});
} else {
start({
code: 400,
headers: {"Content-Type": "application/json"}
});
var msg = "The docs streaming route must be called with include_docs=true!";
@gfredericks
gfredericks / swrubby.rb
Created March 20, 2013 23:22
Some old code I wrote for generating obfuscated strings/integers in rubby.
# After staring at this for five minutes, I believe that what it does
# is takes ruby string/integer literals and tries to print an
# obfuscated symbols-only expression for it
require 'set'
$symbols = "!@$#^%&*.()[]{}|\\/=?+-_;:`~".split('').to_set
def regex_escape(s)
s.gsub(/([\|\{\}\$\^\-\.\(\)\\\/\[\]\+\?\*])/){|m|"\\" + m}
@gfredericks
gfredericks / doseq-bug.cljs
Created March 3, 2013 20:18
cljs scoping/closure bug
(let [fs (atom [])]
(doseq [x (range 4)
:let [y (inc x)
f (fn [] y)]]
(swap! fs conj f))
(map #(%) @fs))
;; => [4 4 4 4]
palehorse.core=> (defroutes appp (GET "/" [] {:foo [1 2 3]}))
#'palehorse.core/appp
palehorse.core=> (appp {:request-method :get :uri "/"})
{:foo [1 2 3], :status 200, :headers {}, :body ""}
(ns poly-tiler.grids
"Messing around with describing/exploring grids.
At the moment we're only looking at the graphs suggested by the
lines and vertices of a 2D tesselation, though this may trivially be
the same as looking at the faces as well.
We assume that a grid can be totally specified by the degree of its
vertices and the girth, which is the shortest cycle in the graph (also
the number of sides of the faces).
(def all-bit-length-triples
(sort-by (fn [a] (vec (cons (apply max a)
(concat
(sort a)
a))))
(for [a (range 9), b (range 9), c (range 9)]
[a b c])))
(defn bindex
"Given a number in (range 256), returns the position of the rightmost
;; This returns a response quickly (as I would expect), but if we add 4
;; to the domain it runs for a while...
(run 1 [x11 y11 x12 y12 x21 y21 x22 y22]
(infd x11 y11 x12 y12 x21 y21 x22 y22 (domain 0 1 2 3 #_4))
(<fd x11 x12) (<fd x21 x22)
(<fd y11 y12) (<fd y21 y22)
(conde
[(<fd x22 x11)]
[(<fd x12 x21)]
(defn condr*
[& goals]
(if (= 1 (count goals))
(first goals)
(let [goals (shuffle goals)]
(conde
[(first goals)]
[(apply condr* (rest goals))]))))
(defmacro condr
(ns diagrams.core
(:require [hiccup.core :as h]))
(let [prelude "<?xml version=\"1.0\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN \" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd \">\n"]
(defn svg-str
[contents]
(str prelude (h/html [:svg {:height 400 :width 400 :xmlns "http://www.w3.org/2000/svg"} contents]) "\n")))
(defn circle
[x y r & {:as attrs}]
@gfredericks
gfredericks / eval_let.clj
Created September 3, 2012 17:46
eval-let
(defmacro eval-let
[lettings code]
(let [pairs (partition 2 lettings)]
`(eval (list `let [~@(apply concat (for [[name val] pairs] [(list 'quote name) val]))] ~code))))
;; (eval-let [a 14 b 48] '(+ a b)) => 62