-
-
Save slifin/109977ae637e26dddc7c to your computer and use it in GitHub Desktop.
js underscore
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
var _ = (function(){ | |
function _(){} | |
_.prototype.compose = function(){ | |
var args = Array.prototype.slice.call(arguments); | |
return args.reduce(function(f,g){ | |
return function(){ | |
return f(g.apply(this,arguments)); | |
}; | |
}); | |
}; | |
_.prototype.curry = function(){ | |
var args = Array.prototype.slice.call(arguments); | |
var func = args.shift(); | |
return function(){ | |
var newArgs = Array.prototype.slice.call(arguments); | |
return func.apply(undefined, args.map(function(v){ | |
if (v.constructor && v.constructor.name && v.constructor.name == '_') | |
return newArgs.shift(); | |
return v; | |
})); | |
}; | |
}; | |
_.prototype.filter = function(func, data){ | |
data = Array.prototype.slice.call(data); | |
return data.filter(func); | |
}; | |
_.prototype.get = function(base, offset, fallback){ | |
try{ | |
return eval("base"+offset); | |
}catch(e){ | |
return fallback; | |
} | |
}; | |
_.prototype.map = function(func, data){ | |
data = Array.prototype.slice.call(data); | |
return data.map(func); | |
}; | |
_.prototype.reduce = function(func, data){ | |
data = Array.prototype.slice.call(data); | |
return data.reduce(func); | |
}; | |
return new _(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment