Skip to content

Instantly share code, notes, and snippets.

View dsamarin's full-sized avatar

Devin Samarin dsamarin

View GitHub Profile
@dsamarin
dsamarin / graycode.js
Last active August 29, 2015 14:27 — forked from silentmatt/graycode.js
JavaScript functions to convert to/from binary-reflected Gray codes
Number.toGrayCode = function(n) {
if (n < 0) {
throw new RangeError("cannot convert negative numbers to gray code");
}
return n ^ (n >>> 1);
};
Number.fromGrayCode = function(gn) {
if (gn < 0) {
throw new RangeError("gray code numbers cannot be negative");
@dsamarin
dsamarin / utils.js
Created June 29, 2011 05:23
Quote JavaScript strings
function(value) {
// First we need to find which quote character to use by comparing the
// number of times each occurs in the string.
var quotes_dbl = (value.match(/"/g) || []).length;
var quotes_sgl = (value.match(/'/g) || []).length;
var quote = quotes_sgl <= quotes_dbl ? "'" : '"';
var quote_code = quote.charCodeAt(0);
@dsamarin
dsamarin / TextLayout.js
Created August 16, 2011 11:33
Text Rendering/Wrapping for HTML5 Canvas
suit.TextLayout = function SUITTextLayout() {
suit.Object.call(this);
/* This stores key/value pairs where the key is the width of a rendered
layout and the value is the number of lines the layout will take. */
this.wrapped_length_cache = [];
this.em_width = this.text_width("M");
};
@dsamarin
dsamarin / lolwat.js
Created August 18, 2011 10:58 — forked from mathiasbynens/lolwat.js
Fun with v8’s Number#toString bug
var fs = require('fs');
var repl = require('repl');
console.log("Loading dictionary...");
var words = fs.readFileSync("/usr/share/dict/american-english-small", "ascii");
var wordlist = {};
var regex = /[a-z]{4,}/g;
var match;
@dsamarin
dsamarin / Integer.js
Created October 3, 2011 03:07
Arbitrary-precision numbers in JavaScript
"use strict";
// http://kickjava.com/src/java/math/MutableBigInteger.java.htm
String.prototype.repeat = function(i) {
var d = '', t = this;
while (i) {
if (i & 1) {
d += t;
}
@dsamarin
dsamarin / gist:1260689
Created October 4, 2011 01:10
C Translation Phases
1. Map characters to source character set (not relevant)
2. Trigraph sequences replaced (no one cares, low priority)
3. Backslash followed by newline deleted
4. Decomposed into tokens
5. Preprocessing directives executed
6. Macros expanded
7. _Pragma execution
@dsamarin
dsamarin / js_direct.js
Created October 16, 2011 03:16
CPS Transforms in JavaScript and Scheme
// Example 1
function pyth(x, y) {
return Math.sqrt (x*x + y*y);
}
// Example 2
function factorial(n) {
if (n === 0) {
return 1;
}
@dsamarin
dsamarin / gist:1518849
Created December 25, 2011 07:28
Flood control queue
/* Connection#send: Sends a message with flood control */
Connection.prototype.send = function(message) {
var queue, now;
now = Date.now();
queue = this.message_queue;
/* If the last message was sent early enough... */
if (this.message_time < (now - this.message_speed)) {
Hub.init = function() {
this.load_config (function() {
this.start_server ();
});
};
Hub.load_config = function(callback) {
var config, locations, self = this;
config = new HubConfig;
@dsamarin
dsamarin / config.json
Created January 7, 2012 07:15
Example Diaptoval configuration file
{
"Core": {
"socket": "/tmp/diaptoval.sock"
},
"IRC": {
"default": {
"nick": ["eboy", "eboyjr", "eboyjr_"],
"quit_message": "Try out lic, the IRC client, for free at http://github.com/oftn/lic/"
},
"servers": [