Skip to content

Instantly share code, notes, and snippets.

View buzzdecafe's full-sized avatar
💭
quaquaquaqua

buzzdecafe

💭
quaquaquaqua
View GitHub Profile
@buzzdecafe
buzzdecafe / where.js
Created February 13, 2014 02:52
where: create a matching predicate from an object
function where(matchThis) {
var keys = Object.keys(matchThis);
return function(testObj) {
var match = ramda.all(function(key) {
return testObj[key] === matchThis[key];
}, keys);
return match ? testObj : false;
};
}
@buzzdecafe
buzzdecafe / solver.js
Last active August 29, 2015 13:58
recursive backtracker
// solver
var R = require('ramda');
function configure(isLeaf, isGoal, getChildren) {
return function recursiveSolve(node, sideEffects) {
return (isLeaf(node)) ? isGoal(node) && sideEffects(node):
R.any(function(child) {
sideEffects(child);
return recursiveSolve(child);
}, getChildren(node));
// Node.js CheatSheet.
// Download the Node.js source code or a pre-built installer for your platform, and start developing today.
// Download: http://nodejs.org/download/
// More: http://nodejs.org/api/all.html
// 0. Synopsis.
// http://nodejs.org/api/synopsis.html
function lazy(f) {
return function () {
var self = this, args = arguments;
return function () {
var data = f.apply(self, args);
return typeof data === "function" ?
data.apply(this, arguments) : data;
};
};
@buzzdecafe
buzzdecafe / xf.js
Created October 17, 2014 13:23
thinking about transducers
var R = require('ramda');
// cf. http://thecomputersarewinning.com/post/Transducers-Are-Fundamental/
function inc(n) {
return n + 1;
}
function meanReducer(acc, x) {
var newAcc = {};
function cond2() {
var cs = [].slice.call(arguments, 0, arguments.length - 1);
var ow = arguments[arguments.length - 1];
return function() {
var i = -1;
while(++i < cs.length) {
var value = cs[i].apply(this, arguments);
if (value) {
return value;
}
// run in ramda dir
var R = require('./ramda');
var Maybe = require('./ext/types/Maybe');
var ms = [Maybe(1), Maybe(2), Maybe(3)];
R.commute(Maybe.of, ms); //=> Maybe([1, 2, 3]) i.e., Just [1, 2, 3]
// or just use constructor fn:
R.commute(Maybe, ms); //=> Maybe([1, 2, 3])
// fizzbuzz ramda-style
var R = require('ramda');
R.forEach(R.cond(
[R.pipe(R.modulo(R.__, 15), R.eq(0)), function() { console.log('FizzBuzz'); }],
[R.pipe(R.modulo(R.__, 3), R.eq(0)), function() { console.log('Fizz'); }],
[R.pipe(R.modulo(R.__, 5), R.eq(0)), function() { console.log('Buzz'); }],
[R.alwaysTrue, function(){}]
), R.range(1, 101));
@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;
}, []);
}