Skip to content
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

Merged
merged 1 commit into from
Dec 24, 2021
Merged

feat: add scan function #104

merged 1 commit into from
Dec 24, 2021

Conversation

ppeeou
Copy link
Member

@ppeeou ppeeou commented Dec 21, 2021

Fixes #87

@ppeeou ppeeou force-pushed the feature/scan branch 4 times, most recently from 481c1c6 to 2836b14 Compare December 24, 2021 03:32
src/Lazy/scan.ts Outdated
Comment on lines 53 to 74
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;
},
};
}
Copy link
Collaborator

@shine1594 shine1594 Dec 24, 2021

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.

Suggested change
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;
},
};
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh.. you are right 👍

Copy link
Collaborator

@shine1594 shine1594 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@shine1594 shine1594 merged commit ce3eea7 into main Dec 24, 2021
@shine1594 shine1594 deleted the feature/scan branch December 24, 2021 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add scan function
2 participants