在问题Iterate a list as pair (current, next) in Python中,OP感兴趣的是将Python列表作为一系列current, next对进行迭代。我也有同样的问题,但是我想用最干净的方式在JavaScript中解决这个问题,也许可以使用lodash。
使用一个简单的for循环很容易做到这一点,但感觉并不是很优雅。
for (var i = 0; i < arr.length - 1; i++) {
var currentElement = arr[i];
var nextElement = arr[i + 1];
}Lodash几乎可以做到这一点:
_.forEach(_.zip(arr, _.rest(arr)), function(tuple) {
var currentElement = tuple[0];
var nextElement = tuple[1];
})在最后一次迭代中,nextElement将是undefined,这是一个微妙的问题。
当然,理想的解决方案应该是一个只在必要时循环的pairwise lodash函数。
_.pairwise(arr, function(current, next) {
// do stuff
});有没有现成的库已经这样做了?或者,有没有另一种我没有尝试过的在JavaScript中进行成对迭代的好方法?
说明:如果为arr = [1, 2, 3, 4],则我的pairwise函数将按如下方式迭代:[1, 2],[2, 3],[3, 4],而不是[1, 2],[3, 4]。这就是操作员在the original question for Python中询问的内容。
发布于 2018-02-21 19:02:54
下面是一个没有任何依赖项的通用函数解决方案:
const nWise = (n, array) => {
iterators = Array(n).fill()
.map(() => array[Symbol.iterator]());
iterators
.forEach((it, index) => Array(index).fill()
.forEach(() => it.next()));
return Array(array.length - n + 1).fill()
.map(() => (iterators
.map(it => it.next().value);
};
const pairWise = (array) => nWise(2, array);我知道看起来一点都不好,但通过引入一些通用的实用函数,我们可以让它看起来更好:
const sizedArray = (n) => Array(n).fill();我可以结合使用sizedArray和forEach来实现times,但这是一个低效的实现。对于这样一个不言自明的函数,使用命令式代码是可以的:
const times = (n, cb) => {
while (0 < n--) {
cb();
}
}如果您对更多核心解决方案感兴趣,请查看this answer。
不幸的是,Array.fill只接受单个值,不接受回调。所以Array(n).fill(array[Symbol.iterator]())会在每个位置放置相同的值。我们可以通过以下方式来解决这个问题:
const fillWithCb = (n, cb) => sizedArray(n).map(cb);最终实现:
const nWise = (n, array) => {
iterators = fillWithCb(n, () => array[Symbol.iterator]());
iterators.forEach((it, index) => times(index, () => it.next()));
return fillWithCb(
array.length - n + 1,
() => (iterators.map(it => it.next().value),
);
};通过将参数样式更改为currying,配对的定义看起来会好得多:
const nWise = n => array => {
iterators = fillWithCb(n, () => array[Symbol.iterator]());
iterators.forEach((it, index) => times(index, () => it.next()));
return fillWithCb(
array.length - n + 1,
() => iterators.map(it => it.next().value),
);
};
const pairWise = nWise(2);如果你运行下面的代码,你会得到:
> pairWise([1, 2, 3, 4, 5]);
// [ [ 1, 2 ], [ 2, 3 ], [ 3, 4 ], [ 4, 5 ] ]https://stackoverflow.com/questions/31973278
复制相似问题