Skip to content

Instantly share code, notes, and snippets.

@qerub
qerub / char-sequence.clj
Created February 18, 2013 14:57
[Clojure] CharSequence adapter for seqables of chars
(defn char-sequence [char-seqable]
(let [char-seq (seq char-seqable)]
(reify CharSequence
(charAt [this i] (nth char-seq i))
(length [this] (count char-seq))
(toString [this] (String. (char-array char-seq)))
; (subSequence [this start end] ...)
)))
@qerub
qerub / gist:4137421
Created November 23, 2012 21:38
Simple Futures in Ruby with Lambdas and Threads
def future # :block:
thread = Thread.new do
Thread.current.abort_on_exception = false
yield
end
lambda { thread.value }
end
def compose(proc) # :block:
@qerub
qerub / sequel_reconnector.rb
Last active October 11, 2015 03:07
Rack middleware that makes sure requests don't get a dead Sequel connection
require "sequel/extensions/connection_validator"
# https://github.com/jeremyevans/sequel/blob/master/lib/sequel/extensions/connection_validator.rb
module Rack
class SequelConnectionValidator
def initialize(app, db)
@app = app
@db = db
@db.extension(:connection_validator)
@qerub
qerub / gist:3786700
Created September 26, 2012 08:01
Run `mvn clean compile` on `git checkout`
$ cat .git/hooks/post-checkout
#!/bin/sh
mvn clean compile
@qerub
qerub / gist:3219854
Last active April 21, 2021 09:46
Representing [Clojure] code in JSON
; Context:
; http://stackoverflow.com/questions/3436216/how-to-map-clojure-code-to-and-from-json
(defn escape-string [x]
(clojure.string/replace x #"^[':\\]" "\\\\$0"))
(defn code-to-json [x]
(condp #(%1 %2) x
number? x
symbol? (str \' (name x))
@qerub
qerub / small-complaints-about-pocket.md
Last active October 7, 2015 11:58
Small Complaints about Pocket
@qerub
qerub / goto.md
Created July 7, 2012 22:08
[Clojure] Ring middleware for JSONP
@qerub
qerub / gist:2918167
Created June 12, 2012 15:23
The Difference between -> and ->> in Clojure
user=> (use 'clojure.walk)
nil
user=> (macroexpand-all '(-> x (1 a) (2 b)))
(2 (1 x a) b)
user=> (macroexpand-all '(->> x (1 a) (2 b)))
(2 b (1 a x))
@qerub
qerub / x_forwarded_for.clj
Created June 12, 2012 15:06
[Clojure] Ring middleware for `X-Forwarded-For` [:remote-addr rewrite]
(ns ring.middleware.x-forwarded-for
(:use [clojure.string :only (split)]))
(defn wrap-x-forwarded-for [handler]
(fn [request]
(if-let [xff (get-in request [:headers "x-forwarded-for"])]
(handler (assoc request :remote-addr (last (split xff #"\s*,\s*"))))
(handler request))))
@qerub
qerub / gist:969308
Created May 12, 2011 19:53
Objective-R: Objective-C-like syntax for method calls in Racket
#lang racket ; Requires Racket >= 5.1.1
(provide (rename-out (objective-r-read read)
(objective-r-read-syntax read-syntax)))
(require syntax/stx)
(define (rewrite-method-calls stx)
(if (stx-list? stx)
(let ((stx* (stx-map rewrite-method-calls stx)))