Skip to content

Instantly share code, notes, and snippets.

View cespare's full-sized avatar

Caleb Spare cespare

View GitHub Profile
@cespare
cespare / lambda.py
Created April 4, 2011 17:49
Python lambda gotcha
def callback(message):
print(message)
foo = ["a", "b", "c"]
# Environment variables are only captured as references by the lambda:
bar = [lambda: callback(s) for s in foo]
for f in bar:
f() # => c x 3
@cespare
cespare / gist:920767
Created April 14, 2011 22:52
fixed.js
var Foo;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Foo = (function() {
function Foo() {}
Foo.prototype.init = function() {
var lalala = this;
$("#submitButton").click(__bind(function(event) {
// throws an error "HTMLDocument element does not have property onSubmitButtonClicked
return this.onSubmitButtonClicked(event);
}, lalala));
@cespare
cespare / gist:1368825
Created November 16, 2011 00:03
scala default map updating
import scala.collection.mutable.Map
import scala.collection.mutable.Buffer
val foo = Map[Int, Buffer[Int]]() withDefault ((key) => Buffer())
foo(1) = Buffer(1, 2)
foo(1) += 3
foo(1) // => ArrayBuffer(1, 2, 3)
/* This all seems as expected */
@cespare
cespare / gist:2402610
Created April 17, 2012 00:45
Coffeescript function with multiple callbacks
foo = (f1, f2) ->
f1()
f2()
# Doesn't work -- syntax error
# foo(-> console.log("hi from f1"), -> console.log("hi from f2"))
# Simplest way I've found to do it -- newlines added for clarity
foo(
(-> console.log("hi from f1")),
@cespare
cespare / gist:2403743
Created April 17, 2012 05:42
array of partials
# In your app
get "/whatever"
erb :_partial_collection, :layout => false, :locals => { partials => partials } # partials is an array of other partials...
end
# in views/_partial_collection.erb
<% partials.each { |partial| %><%= erb partial, :layout => false %><% } %>
@cespare
cespare / config.json
Created June 16, 2012 01:08
Reverse routing proxy in Go
[
["/foo", "localhost:8090"],
["/bar", "google.com:80"],
["/", "localhost:3000"]
]
@cespare
cespare / gist:3167123
Created July 24, 2012 00:19
Imagemagick (rmagick) benchmark
# Need to install imagemagick first.
# sudo apt-get install libmagickwand on ubuntu.
# Available on homebrew as well.
require "RMagick"
images = Dir["./*.png"]
start_time = Time.now
@cespare
cespare / writeup.md
Created September 27, 2012 11:39
A Simple Webserver Comparison

This is a very simple benchmark comparing the response times of a few different webservers for an extremely simple response: just reply with a snippet of static json. It came up in discussion of a real-life service: in the actual server, a long-running thread/process periodically updates the state from a database, but requests will be served with the data directly from memory. It is imperative, though, that the latencies be extremely low for this service.

This comparison was partly inspired by this blog post.

Method

@cespare
cespare / pygments_example.go
Created October 1, 2012 23:23
A first hello-world attempt at pygments.go
package main
// #cgo CFLAGS: -I/usr/include/python2.7
// #cgo LDFLAGS: -lpython2.7
// #include <Python.h>
// int pyRunString(const char *s) { return PyRun_SimpleString(s); }
import "C"
import "unsafe"
@cespare
cespare / log.go
Created October 31, 2012 06:51
Golang apache logging
type ApacheLogRecord struct {
http.ResponseWriter
ip string
time time.Time
method, uri, protocol string
status int
responseBytes int64
elapsedTime time.Duration
}