diff --git a/lib/index.js b/lib/index.js index de39475..8865485 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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();