Skip to content

Instantly share code, notes, and snippets.

View jchris's full-sized avatar

Chris Anderson jchris

View GitHub Profile
@jchris
jchris / learnifthen.js
Created December 20, 2010 21:17
teaching amysue javascript
rainPain = function(temp, cip) {
if (temp < 39) {
console.log ("too cold!")
} else if (temp<80 && cip){
console.log("ouch")
} else {
console.log("time to walk the dogs")
}
}
rainPain(75, true)
@jchris
jchris / couchapp.html
Created November 27, 2010 15:14
the smallest possible CouchApp
<!DOCTYPE html>
<html>
<!-- This should be the simplest possible jQuery based CouchApp. Install by uploading to a design document in Futon or putting in _attachments/index.html and running `couchapp push` -->
<head><title>Tiny CouchApp</title></head>
<body>
<h1>Tiny CouchApp</h1>
<ul id="databases"></ul>
</body>
<script src="/_utils/script/jquery.js"></script>
<script src="/_utils/script/jquery.couch.js"></script>
@jchris
jchris / reduce.js
Created November 26, 2010 04:41
unique key count reduce
function(ks, vs, rr) {
// values are discarded by reduce
// todo: configurable key function, for complex queries
var fk, lk;
function uniqCount(keys) {
var count = 0, obj = {};
for (var i=0; i < keys.length; i++) {
if (!obj[keys[i]]) {
count++;
obj[keys[i]] = true;
require 'rubygems'
require 'garb'
# couchrest is only required because it makes the dates to_json into
# something javascript can parse.
require 'couchrest'
class OpenStruct
def to_json
table.to_json
end
@jchris
jchris / topN.js
Created October 31, 2010 15:03
a CouchDB list function for returning the top Terms in where the key is like [Term|_Rest]
function(seq, req) {
var min = 0, term, count, row
, i, top = parseInt(req.query.top), match = [];
while (row = getRow()) {
term = row.key[0];
count = row.value;
if (match.length < top) {
match.push([term, count]);
} else {
if (count > min) {
require 'digest/md5'
# usage: run this in the root directory of your iTunes Music folder, or wherever, and pipe the output to a file
# next, pipe the output of that file through `sort` to a new file
# now, use the next script on that file
ls = Dir['**/*']
ls.each_with_index do |f, i|
STDERR.puts ls.length - i if (i % 100 == 0)
// Copyright J. Chris Anderson 2007
// Retain this notice.
// Released under the LGPL 2.1
// http://creativecommons.org/licenses/LGPL/2.1/
XSPF = {
XMLfromString: function(string) {
if (window.ActiveXObject) {
var doc = new ActiveXObject("Microsoft.XMLDOM");
doc.async = "false";
require 'lib/yajl/http_stream'
require 'uri'
uri = URI.parse('http://jchrisa.net/toast/_changes')
Yajl::HttpStream.get(uri) do |hash|
# will take a few seconds, since the response is a single ~4MB JSON string
# if the response was a bunch of individual JSON strings, this would be fired
# for every string parsed off the stream, as they were parsed
puts hash.inspect
end
@jchris
jchris / benchbulk.sh
Created March 15, 2009 02:48
benchmarking CouchDB bulk inserts in bash
#!/bin/bash
# usage: time benchbulk.sh dbname
# it takes about 30 seconds to run on my old MacBook
BULKSIZE=1000
DOCSIZE=100
INSERTS=10
ROUNDS=10
DBURL="http://localhost:5984/$1"
require 'rubygems'
require 'couchrest'
# this is the CouchDB where all the old databases are
OLD_HOST = "http://127.0.0.1:5984"
# this is the CouchDB we want to copy to
NEW_HOST = "http://127.0.0.1:5985"
old_couch = CouchRest.new(OLD_HOST)