Skip to content

Instantly share code, notes, and snippets.

@knowtheory
knowtheory / prettyprint.js
Created April 11, 2011 05:44
A pretty printer for Javascript objects that looks like Ruby's pp formatter. In use on Rhino, untested elsewhere.
function pp(object, depth, embedded) {
typeof(depth) == "number" || (depth = 0)
typeof(embedded) == "boolean" || (embedded = false)
var newline = false
var spacer = function(depth) { var spaces = ""; for (var i=0;i<depth;i++) { spaces += " "}; return spaces }
var pretty = ""
if ( typeof(object) == "undefined" ) { pretty += "undefined" }
else if ( typeof(object) == "boolean" ||
typeof(object) == "number" ) { pretty += object.toString() }
else if ( typeof(object) == "string" ) { pretty += "\"" + object + "\"" }
#!/usr/bin/env ruby
require 'net/http'
require 'json'
require 'addressable/uri'
@debug_messages = false
token = '[TOKEN GOES HERE]'
user = '[USER NAME GOES HERE}'
412038: A. Lawrence Foster,A. Foster
401531: Aaron Bliss
401856: Aaron Brown
402077: Aaron Burr
402697: Aaron Cochrane
402968: Aaron Cragin
404210: Aaron Ford
404891: Aaron Hackley
405068: Aaron Harding
405086: Aaron Harlan
@knowtheory
knowtheory / datamapper_composite_index.rb
Created February 15, 2011 02:16
Demonstrating how to do composite indexes in DataMapper
require 'dm-core'
require 'dm-migrations'
DataMapper.setup(:default, "mysql://localhost/test")
class TotallyContrivedForThisExample
include DataMapper::Resource
property :id, Serial # already part of the primary key
property :plerp, String, :key => true # Adds this to the primary key
# You can get cmudict, a phonetic dictionary of english used for speech recognition purposes, here: http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict.0.7a
entries = File.open('/Users/ted/data/cmudict.0.7a.txt').read.split("\n"); ''
dict = entries.map{ |e| e.split }.reject{ |e| e.first =~ /^;;;/}; ''
results = dict.map do |entry|
char_count = entry.first.size
phonemes = entry.slice(1,entry.size)
phonemes.select{ |p| p =~ /\d/ }.size == 4 ? [entry, char_count] : nil;
end.compact.sort_by{ |e| e.last }; results.size
@knowtheory
knowtheory / google_docs_scraper.js
Created January 13, 2015 14:53
Google Drive has an in document javascript API which allows you to do powerful and crazy things like build a web scraper that runs on a timer. When in a google spreadsheet, go to the tools menu and select "script editor".
// Fetch and append the current temperature
function fetchAndAppendWeather() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
// All the actual work is done in the top row.
// We get the link stored in cell A1 (say http://www.wunderground.com/cgi-bin/findweather/getForecast?query=02217 )
var weatherLink = sheet.getRange("A1").getValue();
// we set a formula importing a targeted portion of the web page
@knowtheory
knowtheory / sql.citrus
Created May 20, 2011 16:24
A Parsing Expression Grammar for parsing an SQL subset. Written as a start to parsing SQL and generating Veritas relations (see: https://github.com/dkubb/veritas )
grammar SQL
rule statement
select_statement <Veritas::SQL::Select>
| set_operation_statement <Veritas::SQL::SetOperation>
| "(" [\s]* statement [\s]* ")"
end
rule set_operation_statement
"(" [\s]* left:select_statement <Veritas::SQL::Select> [\s]* ")"
[\s]* set_operator ([\s]+ distinct:`DISTINCT`)? [\s]*
$ lftp -d newftp.epa.gov
> user anonymous
> set ftp:ssl-allow false
> ls
> cd GKM_DOCUMENTS
> mirror -c FOIA_PROD_7.29.16
var elide = function(arr) {
// assume arr is sorted, or put it through a sort right here.
var tokens = []; // this'll be what gets output at the end.
var startOfRun = arr[0]; // pretend we have a stack. This is the first value pushed onto the stack.
var currentEndOfRun = startOfRun; // this is the top of our pretend stack.
var coalesce = function(start, end){ return "" + ((start != end) ? ""+start+"-"+end : start); }
// Consider each number in the array and it's relationship to the current run on the stack.
for (var i=1; i < arr.length; i++) {
// If the nextNumber is adjacent to the value at the top of the stack
# Make sure we've got readline on ubuntu
sudo apt-get install libreadline-dev
# copied from https://github.com/postmodern/ruby-install
wget -O ruby-install-0.3.2.tar.gz https://github.com/postmodern/ruby-install/archive/v0.3.2.tar.gz
# might be worth checking the gpg signature of the file you're installing per
# postmodern's instructions. Install his key from here: http://postmodern.github.io/contact.html#pgp
# and then uncomment the lines below.