fp-ts-iterators
Advanced tools
Comparing version
@@ -432,5 +432,60 @@ "use strict"; | ||
*/ | ||
const flatMapIterable = (f) => { | ||
const aif = (a) => (0, function_1.pipe)(a, f, exports.fromIterable); | ||
return (0, exports.flatMap)(aif); | ||
const flatMapIterable = (f) => (fa) => { | ||
const buffer = []; | ||
const fromBufferQueue = []; | ||
const nextAIFB = (0, function_1.pipe)(fa, AsyncIterableReduce_1.getAsyncIteratorNextTask, TO.map(f)); | ||
let isNextAIFBDone = false; | ||
let waitingForValues = 0; // fight against race conditions | ||
async function toBuffer() { | ||
if (!isNextAIFBDone) { | ||
waitingForValues++; | ||
const maybeAIB = await nextAIFB(); | ||
waitingForValues--; | ||
if (O.isSome(maybeAIB)) { | ||
buffer.push(...maybeAIB.value); | ||
} | ||
else { | ||
isNextAIFBDone = true; | ||
} | ||
} | ||
if (!isNextAIFBDone && buffer.length === 0) { | ||
await toBuffer(); | ||
} | ||
dispatchFromBuffer(); | ||
} | ||
function dispatchFromBuffer() { | ||
if (buffer.length > 0) { | ||
const fromTasks = fromBufferQueue.splice(0, buffer.length); | ||
const values = buffer.splice(0, fromTasks.length); | ||
(0, function_1.pipe)(fromTasks, A.zip(values), A.map(([task, value]) => { | ||
task(O.some(value)); | ||
})); | ||
} | ||
if (waitingForValues > 0 || buffer.length > 0) { | ||
return; | ||
} | ||
const hopeless = fromBufferQueue.splice(0); | ||
hopeless.forEach((task) => { | ||
task(O.none); | ||
}); | ||
} | ||
function addToFromBufferQueue() { | ||
return new Promise((resolve) => fromBufferQueue.push(resolve)); | ||
} | ||
async function next() { | ||
if (buffer.length > 0) { | ||
return O.some(buffer.shift()); | ||
} | ||
const fromBuffer = addToFromBufferQueue(); | ||
await toBuffer(); | ||
return fromBuffer; | ||
} | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
const o = await next(); | ||
if (O.isSome(o)) { | ||
yield o.value; | ||
} | ||
}, | ||
}; | ||
}; | ||
@@ -437,0 +492,0 @@ exports.flatMapIterable = flatMapIterable; |
@@ -330,6 +330,3 @@ "use strict"; | ||
*/ | ||
const flatMapIterable = (f) => { | ||
const aief = (a) => (0, function_1.pipe)(a, f, exports.fromIterable); | ||
return (0, exports.flatMap)(aief); | ||
}; | ||
const flatMapIterable = (f) => (ma) => (0, function_1.pipe)(ma, AI.flatMapIterable((e) => E.isLeft(e) ? I.of(e) : (0, function_1.pipe)(e.right, f, I.map((E.right))))); | ||
exports.flatMapIterable = flatMapIterable; | ||
@@ -336,0 +333,0 @@ /** |
@@ -36,4 +36,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Applicative = exports.apSecond = exports.apFirst = exports.Apply = exports.flap = exports.asUnit = exports.as = exports.Functor = exports.Pointed = exports.filterMap = exports.filter = exports.compact = exports.flatten = exports.flatMapTaskOption = exports.flatMapTaskEither = exports.flatMapTask = exports.flatMapOption = exports.flatMapEither = exports.flatMap = exports.ap = exports.map = exports.fromNullable = exports.getOrElseW = exports.matchEW = exports.matchW = exports.match = exports.fromTaskOptionK = exports.fromTaskOption = exports.fromTaskEitherK = exports.fromTaskEither = exports.fromTask = exports.fromAsyncIterableEither = exports.fromAsyncIterable = exports.fromIterable = exports.fromIO = exports.someIO = exports.fromEitherK = exports.fromOptionK = exports.fromOption = exports.FromEither = exports.fromEither = exports.fromPredicate = exports.someTask = exports.someAsyncIterable = exports.someIterable = exports.none = exports.zero = exports.of = exports.some = exports.URI = void 0; | ||
exports.apS = exports.let = exports.bindTo = exports.bind = exports.Do = exports.tapTaskOption = exports.tapTaskEither = exports.tapTask = exports.tapIO = exports.tapEither = exports.tap = exports.MonadTask = exports.MonadIO = exports.MonadThrow = exports.throwError = exports.Monad = exports.fromTaskK = exports.FromTask = exports.fromIOK = exports.FromIO = exports.Chain = void 0; | ||
exports.apSecond = exports.apFirst = exports.Apply = exports.flap = exports.asUnit = exports.as = exports.Functor = exports.Pointed = exports.filterMap = exports.filter = exports.compact = exports.flatten = exports.flatMapTaskOption = exports.flatMapTaskEither = exports.flatMapTask = exports.flatMapOption = exports.flatMapEither = exports.flatMapIterable = exports.flatMap = exports.ap = exports.map = exports.fromNullable = exports.getOrElseW = exports.matchEW = exports.matchW = exports.match = exports.fromTaskOptionK = exports.fromTaskOption = exports.fromTaskEitherK = exports.fromTaskEither = exports.fromTask = exports.fromAsyncIterableEither = exports.fromAsyncIterable = exports.fromIterable = exports.fromIO = exports.someIO = exports.fromEitherK = exports.fromOptionK = exports.fromOption = exports.FromEither = exports.fromEither = exports.fromPredicate = exports.someTask = exports.someAsyncIterable = exports.someIterable = exports.none = exports.zero = exports.of = exports.some = exports.URI = void 0; | ||
exports.apS = exports.let = exports.bindTo = exports.bind = exports.Do = exports.tapTaskOption = exports.tapTaskEither = exports.tapTask = exports.tapIO = exports.tapEither = exports.tap = exports.MonadTask = exports.MonadIO = exports.MonadThrow = exports.throwError = exports.Monad = exports.fromTaskK = exports.FromTask = exports.fromIOK = exports.FromIO = exports.Chain = exports.Applicative = void 0; | ||
exports.matchE = matchE; | ||
@@ -300,2 +300,8 @@ exports.getOrElse = getOrElse; | ||
*/ | ||
const flatMapIterable = (f) => (ma) => (0, function_1.pipe)(ma, AI.flatMapIterable((o) => O.isNone(o) ? I.of(o) : (0, function_1.pipe)(o.value, f, I.map(O.some)))); | ||
exports.flatMapIterable = flatMapIterable; | ||
/** | ||
* @category sequencing | ||
* @since 1.0.0 | ||
*/ | ||
const flatMapEither = (f) => (fa) => ({ | ||
@@ -302,0 +308,0 @@ async *[Symbol.asyncIterator]() { |
@@ -373,5 +373,60 @@ import { apFirst as apFirst_, apS as apS_, apSecond as apSecond_, } from "fp-ts/lib/Apply"; | ||
*/ | ||
export const flatMapIterable = (f) => { | ||
const aif = (a) => pipe(a, f, fromIterable); | ||
return flatMap(aif); | ||
export const flatMapIterable = (f) => (fa) => { | ||
const buffer = []; | ||
const fromBufferQueue = []; | ||
const nextAIFB = pipe(fa, getAsyncIteratorNextTask, TO.map(f)); | ||
let isNextAIFBDone = false; | ||
let waitingForValues = 0; // fight against race conditions | ||
async function toBuffer() { | ||
if (!isNextAIFBDone) { | ||
waitingForValues++; | ||
const maybeAIB = await nextAIFB(); | ||
waitingForValues--; | ||
if (O.isSome(maybeAIB)) { | ||
buffer.push(...maybeAIB.value); | ||
} | ||
else { | ||
isNextAIFBDone = true; | ||
} | ||
} | ||
if (!isNextAIFBDone && buffer.length === 0) { | ||
await toBuffer(); | ||
} | ||
dispatchFromBuffer(); | ||
} | ||
function dispatchFromBuffer() { | ||
if (buffer.length > 0) { | ||
const fromTasks = fromBufferQueue.splice(0, buffer.length); | ||
const values = buffer.splice(0, fromTasks.length); | ||
pipe(fromTasks, A.zip(values), A.map(([task, value]) => { | ||
task(O.some(value)); | ||
})); | ||
} | ||
if (waitingForValues > 0 || buffer.length > 0) { | ||
return; | ||
} | ||
const hopeless = fromBufferQueue.splice(0); | ||
hopeless.forEach((task) => { | ||
task(O.none); | ||
}); | ||
} | ||
function addToFromBufferQueue() { | ||
return new Promise((resolve) => fromBufferQueue.push(resolve)); | ||
} | ||
async function next() { | ||
if (buffer.length > 0) { | ||
return O.some(buffer.shift()); | ||
} | ||
const fromBuffer = addToFromBufferQueue(); | ||
await toBuffer(); | ||
return fromBuffer; | ||
} | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
const o = await next(); | ||
if (O.isSome(o)) { | ||
yield o.value; | ||
} | ||
}, | ||
}; | ||
}; | ||
@@ -378,0 +433,0 @@ /** |
@@ -276,6 +276,3 @@ import { apFirst as apFirst_, apS as apS_, apSecond as apSecond_, } from "fp-ts/lib/Apply"; | ||
*/ | ||
export const flatMapIterable = (f) => { | ||
const aief = (a) => pipe(a, f, fromIterable); | ||
return flatMap(aief); | ||
}; | ||
export const flatMapIterable = (f) => (ma) => pipe(ma, AI.flatMapIterable((e) => E.isLeft(e) ? I.of(e) : pipe(e.right, f, I.map((E.right))))); | ||
/** | ||
@@ -282,0 +279,0 @@ * @category sequencing |
@@ -244,2 +244,7 @@ import { apFirst as apFirst_, apS as apS_, apSecond as apSecond_, } from "fp-ts/lib/Apply"; | ||
*/ | ||
export const flatMapIterable = (f) => (ma) => pipe(ma, AI.flatMapIterable((o) => O.isNone(o) ? I.of(o) : pipe(o.value, f, I.map(O.some)))); | ||
/** | ||
* @category sequencing | ||
* @since 1.0.0 | ||
*/ | ||
export const flatMapEither = (f) => (fa) => ({ | ||
@@ -246,0 +251,0 @@ async *[Symbol.asyncIterator]() { |
@@ -286,3 +286,3 @@ /** | ||
*/ | ||
export declare const flatMapIterable: <E, A, B>(f: (a: A) => Iterable<B>) => ((ma: AsyncIterableEither<E, A>) => AsyncIterableEither<E, B>); | ||
export declare const flatMapIterable: <E, A, B>(f: (a: A) => Iterable<B>) => (ma: AsyncIterableEither<E, A>) => AsyncIterableEither<E, B>; | ||
/** | ||
@@ -289,0 +289,0 @@ * @category sequencing |
@@ -257,2 +257,7 @@ /** | ||
*/ | ||
export declare const flatMapIterable: <A, B>(f: (a: A) => Iterable<B>) => (ma: AsyncIterableOption<A>) => AsyncIterableOption<B>; | ||
/** | ||
* @category sequencing | ||
* @since 1.0.0 | ||
*/ | ||
export declare const flatMapEither: <E, A, B>(f: (a: A) => Either<E, B>) => (fa: AsyncIterableOption<A>) => AsyncIterableOption<B>; | ||
@@ -259,0 +264,0 @@ /** |
{ | ||
"name": "fp-ts-iterators", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"description": "Functional Iterable and AsyncIterable", | ||
@@ -5,0 +5,0 @@ "main": "build/cjs/index.js", |
237611
1.78%8531
1.43%