Rewrite flat and flatMap.

This commit is contained in:
David 2021-02-08 15:56:55 -06:00
parent 15f0fc2bdb
commit 44d833575b

View file

@ -170,17 +170,21 @@ const Channel = function(length = 0) {
return output;
},
flat: (depth) => {
flat: (depth) =>
readOnly.flatMap((value) =>
Channel.isChannel(value)
? depth > 1
? value.flat(depth - 1)
: value
: Channel.of(value)
),
flatMap: (mapperFunction, thisArg) => {
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 mapperFunction.call(thisArg, value).forEach(output.push);
});
await output.close();
@ -189,9 +193,6 @@ const Channel = function(length = 0) {
return output;
},
flatMap: (mapperFunction, thisArg) =>
readOnly.map(mapperFunction, thisArg).flat(),
forEach: async (callbackfn, thisArg) => {
for (;;) {
const value = await readOnly.shift();