Skip to content

Commit 590ddf7

Browse files
feat(common): Added curry function
1 parent 1500b13 commit 590ddf7

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/common/common.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,7 @@ export function find(collection, callback) {
239239

240240
export function map<T> (collection: T, callback): T {
241241
var result = isArray(collection) ? [] : {};
242-
243-
forEach(collection, function(val, i) {
244-
result[i] = callback(val, i);
245-
});
242+
forEach(collection, (val, i) => result[i] = callback(val, i));
246243
return <T> result;
247244
}
248245

@@ -401,6 +398,20 @@ export function pattern(struct: Function[][]): Function {
401398

402399
export var isPromise = and(isObject, pipe(prop('then'), isFunction));
403400

401+
// Stolen: http://stackoverflow.com/questions/4394747/javascript-curry-function
402+
export function curry(fn: F): F {
403+
var initial_args = [].slice.apply(arguments, [1]);
404+
var func_args_length = fn.length;
405+
406+
function curried(args) {
407+
if (args.length >= func_args_length)
408+
return fn.apply(null, args);
409+
return function () {
410+
return curried(args.concat([].slice.apply(arguments)));
411+
};
412+
}
413+
return curried(initial_args);
414+
}
404415
/**
405416
* @ngdoc overview
406417
* @name ui.router.util

0 commit comments

Comments
 (0)