Skip to content

Instantly share code, notes, and snippets.

View andrewbonnington's full-sized avatar

Andrew Bonnington andrewbonnington

View GitHub Profile
console.caller = function (arguments) {
if (arguments.callee.caller == null) {
console.log('Caller: Global scope');
} else {
console.log('Caller: ' + arguments.callee.caller.name);
}
};
@andrewbonnington
andrewbonnington / fibonacci.js
Created November 22, 2016 19:55
Memoized fibonacci calculation
function fibonacci(n) {
if (n == 0) return 0;
if (n == 1) return 1;
if (fibonacci.cache[n] > 0) return fibonacci.cache[n];
return fibonacci.cache[n] = fibonacci(n - 1) + fibonacci(n - 2);
}
fibonacci.cache = [];
function getRandomElement(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
@andrewbonnington
andrewbonnington / factorial.js
Created October 8, 2016 15:50
Memoized factorial calculation
function factorial(n) {
if (n == 0 || n == 1) return 1;
if (factorial.cache[n] > 0) return factorial.cache[n];
return factorial.cache[n] = factorial(n - 1) * n;
}
factorial.cache = [];
@andrewbonnington
andrewbonnington / floor.scpt
Last active September 26, 2015 10:31
AppleScript Floor function
on floor(x)
set y to x div 1
if x < 0 and x mod 1 is not 0 then
set y to y - 1
end if
return y
end floor
@andrewbonnington
andrewbonnington / ceil.scpt
Last active October 12, 2024 22:55
AppleScript Ceil function
on ceil(x)
set y to x div 1
if x > 0 and x mod 1 is not 0 then
set y to y + 1
end if
return y
end ceil