Add flat and flatMap.

This commit is contained in:
David Braun 2019-04-24 09:27:01 -04:00
parent e9023771cc
commit 6b1c55cff3
No known key found for this signature in database
GPG key ID: 87EC41ADF710B7E2
5 changed files with 129 additions and 62 deletions

View file

@ -172,6 +172,28 @@ const Channel = function(length = 0) {
return output;
},
flat: depth => {
const output = Channel();
(async () => {
await readOnly.forEach(async value => {
if (Channel.isChannel(value)) {
const input = depth > 1 ? value.flat(depth - 1) : value;
await input.forEach(output.push);
} else {
await output.push(value);
}
});
await output.close();
})();
return output;
},
flatMap: (mapperFunction, thisArg) =>
readOnly.map(mapperFunction, thisArg).flat(),
forEach: async (callbackfn, thisArg) => {
for (;;) {
const value = await readOnly.shift();