我必须自己写一封信,我在这里和其他人分享,以便在这里轻松找到:
// subsequently yield iterators of given `size`
// these have to be fully consumed
function* batches(iterable, size) {
const it = iterable[Symbol.iterator]();
while (true) {
// this is for the case when batch ends at the end of iterable
// (we don't want to yield empty batch)
let {value, done} = it.next();
if (done) return value;
yield function*() {
yield value;
for (let curr = 1; curr < size; curr++) {
({value, done} = it.next());
if (done) return;
yield value;
}
}();
if (done) return value;
}
}
它产生发电机,而不是
Array
例如S。在调用之前,必须完全使用每个批
next()
再说一遍。