ES6中的高阶函数问题及解决方法:循环问题

  一,用好 filter,map,和其它 ES6 新增的高阶遍历函数

  问题一: 将数组中的空值去除

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const arrContainsEmptyVal = [3, 4, 5, 2, 3, undefined, null, 0, ""];
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const compact = arr => arr.filter(Boolean);
复制代码`</pre>

  问题二: 将数组中的 VIP 用户余额加 10

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const VIPUsers = [
 { username: "Kelly", isVIP: true, balance: 20 },
 { username: "Tom", isVIP: false, balance: 19 },
 { username: "Stephanie", isVIP: true, balance: 30 }
];
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`VIPUsers.map(
 user => (user.isVIP ? { ...user, balance: user.balance + 10 } : user)
);
复制代码`</pre>

  问题三: 判断字符串中是否含有元音字母

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const randomStr = "hdjrwqpi";
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const isVowel = char => ["a", "e", "o", "i", "u"].includes(char);
const containsVowel = str => [...str].some(isVowel);
containsVowel(randomStr);
复制代码`</pre>

  问题四: 判断用户是否全部是成年人

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const users = [
 { name: "Jim", age: 23 },
 { name: "Lily", age: 17 },
 { name: "Will", age: 25 }
];
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`users.every(user => user.age >= 18);
复制代码`</pre>

  问题五: 找出上面用户中的未成年人

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const findTeen = users => users.find(user => user.age < 18);
findTeen(users);
复制代码`</pre>

  问题六: 将数组中重复项清除

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const dupArr = [1, 2, 3, 3, 3, 3, 6, 7];
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const uniq = arr => [...new Set(arr)];
uniq(dupArr);
复制代码`</pre>

  问题七: 生成由随机整数组成的数组,数组长度和元素大小可自定义

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const genNumArr = (length, limit) =>
 Array.from({ length }, _ => Math.floor(Math.random() * limit));
genNumArr(10, 100);
复制代码`</pre>

  二,理解和熟练使用 reduce

  问题八: 不借助原生高阶函数,定义 reduce

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const reduce = (f, acc, arr) => {
 if (arr.length === 0) return acc;
 const [head, ...tail] = arr;
 return reduce(f, f(head, acc), tail);
};
复制代码`</pre>

  问题九: 将多层数组转换成一层数组

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const nestedArr = [1, 2, [3, 4, [5, 6]]];
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const flatten = arr =>
 arr.reduce(
   (flat, next) => flat.concat(Array.isArray(next) ? flatten(next) : next),
   []
 );
复制代码`</pre>

  问题十: 将下面数组转成对象,key/value 对应里层数组的两个值

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const objLikeArr = [["name", "Jim"], ["age", 18], ["single", true]];
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const fromPairs = pairs =>
 pairs.reduce((res, pair) => ((res[pair[0]] = pair[1]), res), {});
fromPairs(objLikeArr);
复制代码`</pre>

  问题十一: 取出对象中的深层属性

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const deepAttr = { a: { b: { c: 15 } } };
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const pluckDeep = path => obj =>
 path.split(".").reduce((val, attr) => val[attr], obj);
pluckDeep("a.b.c")(deepAttr);
复制代码`</pre>

  问题十二: reduce 的计算过程,在范畴论里面叫 catamorphism,即一种连接的变形。和它相反的变形叫 anamorphism。现在我们定义一个和 reduce 计算过程相反的函数 unfold(注:reduce 在 Haskell 里面叫 fold,对应 unfold)

  for循环创建数组 js_js有哪些循环_js 循环 continue

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const unfold = (f, seed) => {
 const go = (f, seed, acc) => {
   const res = f(seed);
   return res ? go(f, res[1], acc.concat(res[0])) : acc;
 };
 return go(f, seed, []);
};
复制代码`</pre>

  根据这个 unfold 函数,定义一个 Python 里面的 range 函数。

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const range = (min, max, step = 1) =>
 unfold(x => x < max && [x, x + step], min);
复制代码`</pre>

  三,用递归代替循环

  问题十三: 将两个数组每个元素一一对应相加

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const num1 = [3, 4, 5, 6, 7];
const num2 = [43, 23, 5, 67, 87];
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const zipWith = f => xs => ys => {
 if (xs.length === 0 || ys.length === 0) return [];
 const [xHead, ...xTail] = xs;
 const [yHead, ...yTail] = ys;
 return [f(xHead)(yHead), ...zipWith(f)(xTail)(yTail)];
};
const add = x => y => x + y;
zipWith(add)(num1)(num2);
复制代码`</pre>

  问题十四: 将 Stark 家族成员提取出来。注意,目标数据在数组前面,使用 filter 方法遍历整个数组是浪费。

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const houses = [
 "Eddard Stark",
 "Catelyn Stark",
 "Rickard Stark",
 "Brandon Stark",
 "Rob Stark",
 "Sansa Stark",
 "Arya Stark",
 "Bran Stark",
 "Rickon Stark",
 "Lyanna Stark",
 "Tywin Lannister",
 "Cersei Lannister",
 "Jaime Lannister",
 "Tyrion Lannister",
 "Joffrey Baratheon"
];
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const takeWhile = f => ([head, ...tail]) =>
 f(head) ? [head, ...takeWhile(f)(tail)] : [];
const isStark = name => name.toLowerCase().includes("stark");
takeWhile(isStark)(houses);
复制代码`</pre>

  四,使用高阶函数遍历数组时可能遇到的陷阱

  问题十五: 从长度为 100 万的随机整数组成的数组中取出偶数,再把所有数字乘以 3

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`// 用我们刚刚定义的辅助函数来生成符合要求的数组
const bigArr = genNumArr(1e6, 100);
复制代码`</pre>

  能运行的答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const isOdd = num => num % 2 === 0;
const triple = num => num * 3;
bigArr.filter(isOdd).map(triple);
复制代码`</pre>

  注意,上面的解决方案将数组遍历了两次,无疑是浪费。如果写 for 循环,只用遍历一次:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const results = [];
for (let i = 0; i < bigArr.length; i++) {
 if (isOdd(bigArr[i])) {
   results.push(triple(bigArr[i]));
 }
}
复制代码`</pre>

  在我的电脑上测试,先 filter 再 map 的方法耗时 105.024 ms,而采用 for 循环的方法耗时仅 25.598 ms!那是否说明遇到此类情况必须用 for 循环解决呢? No!

  五,死磕到底,Transduce!

  我们先用 reduce 来定义 filter 和 map,至于为什么这样做等下再解释。

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const filter = (f, arr) =>
 arr.reduce((acc, val) => (f(val) && acc.push(val), acc), []);
const map = (f, arr) => arr.reduce((acc, val) => (acc.push(f(val)), acc), []);
复制代码`</pre>

  重新定义的 filter 和 map 有共有的逻辑。我们把这部分共有的逻辑叫做 reducer。有了共有的逻辑后,我们可以进一步地抽象,把 reducer 抽离出来,然后传入 filter 和 map:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const filter = f => reducer => (acc, value) => {
 if (f(value)) return reducer(acc, value);
 return acc;
};
const map = f => reducer => (acc, value) => reducer(acc, f(value));
复制代码`</pre>

  现在 filter 和 map 的函数 signature 一样,我们就可以进行函数组合(function composition)了。

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const pushReducer = (acc, value) => (acc.push(value), acc);
bigNum.reduce(map(triple)(filter(isOdd)(pushReducer)), []);
复制代码`</pre>

  js有哪些循环_for循环创建数组 js_js 循环 continue

  但是这样嵌套写法易读性太差,很容易出错。我们可以写一个工具函数来辅助函数组合:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const pipe = (...fns) => (...args) => fns.reduce((fx, fy) => fy(fx), ...args);
复制代码`</pre>

  然后我们就可以优雅地组合函数了:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`bigNum.reduce(
 pipe(
   filter(isOdd),
   map(triple)
 )(pushReducer),
 []
);
复制代码`</pre>

  经过测试(用 console.time()/console.timeEnd()),上面的写法耗时 33.898 msjs有哪些循环,仅比 for 循环慢 8 ms。为了代码的易维护性和易读性,这点性能上的微小牺牲,我认为是可以接受的。

  这种写法叫 transduce。有很多工具库提供了 transducer 函数。比如 transducers-js。除了用 transducer 来遍历数组,还能用它来遍历对象和其它数据集。功能相当强大。

  六,for 循环和 for … of 循环的区别

  for … of 循环是在 ES6 引入 Iterator 后,为了遍历 Iterable 数据类型才产生的。EcmaScript 的 Iterable 数据类型有数组,字符串js有哪些循环,Set 和 Map。for … of 循环属于重型的操作(具体细节我也没了解过),如果用 AirBNB 的 ESLint 规则,在代码中使用 for … of 来遍历数组是会被禁止的。

  那么,for … of 循环应该在哪些场景使用呢?目前我发现的合理使用场景是遍历自定义的 Iterable。来看这个题目:

  问题十六: 将 Stark 家族成员名字遍历,每次遍历暂停一秒,然后将当前遍历的名字打印来,遍历完后回到第一个元素再重新开始,无限循环。

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`const starks = [
 "Eddard Stark",
 "Catelyn Stark",
 "Rickard Stark",
 "Brandon Stark",
 "Rob Stark",
 "Sansa Stark",
 "Arya Stark",
 "Bran Stark",
 "Rickon Stark",
 "Lyanna Stark"
];
复制代码`</pre>

  答案:

  <pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">`function* repeatedArr(arr) {
 let i = 0;
 while (true) {
   yield arr[i++ % arr.length];
 }
}
const infiniteNameList = repeatedArr(starks);
const wait = ms =>
 new Promise(resolve => {
   setTimeout(() => {
     resolve();
   }, ms);
 });
(async () => {
 for (name of infiniteNameList) {
   await wait(1000);
   console.log(name);
 }
})();
复制代码`</pre>

  七,放弃倔强,实在需要用 for 循环了

  前面讲到的问题基本覆盖了大部分需要使用 for 循环的场景。那是否我们可以保证永远不用 for 循环呢?其实不是。我讲了这么多,其实是在鼓励大家不要写 for 循环,而不是不用 for 循环。我们常用的数组原型链上的 map,filter 等高阶函数,底层其实是用 for 循环实现的。在需要写一些底层代码的时候,还是需要写 for 循环的。来看这个例子:

<p><pre style="font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em;line-height: 1.2em;margin: 1.2em 0px;">Number.prototype[Symbol.iterator] = function*() {
 for (let i = 0; i

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1538
0 评论
334

发表评论

!