Last active
August 30, 2015 14:41
-
-
Save RB-Lab/9b200c0821a0bb3a023a to your computer and use it in GitHub Desktop.
A Chrome snipet for http://drboolean.gitbooks.io/mostly-adequate-guide/
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
// -- Base -- // | |
// original from https://gist.github.com/amatiasq/2e4344792f28611fa499 | |
function curry(fn, length) { | |
length = length || fn.length; | |
return function currified() { | |
var args = [].slice.call(arguments); | |
if (args.length === 0) | |
return currified; | |
if (args.length >= length) | |
return fn.apply(this, args); | |
var child = fn.bind.apply(fn, [this].concat(args)); | |
return curry(child, length - args.length); | |
}; | |
} | |
// original from https://github.com/jashkenas/underscore/blob/master/underscore.js#L884 | |
var compose = function() { | |
var args = arguments; | |
var start = args.length - 1; | |
return function() { | |
var i = start; | |
var result = args[start].apply(this, arguments); | |
while (i--) { | |
result = args[i].call(this, result); | |
} | |
return result; | |
}; | |
}; | |
var id = function(x){ return x; }; | |
// -- Utilities -- // | |
var trace = curry(function(tag, x){ | |
console.log(tag, x); | |
return x; | |
}); | |
// -- Numbers -- // | |
var add = curry(function(a, b){ | |
return a + b; | |
}); | |
// -- Strings -- // | |
var split = curry(function split(divider, str){ | |
return str.split(divider); | |
}); | |
var toLower = function toLower(str){ | |
return str.toLowerCase(); | |
}; | |
var replace = curry(function split(replacement, replacer, str){ | |
return str.replace(replacement, replacer); | |
}); | |
var match = curry(function(reg, str){ | |
return reg.test(str); | |
}); | |
// -- Arrays -- // | |
var head = function(x) { return x[0]; }; | |
var last = function(x) { return x[x.length - 1]; }; | |
var join = curry(function curry(glue, arr){ | |
return arr.join(glue); | |
}) | |
var map = curry(function map(fn, arr){ | |
return arr.map(fn); | |
}); | |
var filter = curry(function(fn, arr){ | |
return arr.filter(fn); | |
}); | |
var reduce = curry(function(fn, initval, arr){ | |
return arr.reduce(fn, initval); | |
}); | |
var slice = curry(function(start, end, arr){ | |
return arr.slice(start, end); | |
}); | |
// -- Objects -- // | |
var prop = curry(function prop(prop, obj){ | |
return obj[prop]; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment