Skip to content

Instantly share code, notes, and snippets.

View buzzdecafe's full-sized avatar
💭
quaquaquaqua

buzzdecafe

💭
quaquaquaqua
View GitHub Profile
@buzzdecafe
buzzdecafe / compose.js
Last active March 27, 2017 16:22
curried binary compose
// first try:
R.cc = R.curry(function cc(f, g) {
return R.curryN(g.length, function() {
var gargs = Array.prototype.slice.call(arguments, 0, g.length);
var fargs = Array.prototype.slice.call(arguments, g.length);
return f.apply(this, [g.apply(this, gargs)].concat(fargs));
});
});
// so this works:
// 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));
// 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])
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;
}
@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 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;
};
};
// 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
@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));
@buzzdecafe
buzzdecafe / gridstyle.css
Last active January 1, 2016 07:51
sudoku solver
#grid {
border: 1px solid #000;
border-spacing:0;
}
#grid tr:nth-child(3) td,
#grid tr:nth-child(6) td {
border-bottom: 1px solid #000;
}
@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;
};
}