-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add scan
function
#104
Conversation
481c1c6
to
2836b14
Compare
src/Lazy/scan.ts
Outdated
function asyncWithoutSeed<A, B>( | ||
f: (a: B, b: A) => B, | ||
iterable: AsyncIterable<A>, | ||
): AsyncIterableIterator<B> { | ||
let finished = false; | ||
let _iterator: AsyncIterator<B>; | ||
let _iterable: AsyncIterable<A>; | ||
let prevItem = Promise.resolve(); | ||
const settlementQueue: [Resolve<B>, Reject][] = [] as unknown as [ | ||
Resolve<B>, | ||
Reject, | ||
][]; | ||
|
||
const pullItem = async (_concurrent: any) => { | ||
if (_iterator === undefined) { | ||
_iterable = isConcurrent(_concurrent) | ||
? concurrent(_concurrent.length, iterable) | ||
: iterable; | ||
|
||
const iterator = _iterable[Symbol.asyncIterator](); | ||
const { done, value } = await iterator.next(); | ||
|
||
if (done) { | ||
return { | ||
done: true, | ||
value: undefined, | ||
} as IteratorReturnResult<undefined>; | ||
} | ||
|
||
_iterator = asyncSequential(f, value, { | ||
[Symbol.asyncIterator]() { | ||
return iterator; | ||
}, | ||
}); | ||
} | ||
|
||
return _iterator.next(_concurrent); | ||
}; | ||
|
||
return { | ||
async next(_concurrent: any) { | ||
if (_concurrent === undefined) { | ||
return pullItem(_concurrent); | ||
} | ||
|
||
if (finished) { | ||
return { done: true, value: undefined }; | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
settlementQueue.push([resolve, reject]); | ||
|
||
prevItem = prevItem | ||
.then(() => pullItem(_concurrent)) | ||
.then(({ done, value }) => { | ||
if (done) { | ||
while (settlementQueue.length > 0) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const [resolve] = settlementQueue.shift()!; | ||
resolve({ done: true, value: undefined }); | ||
} | ||
return void (finished = true); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const [resolve] = settlementQueue.shift()!; | ||
resolve({ done: false, value: value }); | ||
}) | ||
.catch((reason) => { | ||
finished = true; | ||
while (settlementQueue.length > 0) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const [, reject] = settlementQueue.shift()!; | ||
reject(reason); | ||
} | ||
}); | ||
}); | ||
}, | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code also seems to satisfy all test cases.
function asyncWithoutSeed<A, B>( | |
f: (a: B, b: A) => B, | |
iterable: AsyncIterable<A>, | |
): AsyncIterableIterator<B> { | |
let finished = false; | |
let _iterator: AsyncIterator<B>; | |
let _iterable: AsyncIterable<A>; | |
let prevItem = Promise.resolve(); | |
const settlementQueue: [Resolve<B>, Reject][] = [] as unknown as [ | |
Resolve<B>, | |
Reject, | |
][]; | |
const pullItem = async (_concurrent: any) => { | |
if (_iterator === undefined) { | |
_iterable = isConcurrent(_concurrent) | |
? concurrent(_concurrent.length, iterable) | |
: iterable; | |
const iterator = _iterable[Symbol.asyncIterator](); | |
const { done, value } = await iterator.next(); | |
if (done) { | |
return { | |
done: true, | |
value: undefined, | |
} as IteratorReturnResult<undefined>; | |
} | |
_iterator = asyncSequential(f, value, { | |
[Symbol.asyncIterator]() { | |
return iterator; | |
}, | |
}); | |
} | |
return _iterator.next(_concurrent); | |
}; | |
return { | |
async next(_concurrent: any) { | |
if (_concurrent === undefined) { | |
return pullItem(_concurrent); | |
} | |
if (finished) { | |
return { done: true, value: undefined }; | |
} | |
return new Promise((resolve, reject) => { | |
settlementQueue.push([resolve, reject]); | |
prevItem = prevItem | |
.then(() => pullItem(_concurrent)) | |
.then(({ done, value }) => { | |
if (done) { | |
while (settlementQueue.length > 0) { | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
const [resolve] = settlementQueue.shift()!; | |
resolve({ done: true, value: undefined }); | |
} | |
return void (finished = true); | |
} | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
const [resolve] = settlementQueue.shift()!; | |
resolve({ done: false, value: value }); | |
}) | |
.catch((reason) => { | |
finished = true; | |
while (settlementQueue.length > 0) { | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
const [, reject] = settlementQueue.shift()!; | |
reject(reason); | |
} | |
}); | |
}); | |
}, | |
[Symbol.asyncIterator]() { | |
return this; | |
}, | |
}; | |
} | |
function asyncWithoutSeed<A, B>( | |
f: (a: B, b: A) => B, | |
iterable: AsyncIterable<A>, | |
): AsyncIterableIterator<B> { | |
let _iterator: AsyncIterator<B>; | |
return { | |
async next(_concurrent: any) { | |
if (_iterator === undefined) { | |
if (isConcurrent(_concurrent)) { | |
const _iterable = concurrent(_concurrent.length, iterable); | |
_iterator = asyncSequential(f, head(_iterable) as any, _iterable); | |
} else { | |
_iterator = asyncSequential(f, head(iterable) as any, iterable); | |
} | |
} | |
return _iterator.next(_concurrent); | |
}, | |
[Symbol.asyncIterator]() { | |
return this; | |
}, | |
}; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh.. you are right 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Fixes #87