Skip to content

Instantly share code, notes, and snippets.

View AutoSponge's full-sized avatar

Paul Grenier AutoSponge

View GitHub Profile
@buzzdecafe
buzzdecafe / generator.js
Last active December 16, 2015 13:39
infinite stream fibonacci generator
/*
Fibonacci infinite stream generator. Not hugely exciting yet
*/
var fgen = (function() {
var fn1 = 0, fn2 = 1;
f = function f() {
var curr = fn2;
fn2 = fn1;
fn1 = fn1 + curr;
return fn1;
@buzzdecafe
buzzdecafe / cons, car, cdr
Last active November 17, 2021 10:11
cons car and cdr implemented as javascript functions. cribbed from SICP http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html#%_thm_2.4then implemented a bunch of functions just for fun.
// clean and pure:
function cons(x, y) {
return function(pick) {
return pick(x, y);
}
}
// does more stuff:
function cons(x, y) {
var fn = function(pick) {
@CrossEye
CrossEye / omega.js
Last active May 14, 2018 08:01
Functional compostion using fake operator overloading
// Based on a discussion with Michael Haufe:
// https://groups.google.com/group/jsmentors/browse_thread/thread/d028fb0041f93a27
// Not really recommended for anything but the fun of knowing it can be done!
var omega = function() {
var queue = [];
var valueOf = Function.prototype.valueOf;
Function.prototype.valueOf = function() {
queue.push(this);
return 1; // not needed now, but could be used later to distinguish operators.
@cowboy
cowboy / isprimitive-no-strict.js
Created September 18, 2012 12:23
JavaScript: isPrimitive
var isPrimitive = function(val) {
return val !== function() { return this; }.call(val);
};
@dherman
dherman / functions.js
Created July 29, 2012 01:44
Function utilities
(function(Fp, Ap) {
var applyMethod = Fp.apply,
bindMethod = Fp.bind,
callMethod = Fp.call;
var sliceMethod = Ap.slice,
concatMethod = Ap.concat;
var apply = callMethod.bind(applyMethod),
bind = callMethod.bind(bindMethod),
@dherman
dherman / closure.js
Created July 27, 2012 21:39
Analog to Function constructor for building closures
(function() {
var hasOwnProperty = Object.prototype.hasOwnProperty;
var Function = hasOwnProperty.constructor;
function isIdentifier(s) {
return /[a-zA-Z_$][a-zA-Z_$0-9]*/.test(s);
}
@pamelafox
pamelafox / senderror.js
Created February 21, 2012 19:20
Sending JS errors to server
function sendError(message, url, lineNum) {
var i;
// First check the URL and line number of the error
url = url || window.location.href;
lineNum = lineNum || 'None';
// If the error is from these 3rd party script URLs, we ignore
// We could also just ignore errors from all scripts that aren't our own
var scriptURLs = [
@dherman
dherman / 1-recursive.js
Created February 7, 2012 20:10
turning recursion into iteration in JS
// Version 1. Simple recursive function. Blows the stack for large nodes.
function replace(node, from, to) {
switch (node.type) {
case IF:
return {
type: IF,
test: replace(node.test, from, to),
then: replace(node.then, from, to),
else: replace(node.else, from, to)
};
@Gozala
Gozala / example.js
Created January 29, 2012 03:46
Workaround for lack of "tail call optimization" in JS
// Lack of tail call optimization in JS
var sum = function(x, y) {
return y > 0 ? sum(x + 1, y - 1) :
y < 0 ? sum(x - 1, y + 1) :
x
}
sum(20, 100000) // => RangeError: Maximum call stack size exceeded
// Using workaround
@rauschma
rauschma / module_boilerplate.js
Created January 23, 2012 15:36
Triple module boilerplate: Node.js, AMD, plain browser
// No imports, those are more work, especially for plain browser.
// However, as soon as you have imports, you should switch to AMD on browsers.
// Related: http://www.2ality.com/2011/11/module-gap.html
({ define:
typeof define === "function" ?
define
: typeof module !== "undefined" ?
function(F) { module.exports = F() }
: function(F) { this.defClass = F() }.bind(this)