Skip to content

Instantly share code, notes, and snippets.

View dbrockman's full-sized avatar

David Brockman dbrockman

View GitHub Profile
@dbrockman
dbrockman / gist:1851609
Created February 17, 2012 07:41
Git aware bash PS1
c_cyan=`tput setaf 6`
c_red=`tput setaf 1`
c_green=`tput setaf 2`
c_sgr0=`tput sgr0`
parse_git_status() {
local color=""
if git rev-parse --git-dir >/dev/null 2>&1
then
@dbrockman
dbrockman / wrapLineAtDivisibleColumn.js
Last active December 24, 2019 22:05
Wrap line at the highest column (<= 80) divisible by the length of the line
function wrapLineAtDivisibleColumn(line) {
var column = 80;
while (line.length / column % 1) --column;
return line.replace(new RegExp('.{' + column + '}(?!$)', 'g'), '$&\n');
}
// high-resolution time diff in milliseconds
function hrmstime(t) {
if (t) {
t = process.hrtime(t);
return (t[0] * 1e9 + t[1]) / 1e6;
}
return process.hrtime();
}
// Takes a number and clamps it to within the provided bounds.
#define Clamp(_num, _min, _max) MIN((_max), MAX((_min), (_num)))
// Check if the two floats are equal.
#define FloatEqual(A, B) (ABS((A) - (B)) < FLT_EPSILON)
// Check if the two floats are NOT equal.
#define FloatNotEqual(A, B) (ABS((A) - (B)) > FLT_EPSILON)
// Check if the float is equal to zero.
// Fisher-Yates shuffle
function shuffle(array) {
for (var n = array.length, k, x; 1 < n;) {
k = Math.floor(Math.random() * n--);
x = array[n];
array[n] = array[k];
array[k] = x;
}
}
@dbrockman
dbrockman / degrees-radians.js
Created February 12, 2013 21:43
Convert degrees <-> radians. Taken from Google Closure lib.
/**
* Converts degrees to radians.
* @param {number} angleDegrees Angle in degrees.
* @return {number} Angle in radians.
**/
function degreesToRadians(angleDegrees) {
return angleDegrees * Math.PI / 180;
}
/**
@dbrockman
dbrockman / degrees-radians.h
Created February 12, 2013 21:52
Convert degrees <-> radians C macros
// Converts degrees to radians.
#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
// Converts radians to degrees.
#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)
@dbrockman
dbrockman / insertAt-removeAt.js
Created March 7, 2013 18:29
JS array insertAt and removeAt function
function insertAt(arr, val, i) {
arr.splice(i, 0, val);
}
function removeAt(arr, i) {
return arr.splice(i, 1).length === 1;
}
function genericInsertAt(arr, val, i) {
Array.prototype.splice.call(arr, i, 0, val);
}
function fibonacci(n) {
return n < 2 ? n : fibonacci(n - 2) + fibonacci(n - 1);
}
function fibonacci(n) {
var prev, curr = 0, next = 1;
while (n-- > 0) {
prev = curr;
curr = next;
next = prev + curr;
@interface NSIndexSet (Operations)
// http://en.wikipedia.org/wiki/Union_(set_theory)
- (NSIndexSet *)unionWith:(NSIndexSet *)other;
// http://en.wikipedia.org/wiki/Intersection_(set_theory)
- (NSIndexSet *)intersectionWith:(NSIndexSet *)other;
// http://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement
- (NSIndexSet *)relativeComplementIn:(NSIndexSet *)universe;