Skip to content

Instantly share code, notes, and snippets.

@EvanHahn
EvanHahn / intermediate132.rb
Last active December 21, 2015 09:39
Solution to Reddit's r/dailyprogrammer challenge #132. http://redd.it/1kqxz9
INSTRUCTIONS = {
'and' => {
0x00 => [:address, :address],
0x01 => [:address, :literal]
},
'or' => {
0x02 => [:address, :address],
0x03 => [:address, :literal]
},
'xor' => {
@EvanHahn
EvanHahn / miniclass.js
Created August 20, 2013 07:38
MiniClass, minified. 338 bytes uncompressed.
(function(){var b=function(){};b.extend=function(c){c||(c={});var d=this,b=c.initialize,e=function(){b?b.apply(this,arguments):d.apply(this,arguments)},a;for(a in d)"prototype"!=a&&(e[a]=d[a]);for(a in d.prototype)e.prototype[a]=d.prototype[a];for(a in c)e.prototype[a]=c[a];return e};module.exports?module.exports=b:this.MiniClass=b})();
@EvanHahn
EvanHahn / gist:6106033
Created July 29, 2013 17:34
Add this to Pocket to make the sans-serif font family consistent with the mobile apps.
#page_reader[font="sans"] .reader_content {
font-family: "ProximaNova", Helvetica, Arial, sans-serif;
}
@EvanHahn
EvanHahn / server.js
Last active December 17, 2015 10:39
Express server that looks for files in /common, then figures out the useragent and goes somewhere else.
// Load dependencies
var express = require('express');
var is = require('browseris');
var fs = require('fs');
// Build me an app!
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(express.compress());
@EvanHahn
EvanHahn / gist:4945775
Last active December 13, 2015 17:09
A friend asked for help on some basic Java homework, and so here it is.

Let's first talk about booleans, which you might already know, so I apologize if this is review. If you think you understand booleans completely, skip this puppy.

Booleans

A boolean is a variable type in many programming languages. It usually that something can be true or false, yes or no, on or off, 0 or 1.

I imagine booleans like lightswitches. They can either be on or off -- true or false.

Most languages have true and false, but they vary somewhat. C++ writes true and false, where Python has capitalized True and False. CoffeeScript supplements the classics and adds yes, no, on, and off. C kinda doesn't have them built-in, you have to make your own.

@EvanHahn
EvanHahn / gist:4498026
Last active December 10, 2015 21:48
Example page that does its best to disable copy-paste. For more: <http://evanhahn.com/?p=379>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
-webkit-user-select: none;
-khtml-user-drag: none;
-khtml-user-select: none;
-moz-user-select: none;
@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 / 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
});