map

const fakeMap = (list, fn) = > {
  let res = []
  for (let i = 0; i < list.length; i++) {
    res.push(fn(list[i]));
  }
  return res;
}

const list = [1.2.3.4];
const f1 = x= > x * x;
fakeMap(list, f1); // [1, 4, 9, 16]
Copy the code

filter

const fakeFilter = (list, fn) = > {
  let res = []
  for (let i = 0; i < list.length; i++) {
    const ele = list[i];
    if (fn(ele)) {
      res.push(ele)
    }
  }
  return res;
}
const list = [1.2.3.4];
const f2 = x= > x > 2;
fakeFilter(list, f2); // [3, 4]
Copy the code

reduce

const fakeReduce = (list, fn, init) = > {
  let res = list[0], j = 1;
  init && (res = init) && (j = 0);
  for (let i = j; i < list.length; i++) {
    res = fn(res, list[i], i, list);
  }
  return res;
}
const list = [1.2.3.4];
const reducer = (acc, cur) = > acc + cur;
fakeReduce(list, reducer); / / 10
fakeReduce(list, reducer, 10); / / 20
Copy the code