|
@@ -5,6 +5,9 @@
|
|
// fn(5)
|
|
// fn(5)
|
|
// fn()
|
|
// fn()
|
|
// console.log(1,2,3,4,5)
|
|
// console.log(1,2,3,4,5)
|
|
|
|
+function isFunction(fn) {
|
|
|
|
+ return Object.prototype.toString.call(fn)=== '[object Function]';
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
let fn = _curry(console.log,'test');
|
|
let fn = _curry(console.log,'test');
|
|
@@ -26,4 +29,99 @@ function _curry(fn){
|
|
return fn(...params,...arguments);
|
|
return fn(...params,...arguments);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|
|
|
|
+console.log('\n--------------------------------\n');
|
|
|
|
+/**
|
|
|
|
+ * @returns {(function(): (*))|*}
|
|
|
|
+ */
|
|
|
|
+ function curry(){
|
|
|
|
+ let _arguments = Array.from(arguments);
|
|
|
|
+ let runLength = null;
|
|
|
|
+ let fn = null;
|
|
|
|
+ let params = [..._arguments];
|
|
|
|
+ let cb = function (){
|
|
|
|
+ let __arguments = Array.from(arguments);
|
|
|
|
+ params.push(...__arguments);
|
|
|
|
+ // 本次函数的参数
|
|
|
|
+ // 历史函数的参数
|
|
|
|
+ // 参数数量是否至少两个. 一般需要三个
|
|
|
|
+ if(fn){
|
|
|
|
+ // 空值执行
|
|
|
|
+ if(__arguments.length<1 || (runLength && runLength <= params.length)){
|
|
|
|
+ return fn(...params);
|
|
|
|
+ }
|
|
|
|
+ return cb;
|
|
|
|
+ }
|
|
|
|
+ // console.log(params);
|
|
|
|
+ // 从输入中提取要执行的函数
|
|
|
|
+ if(typeof params[0] === 'number' && isFunction(params[1])){
|
|
|
|
+ runLength = params[0];
|
|
|
|
+ fn = params[1];
|
|
|
|
+ params = params.splice(2);
|
|
|
|
+ }else if(isFunction(params[0])){
|
|
|
|
+ runLength = null;
|
|
|
|
+ fn = params[0];
|
|
|
|
+ params = params.splice(1);
|
|
|
|
+ if(fn.length){
|
|
|
|
+ console.log(fn.length);
|
|
|
|
+ runLength = fn.length;
|
|
|
|
+ }
|
|
|
|
+ }else if(typeof params[0] === 'number'){
|
|
|
|
+ runLength = params[0];
|
|
|
|
+ params = params.splice(1);
|
|
|
|
+ }else{
|
|
|
|
+ throw {message:'first param must function or number'}
|
|
|
|
+ }
|
|
|
|
+ // console.log(params);
|
|
|
|
+ if(fn){
|
|
|
|
+ if( runLength && runLength <= params.length){
|
|
|
|
+ console.log('?');
|
|
|
|
+
|
|
|
|
+ return fn(...params);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return cb;
|
|
|
|
+ }
|
|
|
|
+ return cb;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+function curry2(func) {
|
|
|
|
+ return function curried(...args) {
|
|
|
|
+ if (args.length >= func.length) {
|
|
|
|
+ return func.apply(this, args);
|
|
|
|
+ } else {
|
|
|
|
+ return function(...args2) {
|
|
|
|
+ return curried.apply(this, args.concat(args2));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+let c1 = function (event,msg){
|
|
|
|
+ console.log(`event:${event} say ${msg}`)
|
|
|
|
+}
|
|
|
|
+let c2 = function (f1,f2,f3,f4){
|
|
|
|
+ console.log(`1: ${f1}\n2: ${f2}\n3: ${f3}\n4: ${f4}`)
|
|
|
|
+}
|
|
|
|
+let c5 =function(){
|
|
|
|
+ console.log(...arguments);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+let fn1 = curry(c1);
|
|
|
|
+let r1 = fn1('text')('你好');
|
|
|
|
+console.log('--------');
|
|
|
|
+let fn2 = curry(4);
|
|
|
|
+let r2 = fn2(c2)('a','b')('c','d');
|
|
|
|
+console.log('--------');
|
|
|
|
+let fn3 = curry(4,c2,'fn3 1','fn3 2');
|
|
|
|
+let r3 = fn3('tes')('你好','9999999');
|
|
|
|
+console.log('--------');
|
|
|
|
+let fn4 = curry(2,console.log);
|
|
|
|
+let r4 = fn4('fn4');
|
|
|
|
+console.log('--------');
|
|
|
|
+let fn5 = curry(c5);
|
|
|
|
+let r5 = fn5('text555')('你好---')();
|
|
|
|
+console.log('--------');
|
|
|
|
+
|
|
|
|
+
|