Skip to content

Instantly share code, notes, and snippets.

View jchris's full-sized avatar

Chris Anderson jchris

View GitHub Profile
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)
@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 '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 / 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;
@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 / 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 / index.html
Created April 27, 2011 16:54
jQuery CouchDB Hello World - to use this script, create the map view in futon and click Save As, then upload index.html as an attachment to _design document created with the view. browse to index.html to run the app.
<!DOCTYPE html>
<html>
<head><title>Tiny CouchApp</title></head>
<body>
<h1>Tiny CouchApp</h1>
<form id="new_message">
<label for="message">Message:</label>
<input type="text" name="message" value="">
<p><input type="submit" value="Save &rarr;"></p>
</form>
@jchris
jchris / jquery.couchLogin.js
Created May 9, 2011 22:48
jQuery plugin for CouchDB login/signup/logout in 80 lines (2kb minified)
// Copyright Chris Anderson 2011
// Apache 2.0 License
// jquery.couchLogin.js requires jquery.couch.js
//
// Example Usage (loggedIn and loggedOut callbacks are optional):
// $("#mylogindiv").couchLogin({
// loggedIn : function(userCtx) {
// alert("hello "+userCtx.name);
// },
// loggedOut : function() {
@jchris
jchris / not_in_svn.rb
Created July 22, 2011 01:54
you want to know the commits that never made it into svn
# usage:
# git log | ruby not_in_svn.rb | less
# license: Apache 2.0
# copyright: J Chris Anderson 2011
# this might be too trivial to copyright
def print_if_no_match buffer, regex
text = buffer.join("\n")
unless text.match(regex)
puts text
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 8 20:32:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".Attaching to process 44141.
2011-09-24 15:04:57.694 daz[44141:f803] Registering custom .ini file /Users/jchrisa/Library/Application Support/iPhone Simulator/5.0/Applications/696D756A-2862-4778-A48A-E63A9E7FDAF8/daz.app/app.ini
2011-09-24 15:04:57.697 daz[44141:f803] Couchbase: Starting CouchDB, using runtime files at: /Users/jchrisa/Library/Application Support/iPhone Simulator/5.0/Applications/696D756A-2862-4778-A48A-E63A9E7FDAF8/daz.app/CouchbaseResources (built Sep 21 2011, 14:55:44)
Eshell V5.8 (abort with ^G)