Array的reducer方法; //1:自定義reducer函數 Array.prototype.myReduce = function ( fn = (pre, curr, index, arr) => {}, initValue ) { let acc; for (let i = 0; i < this.length; i++) { if (initValue !== undefined && i === 0) { acc = initValue; } if (i === 0 && initValue === undefined) { acc = this[i]; } else { acc = fn(acc, this[i], i); } } return acc; }; // 測試1: without initValue ['a', 'b', 'c', 'd'].myReduce((pre, curr, i) => { console.log( `pre:${pre}, curr:${curr}, index:${i}, return value:${pre + curr}` ); return pre + curr; }); /* print result pre:a, curr:b, index:1, return value:ab pre:ab, curr:c, index:2, return value:abc pre:abc, curr:d, index:3, return value:abcd */ // 測試2: with initValue [('a', 'b', 'c', 'd')].myReduce((pre, curr, i) => { console.log( `pre:${pre}, curr:${curr}, index:${i}, return value:${pre + curr}` ); return pre + curr; }, 'jeff'); /* print result pre:jeff, curr:a, index:0, return value:jeffa pre:jeffa, curr:b, index:1, return value:jeffab pre:jeffab, curr:c, index:2, return value:jeffabc pre:jeffabc, curr:d, index:3, return value:jeffabcd */ //2: 實現pipe管道函數 function a(s) { return s + 'a'; } function b(s) { return s + 'b'; } function upperCase(s) { return s.toUpperCase(); } function addHello(s) { return s + ',hello'; } [a, b].reduce((fx, fy) => { return function (arg) { return fy(fx(arg)); }; })('jeff'); /** * 嵌套执行的函数平铺,從左到右 fy(fx(arg)) * @param {...any} funs fx(arg) fy(arg) * @returns function(arg){fy(fx(arg))} */ const pipe = (...funs) => { return function (...args) { const length = funs.length; if (length === 0) { return args; } else if (length === 1) { return funs[0](...args); } return funs.reduce((fx, fy) => { return fy(fx(...args)); }); }; }; const upperCaseThenAdd = pipe(upperCase, addHello); upperCaseThenAdd('jeff'); // output:"JEFF,hello" const addThenUpperCase = pipe(addHello, upperCase); addThenUpperCase('jeff'); // output:"JEFF,HELLO" const upperCaseString = pipe(upperCase); upperCaseString('jeff'); // output:"JEFF" //3: 實現compose函數 // 嵌套执行的函数平铺,從右到左
自定义reducer,pipe,compose函数
Array的reducer方法;//1:自定義reducer函數Array.prototype.myReduce = function ( fn = (pre, curr, index, arr) => {}, initValue) { let acc; for (let i = 0; i < this.length; i++) { if (initValue !== undefined && i === 0) { acc = initValue; } if (i === 0 && initValue === undefined) { acc = this[i]; } else { acc = fn(acc, this[i], i); } }
return acc;};
// 測試1: without initValue['a', 'b', 'c', 'd'].myReduce((pre, curr, i) => { console.log( `pre:${pre}, curr:${curr}, index:${i}, return value:${pre + curr}` ); return pre + curr;});
/* print result pre:a, curr:b, index:1, return value:ab pre:ab, curr:c, index:2, return value:abc pre:abc, curr:d, index:3, return value:abcd */
// 測試2: with initValue[('a', 'b', 'c', 'd')].myReduce((pre, curr, i) => { console.log( `pre:${pre}, curr:${curr}, index:${i}, return value:${pre + curr}` ); return pre + curr;}, 'jeff');
/* print result pre:jeff, curr:a, index:0, return value:jeffa pre:jeffa, curr:b, index:1, return value:jeffab pre:jeffab, curr:c, index:2, return value:jeffabc pre:jeffabc, curr:d, index:3, return value:jeffabcd */
//2: 實現pipe管道函數function a(s) { return s + 'a';}
function b(s) { return s + 'b';}function upperCase(s) { return s.toUpperCase();}function addHello(s) { return s + ',hello';}
[a, b].reduce((fx, fy) => { return function (arg) { return fy(fx(arg)); };})('jeff');
/** * 嵌套执行的函数平铺,從左到右 fy(fx(arg)) * @param {...any} funs fx(arg) fy(arg) * @returns function(arg){fy(fx(arg))} */const pipe = (...funs) => { return function (...args) { const length = funs.length; if (length === 0) { return args; } else if (length === 1) { return funs[0](...args); } return funs.reduce((fx, fy) => { return fy(fx(...args)); }); };};
const upperCaseThenAdd = pipe(upperCase, addHello);upperCaseThenAdd('jeff');// output:"JEFF,hello"
const addThenUpperCase = pipe(addHello, upperCase);addThenUpperCase('jeff');// output:"JEFF,HELLO"
const upperCaseString = pipe(upperCase);upperCaseString('jeff');// output:"JEFF"
//3: 實現compose函數// 嵌套执行的函数平铺,從右到左
人生旅途,边走边看...