Created
April 20, 2014 13:37
-
-
Save raineorshine/11114359 to your computer and use it in GitHub Desktop.
Maybe some day I'll understand the code I wrote...?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// functional helpers | |
var before = function() { | |
var functions = Array.prototype.slice.call(arguments, 0); | |
return function() { | |
var that = this; | |
var args = Array.prototype.slice.call(arguments, 0); | |
var ccat = [].concat.bind([]); | |
return curry(tail, functions.map( | |
compose( | |
curry(invoke.apply, that), | |
( | |
curry(compose, rcurry(aritize, 1)) | |
(rthunkify(args)) | |
)(ccat) | |
) | |
))(); | |
}; | |
} | |
// old before | |
// var before = function() { | |
// var args = Array.prototype.slice.call(arguments, 0); | |
// return function() { | |
// f.apply(this, arguments); | |
// return g.apply(this, arguments); | |
// }; | |
// } | |
// SIMPLIFYING STRUCTURES | |
// function(f) { | |
// return curry(g)(); | |
// } | |
// curry(g) | |
// function(f) { | |
// return compose(z)(f); | |
// } | |
// compose(z) | |
var wrap = function(x) { return '(' + x + ')'; } | |
var head = function(arr) { return arr[0]; }; | |
var tail = function(arr) { return arr[arr.length-1]; }; | |
var I = function(x) { return x; }; | |
var args = function() { return Array.prototype.slice.call(arguments); }; | |
var getValue = function(obj, key) { return obj[key]; } | |
var invoke = function(f) { | |
var partials = Array.prototype.slice.call(arguments, 1); | |
return f.apply(this, partials); | |
} | |
invoke.apply = invoke.apply.bind(invoke); | |
var curry = function(f) { | |
var partials = Array.prototype.slice.call(arguments, 1); | |
return f.bind.apply(f, [].concat(this, partials)); | |
} | |
var rcurry = function(f) { | |
var that = this; | |
var partials = Array.prototype.slice.call(arguments, 1); | |
return function() { | |
var args = Array.prototype.slice.call(arguments); | |
return f.apply(that, [].concat(args, partials)); | |
}; | |
}; | |
var aritize = function(f, n) { | |
return function() { | |
var args = Array.prototype.slice.call(arguments, 0, n); | |
return f.apply(this, args); | |
} | |
} | |
// thunkify(log) | |
// curry(curry, log) | |
var thunkify = curry(curry, curry); | |
var rthunkify = curry(rcurry, rcurry); | |
var compose = function(f, g) { | |
var that = this; | |
return function() { | |
return f(g.apply(that, arguments)); | |
} | |
} | |
var sequence = function() { | |
var args = Array.prototype.slice.call(arguments, 0); | |
return compose.apply(this, args.reverse()); | |
}; | |
// lambda functions | |
// var getUser = new Promise(function(cb) { | |
// cb({ name: 'raine' }); | |
// }); | |
// var getName = function(user) { | |
// return user.name; | |
// }; | |
// var log = console.log.bind(console); | |
// var say = function(str) { | |
// return function(val) { | |
// console.log(str); | |
// return val; | |
// }; | |
// } | |
// point-free style | |
var getUser = new Promise(aritize(rcurry(invoke, { name: 'raine' }), 1)); | |
var getName = rcurry(getValue, 'name'); | |
var log = console.log.bind(console); | |
var say = sequence( | |
thunkify(aritize(log, 1)), | |
rcurry(before, I) | |
); | |
getUser | |
.then(say(1)) | |
.then(log) | |
getUser | |
.then(say(2)) | |
.then(getName) | |
.then(say(3)) | |
.then(log) | |
undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment