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

Proper compose operator #1112

Open
bbrk24 opened this issue Mar 21, 2024 · 1 comment
Open

Proper compose operator #1112

bbrk24 opened this issue Mar 21, 2024 · 1 comment
Labels
proposal Proposal or discussion about a significant language feature

Comments

@bbrk24
Copy link
Contributor

bbrk24 commented Mar 21, 2024

I've implemented this operator before:

operator o looser(??) <T < ???[], U, V>(lhs: (arg: U) => V, rhs: (...args: T) => U): (...args: T) => V
  (...args) => lhs rhs ...args

However, implementing this in userspace has a few drawbacks:

  • Because this is an actual JS function at runtime, there is a minor performance and code size hit. (Somehow, terser is smart enough to inline this, so in practice it doesn't matter.)
  • Unlike with |>, you can lose the this value of the arguments. So instead of foo.bar o baz.qux, you have to say foo@bar o baz@qux.

So, I propose that a similar operator be introduced at the language level into Civet. (It doesn't have to be o -- I just called it that because the mathematical symbol for composition is a circle.) foo o bar becomes (...args) => foo(bar(...args)), with the same precedence as |>. When chained, the arguments are only spread once: foo o bar o baz becomes (...args) => foo(bar(baz(...args))).

This is left-associative with |> and ||>, so foo o bar |> baz is baz((...args) => foo(bar(...args))), and foo |> bar o baz would be (...args) => bar(foo)(baz(...args)).

@edemaine edemaine added the proposal Proposal or discussion about a significant language feature label Mar 21, 2024
@STRd6
Copy link
Contributor

STRd6 commented Mar 23, 2024

Related to #1081

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Proposal or discussion about a significant language feature
Projects
None yet
Development

No branches or pull requests

3 participants