Skip to content

Instantly share code, notes, and snippets.

View buzzdecafe's full-sized avatar
💭
quaquaquaqua

buzzdecafe

💭
quaquaquaqua
View GitHub Profile
@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) {
@buzzdecafe
buzzdecafe / aaa.js
Last active December 15, 2015 17:29 — forked from AutoSponge/tco.md
function recur(fn) {
return function () {
var bounce = fn.apply(this, arguments);
while (bounce.onTheTrampoline) {
bounce = bounce();
}
return bounce;
};
}
var sum1 = recur(function sum(x, y) {
@buzzdecafe
buzzdecafe / dedupe with reduce
Last active December 16, 2015 05:59
dedupe an array of objects
// Paul's clean-up
function deDupe(dupeCheck, list) {
return list.reduce(function (prev, curr) {
if (!dupeCheck(curr, prev)) {
prev.push(curr);
}
return prev;
}, []);
}
/*
Factorial of course. Can't talk about recursion without factorial.
*/
function basicFactorial(n) {
return n === 0 ? 1 : n * basicFactorial(n-1);
}
/*
Define a *non-recursive* function that has the factorial function as a fixpoint.
*/
@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;
function fail(x) { return []; }
function succeed(x) { return [x]; }
function disj(f1, f2) {
return function(x) {
return f1(x).concat(f2(x));
}
}
function conj(f1, f2) {
@buzzdecafe
buzzdecafe / Functor.js
Last active October 18, 2023 07:28 — forked from CrossEye/Functor.js
(function(global) {
global.Functor = function(conf) {
Functor.types[conf.key] = {
obj: conf.obj,
fmap: conf.fmap
};
};
Functor.types = {};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@buzzdecafe
buzzdecafe / generators.js
Last active December 19, 2015 12:18
generators again. tweaking scott's generators for ramda (cf. https://github.com/CrossEye/ramda/blob/master/examples/generators.js)
// require("ramda");
var gens = (function() {
var trampoline = function(fn) {
var result = fn.apply(this, tail(arguments));
while (typeof result == "function") {
result = result();
}
return result;
};
@buzzdecafe
buzzdecafe / composition.js
Last active December 20, 2015 21:09
working out composition problems in Ramda
// currently in Ramda:
var compose = R.compose = function() { // TODO: type check of arguments?
var fns = slice(arguments);
return function() {
return foldr(function(fn, args) {return [fn.apply(this, args)];}, slice(arguments), fns)[0];
};
};
//...
var useWith = R.useWith = _(function(fn /*, tranformers */) {