Skip to content

Instantly share code, notes, and snippets.

@EvanHahn
EvanHahn / quicksort_nodupes.coffee
Created October 11, 2012 17:37
CoffeeScript quicksort with tests
# Non-destructive quicksort.
# DOESN'T WORK WITH DUPLICATES. Too lazy.
# [1, 5, 8, 12].quickSort()
Array::quickSort = ->
# Is the array already sorted by nature of its length?
return this if @length <= 1
# Pick a pivot as a random element.
# This is probably not the best pivot we could choose.
@EvanHahn
EvanHahn / gist:3873495
Created October 11, 2012 16:10
Testing the behavior of Object.is
// Test my hypothesis that Object.is(a, b) is the same as a === b.
// Log an error if they're different.
var numTests = 0;
function test(a, b) {
if ((Object.is(a, b)) !== (a === b)) {
console.error('Object.is is different from === for ' + a + ' and ' + b);
}
numTests ++;
}
@EvanHahn
EvanHahn / mergesort.coffee
Created October 11, 2012 04:16
CoffeeScript merge sort with tests
# Non-destructive merge sort.
# [1, 8, 12, 5].mergeSort()
Array::mergeSort = ->
# If the array has one element, we're done.
return this if @length is 1
# Split the array in half.
halfwayMark = Math.floor(@length / 2)
firstHalf = @slice(0, halfwayMark)
@EvanHahn
EvanHahn / selectionsort.coffee
Created October 11, 2012 03:25
CoffeeScript selection sort with tests
# Destructive selection sort.
# [1, 12, 8, 5].selectionSort()
Array::selectionSort = ->
# Look forward from each element.
for num, i in this
# Find the smallest element in this partition.
smallest = null
smallestIndex
@EvanHahn
EvanHahn / gist:3723731
Created September 14, 2012 18:24
EECS 281 discussion 1 notes

C++ I/O

  • cin.get

  • cin.unget

  • getline(cin, var)

  • cin >> var

  • cout is slow because it has to go to the operating system

@EvanHahn
EvanHahn / gist:3486751
Created August 27, 2012 08:50
After the DOM is ready, jQuery
// The most readable way (in my opinion)
$(document).ready(function() {
// your code here
});
// Alternate way that's supposedly faster
$.fn.ready(function() {
// your code here
});
# Idiot solution to this dailyprogrammer idea
# <http://redd.it/yln9p>
puts """abandonments
abbreviating
abbreviation
abbreviators
abecedarians
aberrational
abjectnesses
@EvanHahn
EvanHahn / gist:3415282
Created August 21, 2012 13:09
Programming explained, draft 1

Let me teach you how programming works through the analogy of slavery.

Imagine that you had a slave. This slave had taken a bunch of your money and you struck a deal with him or her -- do my bidding and I will not throw you in jail.

You quickly learn that this slave isn't like the many other slaves you've had.

For one, this slave has terrible language skills. They only understands about 50 words and doesn't understand anything you say unless you say it very explicitly. If I ordered you, a non-idiot slave, to "bake me a cake", you'd do the same thing as if I said "bake a cake for me." Your slave is not this way: they only respond to "bake new cake for self."

Your slave also doesn't really know how to do anything. "Bake" means nothing to them. When your slave said "I don't understand," you rolled your eyes and said, "okay, start by getting the mix." Your slave said "I don't understand." After about an hour, you realized that your slave doesn't know how to get the mix! You had to instruct your slave to put o

Cell = require '../conway'
describe 'conway', ->
alive = Cell.isAlive
cell = null
beforeEach ->
cell = new Cell()
describe '#die', ->
@EvanHahn
EvanHahn / gist:3374882
Created August 17, 2012 00:47
Private members don't really work in CoffeeScript
// This is an example in a post about private members in CoffeeScript.
// Read more: http://evanhahn.com/?p=1126
var Animal, birch, jambo;
Animal = (function() {
var firstName;
firstName = "";