Skip to content

Instantly share code, notes, and snippets.

View dtipson's full-sized avatar

Drew dtipson

View GitHub Profile
@dtipson
dtipson / Ramda-program.js
Created November 3, 2015 22:13
Ramda Program
//Some extra sugar on top
let program = R.compose( R.flip(pipePromise), R.unapply(R.flatten) );
R.reduce( (a,b) => a+b ,0)//sum
R.reduce( (a,b) => a+b ,"")//join strings
R.reduce( (a,b) => a.length > b.length ? a : b ,"")//return the longest string
R.reduce( (a,b) => {a[b]=b;return a;}} ,{})//create object of keys set to their values
@dtipson
dtipson / Ramda-pipe.js
Last active April 6, 2017 03:55
Piping for Promises
let pipePromise = R.reduce( (p,fn) => p.then(fn) );
var bindP = function(promise, f) {
return promise.then(f);
};
var pipeP = _.curry(function(functionList, starting_promise) {
return _.reduce(functionList, bind, starting_promise);
});
var pipeP = _.curry(_.partial(_.reduce,_, bind),2);//shorter
var sequenceP = _.compose(pipeP,_.restParam(_.flattenDeep,0));
var bindP = function(promise, f) {
input.then(function(x) {
return f(x);
});
};
//ok, this is better
var bindP = function(promise, f) {
input.then(f);
};
@dtipson
dtipson / immutable-lensover.js
Last active August 29, 2015 14:28
composable lensOver constructs w/ Immutable.js instead of lodash's deepClone
function curry(fx) {
var arity = fx.length;
return function f1() {
var args = Array.prototype.slice.call(arguments, 0);
if (args.length >= arity) {
return fx.apply(null, args);
}
else {
return function f2() {
@dtipson
dtipson / one-line-single-page-app.js
Last active August 29, 2015 14:27
Instantly, turn any site, anywhere, into a single-page app, forever banishing load times!
document.documentElement.innerHTML = '<iframe src="'+document.location.href+'" style="position:absolute;top:0;left:0;width:100%;height:100%;border:0"></iframe>';
function parseJSON(json_string){
try {
return JSON.parse(json_string);
}
catch(e){
e.value = json_string;
return e;
}
}
function done_wrapper(fn){
return function(value){
fn(value);
return value;
};
}
function badlog(x){
console.log(x);
return 'return a complete nonsense string!';
//using the .then( successCB, failCB ) pattern
$.ajax('some-url')
.then( filter_and_format_results, filter_and_format_errors )
.done( display_results )
.fail( display_error_message );
//or, using the new .catch( failCB ) pattern
$.ajax('some-url')
.then( filter_and_format_results, )
.catch( filter_and_format_errors )