Skip to content

Instantly share code, notes, and snippets.

View wyattdanger's full-sized avatar

Stephen Bush wyattdanger

View GitHub Profile
{setup:"Our wedding was so beautiful,",punchline:"even the cake was in tiers"},{setup:"I'm reading a book on the history of glue",punchline:"I just can't seem to put it down"},{setup:"What do you call an Argentinian with a rubber toe?",punchline:"Roberto"},{setup:"I am terrified of elevators,",punchline:"I'm going to start taking steps to avoid them"},{setup:"Why do crabs never give to charity?",punchline:"Because they're shellfish."},{setup:"Why don't skeletons ever go trick or treating?",punchline:"Because they have no body to go with"},{setup:"What do you call cheese by itself?",punchline:"Provolone"},{setup:'"Ill call you later."',punchline:"Don't call me later, call me Dad"},{setup:"Dad, I'm hungry.",punchline:"Hello, Hungry. I'm Dad"},{setup:"Where does Fonzie like to go for lunch?",punchline:"Chick-Fil-Eyyyyyyyy"},{setup:"Did you hear about the cheese factory that exploded in France?",punchline:"There was nothing left but de Brie"},{setup:"I knew I shouldn’t have had the seafood",punchline:"I’m feeling
@wyattdanger
wyattdanger / linked-list.js
Created September 28, 2012 17:18
simple implementation, does not handle errors
/*
* LinkedList
* Very simple LinkedList implementation
*/
function Node(item, next) {
this.item = item;
this.next = next;
}
@wyattdanger
wyattdanger / plan.js
Created September 20, 2012 23:21
js demo
var Plan = (function() {
// protected
var records = [];
// the Plan constructor
function Plan(id, price) {
this.id = id;
this.price = price;
records.push(this);
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
begin
rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME']))
rvm_lib_path = File.join(rvm_path, 'lib')
#$LOAD_PATH.unshift rvm_lib_path
require 'rvm'
RVM.use_from_path! File.dirname(File.dirname(__FILE__))
rescue LoadError
raise "RVM ruby lib is currently unavailable."
end
@wyattdanger
wyattdanger / timeouts.js
Created May 25, 2012 19:39
events can be scheduled while code is running, but can't be executed until the runtime is free
var start = new Date();
setTimeout(function () { var end = new Date(); console.log("Timeout 3ms elapsed:", end - start, "ms"); }, 3);
setTimeout(function () { var end = new Date(); console.log("Timeout 2ms elapsed:", end - start, "ms"); }, 2);
setTimeout(function () { var end = new Date(); console.log("Timeout 1ms elapsed:", end - start, "ms"); }, 1);
while( new Date() - start < 2000 ) {}
setTimeout(function () { var end = new Date(); console.log("Timeout 0ms elapsed:", end - start, "ms"); }, 0);
squareRoot = ( num ) ->
square = ( x ) -> x * x
avg = ( x, y ) -> (x + y) / 2
goodEnough = ( x ) -> Math.abs( square( x ) - num ) < 0.01
improveGuess = ( x ) -> avg( x, num/x )
iter = ( guess ) ->
return guess if goodEnough guess
iter improveGuess guess
iter 1.0
function longestCommonPrefix ( arr ) {
var result = "";
var firstWord = arr[0];
for ( var i = 0; i < firstWord.length; i++ ) {
for ( var j = 0; j < arr.length; j++) {
if ( firstWord[i] === arr[j][i]) {
continue;
} else {
return result;
@wyattdanger
wyattdanger / further-example.diff
Created May 1, 2012 20:28
to give each instance of Bar it's own `a` property, rather than a shared `a` on the prototype
function Foo () {
this.a = [];
}
+function Bar () {
+ Foo.call(this);
+}
-function Bar () {}
Bar.prototype = new Foo();
@wyattdanger
wyattdanger / inheritance-demo.js
Created May 1, 2012 19:37
Contrasting prototypal inheritance in JavaScript with classical inheritance in Ruby. In this gist you'll find a JavaScript file and a Ruby file which try to do the same thing, the outputs of each file, and a diff of the two outputs.
var inspect = require('util').inspect, // To help with printing to console
log = console.log;
// Define class Foo
function Foo () {
this.a = [];
}
// Define class Bar which inherits from Foo
function Bar () {}
@wyattdanger
wyattdanger / simple-parser
Created April 23, 2012 00:50
simple parser in peg.js
start =
expression
expression =
_ a:atom
{ return a }
/ _ "(" a:expression b:expression* ")"
{ return [a].concat(b) }