1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| const map = (array,fn) => { let results = []; for(const value of array) results.push(fn(value))
return results; }
const filter = (array,fn) => { let results = []; for(const value of array) (fn(value)) ? results.push(value) : undefined
return results; }
const concatAll = (array,fn) => { let results = []; for(const value of array) results.push.apply(results, value);
return results; }
const reduce = (array,fn,initialValue) => { let accumlator;
if(initialValue != undefined) accumlator = initialValue; else accumlator = array[0];
if(initialValue === undefined) for(let i=1;i<array.length;i++) accumlator = fn(accumlator,array[i]) else for(const value of array) accumlator = fn(accumlator,value)
return [accumlator] }
const zip = (leftArr,rightArr,fn) => { let index, results = [];
for(index = 0;index < Math.min(leftArr.length, rightArr.length);index++) results.push(fn(leftArr[index],rightArr[index])); return results; }
zip([1,2,3],[4,5,6],(x,y) => x+y))
|