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: implement reduceLazy (#239) #245

Merged
merged 3 commits into from
Feb 18, 2024
Merged

Conversation

Phryxia
Copy link
Contributor

@Phryxia Phryxia commented Feb 17, 2024

Fixes #239

  • Extract type of function argument in reduce as SyncReducer and AsyncReducer
    • This is because they are both used in reduce and reduceLazy
    • It's more concise than messy raw arrowy type
  • Implement reduceLazy
    • At first I thought the implementation of reduce covers lazy cases 100%, but it wasn't
    • To resolve issue just relay reduce with rich dedicated types
  • Add type test and spec test for reduceLazy
    • Type tests were desigend using exhaustive cartessian thinking of following three dimension:
      • homogeneous vs heterogeneous
      • SyncReducer vs AsyncReducer
      • Iterable vs AsyncIterable
    • Spec tests were almost identical to reduce
      • Test for promise rejection was omitted since it is responsible for reduce

src/Lazy/reduceLazy.ts Show resolved Hide resolved
Comment on lines +57 to +60
reduce(f, iterable as any);
}
return (iterable: Iterable<unknown> | AsyncIterable<unknown>) =>
reduce(f, seed, iterable as any);
Copy link
Contributor Author

@Phryxia Phryxia Feb 17, 2024

Choose a reason for hiding this comment

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

I don't have any idea to overcome this, and reduce also uses as any so I just simply suppress them.
Actually it's common to use anyscript(😆) for implementation if type signatures are sufficiently provided.

) => IterableInfer<T> | Promise<IterableInfer<T>>,
): (iterable: T) => ReturnValueType<T, IterableInfer<T>>;
f: AsyncReducer<IterableInfer<T>, IterableInfer<T>>,
): (iterable: T) => ReturnValueType<T>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ReturnValueType<T> is equivalent to ReturnValueType<T, IterableInfer<T>>. See ReturnValueType.ts.

@Phryxia
Copy link
Contributor Author

Phryxia commented Feb 17, 2024

Note that test succeed for reduce and reduceLazy. CI failed because of #93

* {@link https://github.com/marpple/FxTS/issues/239 | #related issue}
*
* For backward compatibility, `reduce` can support partial lazy form.
* You may want to use `reduceLazy` to use `seed`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* You may want to use `reduceLazy` to use `seed`.
* You may want to use {@link https://fxts.dev/docs/reduceLazy | reduceLazy} to use `seed`.

expect(reduce([])).toEqual("seed");
});

it("should be occured error when the given `iterable` is an empty array and initial value is absent", () => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
it("should be occured error when the given `iterable` is an empty array and initial value is absent", () => {
it("should be occurred error when the given `iterable` is an empty array and initial value is absent", () => {

@ppeeou ppeeou changed the base branch from main to feat/reduceLazy February 18, 2024 11:21
@ppeeou ppeeou changed the base branch from feat/reduceLazy to main February 18, 2024 11:21
@ppeeou ppeeou changed the base branch from main to feat/reduceLazy February 18, 2024 11:22
@ppeeou ppeeou merged commit 3ff213a into marpple:feat/reduceLazy Feb 18, 2024
1 check passed
@ppeeou
Copy link
Member

ppeeou commented Feb 18, 2024

LGTM👍

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.

Missing type of reduce with initial seed in declarative form
2 participants