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

Pipe as lazy function #218

Open
darky opened this issue Jun 29, 2023 · 4 comments
Open

Pipe as lazy function #218

darky opened this issue Jun 29, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@darky
Copy link
Contributor

darky commented Jun 29, 2023

Suggestion

⭐ Suggestion

pipe in FxTS works in eager fashion.

pipe(1, (n) => n + 1) // receiving of 2 immediately

Would be great to have something like pipeLazy, which works in lazy fashion.

const fn = pipeLazy((n: number) => n, n => n + 1) // Function<(n: number) => number>
fn(1) // 2

💻 Use Cases

  • Huge amount of FP libraries, that I know, using lazy pipe, not eager
  • Lazy pipes more JIT friendly and faster. They can be initialized once on application bootstrap and will works with maximum speed. Eager pipes will be generated every time and spend additional CPU resources.
  • Eager pipes increasing GC work much more than Lazy pipes, because lazy pipes can be initialized on application bootstrap and don't recreate pipe functions every time
  • Lazy pipes can be easily composed with each other.
  • Lazy pipes reduces pipe hell. Lazy pipes propagates to creating many little pipes. Eager pipes propagates to creating one big pipe.
@ppeeou
Copy link
Member

ppeeou commented Jul 10, 2023

Good idea. I already checked your library rocket.pipes 👍

Could you please create a PR??

@ppeeou ppeeou added the enhancement New feature or request label Jul 10, 2023
@darky
Copy link
Contributor Author

darky commented Jul 10, 2023

Good idea. I already checked your library rocket.pipes 👍

Could you please create a PR??

Thanks, yep 👌
But need to wait some time, when I will get free time 🕐

darky added a commit to darky/FxTS that referenced this issue Jul 19, 2023
darky added a commit to darky/FxTS that referenced this issue Jul 20, 2023
ppeeou pushed a commit that referenced this issue Aug 20, 2023
* feat: pipeLazy (#218)

* feat: pipeLazy types test (#218)

* feat: pipeLazy, fix type inference for Promise arg
@ppeeou
Copy link
Member

ppeeou commented Aug 20, 2023

@darky There is something I would like to discuss.

Currently, pipeLazy supports code type 1, but code type 2 is not supported. Existing functions used in FxTS are designed to receive and determine whether it is Iterable or AsyncIterable. In the case of shape 2, it is not included in the specified signature because we do not know what type will come from the head function.

  1. pass
const res1 = pipeLazy(
    (a: string) => head(a),
);
  1. fail
const res2 = pipeLazy(
   head,
);

So the initial design was intended for users to type something like the following to use the pipeLazy function.

const res3 = (a:string) => pipe(a, head)

Is there any way to support type 2?🧐

@darky
Copy link
Contributor Author

darky commented Aug 21, 2023

@ppeeou

Sorry, but I don't think that case 2 is possible.
Due to my experience with rocket-pipes, ramda pipe, lodash flow first function in pipe should be typed in explicit way usually
Seems it's limitation of TypeScript type system
You desire more fits with ML based languages like Ocaml, ReScript, Flow where type of function parameters can inferred from caller side

P. S. Maybe modern TypeScript allows to implement case 2 via some secret trick, but I don't know it. Will glad to accept this insight. Suggestions welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants