Skip to content

Instantly share code, notes, and snippets.

@EvanHahn
EvanHahn / gist:3330517
Created August 12, 2012 07:42
JavaScript sleepsort
// JavaScript sleepsort
// by Evan Hahn (evanhahn.com)
// Port of this genius on 4chan:
// http://dis.4chan.org/read/prog/1295544154
// Call it like this:
// sleepsort(5, 4, 12, 8, 9, 10, 240);
function sleepsort() {
@EvanHahn
EvanHahn / gist:3363965
Created August 15, 2012 21:41
Simple private member functions in CoffeeScript
# There's more complexity to this, as described below:
# http://evanhahn.com/?p=1126
class Person
hello = ->
"hello"
helloWorld: ->
"#{hello()} world!"
me = new Person
@EvanHahn
EvanHahn / gist:3364133
Created August 15, 2012 22:08
Getting private member functions working in CoffeeScript, with .call()
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
constructor: (@spell) ->
conjureSpell = -> # private
@spell.conjure()
useSpell: ->
conjureSpell.call(this) # Note this little hack
@spell.use()
@EvanHahn
EvanHahn / gist:3364153
Created August 15, 2012 22:11
Getting private member functions working in CoffeeScript, by scoping differently
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
that = null
constructor: (@spell) ->
that = this
conjureSpell = -> # private
that.spell.conjure() # Note this hack
useSpell: ->
@EvanHahn
EvanHahn / gist:3364224
Created August 15, 2012 22:17
Getting private member functions working in CoffeeScript by scoping differently breaks inheritance
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
that = null
constructor: (@spell) ->
that = this
conjureSpell = -> # private
that.spell.conjure()
useSpell: ->
@EvanHahn
EvanHahn / gist:3364303
Created August 15, 2012 22:30
Getting private member functions working in CoffeeScript by defining them in the prototype breaks inheritance
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
conjureSpell = null
constructor: (@spell) ->
conjureSpell = => # private
@spell.conjure()
useSpell: ->
conjureSpell()
@EvanHahn
EvanHahn / gist:3364322
Created August 15, 2012 22:35
Inheriting private member functions working in CoffeeScript, with .call()
# This is an example in a post about private members in CoffeeScript.
# Read more: http://evanhahn.com/?p=1126
class Sorcerer
constructor: (@spell) ->
conjureSpell = -> # private
@spell.conjure()
useSpell: ->
conjureSpell.call(this)
@spell.use()
@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 = "";
Cell = require '../conway'
describe 'conway', ->
alive = Cell.isAlive
cell = null
beforeEach ->
cell = new Cell()
describe '#die', ->
@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