Skip to content

Proposal: Functional piping + OO chaining #5316

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

Closed
GabrielRatener opened this issue Apr 25, 2020 · 2 comments
Closed

Proposal: Functional piping + OO chaining #5316

GabrielRatener opened this issue Apr 25, 2020 · 2 comments

Comments

@GabrielRatener
Copy link
Contributor

I am aware that chaining is quite a hot topic for CS, but I hope this proposal will improve significantly on previous piping proposals like #4144 that didn't make the cut.

I'm proposing to have an OO chaining operator |. that performs a chain of operations on a given object, while always returning the original object, much like Dart does.

This is in addition to supporting the |> piping operator that exists in languages like LiveScript, F#, and Elixir.

I'm not sure if |> should append or prepend arguments to function calls, but the general idea is the same.

Example

For an illustration of what this could look like, check out this code.

users = [
  { user: 'barney',  active: false, age: 37}
  { user: 'fred', active: true,  age: 21}
  { user: 'pebbles', active: true,  age: 19}
]

add = (n, value) => n + value

arrayOfNumbers = users
  |.pop() # pop value from users but return `users`
  |[1].age = 1 # also support computed props and assignment in chains
  |.reverse() # oo chain expressions still resolves to `users` array!
  |> .map ({age}) => age # end oo chain & return value, no parens needed!
  |> ['reduce'] (sum, age) => age + sum # do same with newly created array
  |> add 2 # append argument and return call value
  |> ([1 ,2, 4]).concat # pipe to end of new array

Context

This solves two major issues, call over-nesting and tediously doing repeated operations on the same object.

The |> . syntax is optional, and can be replaced with just . but this requires adding parens to method calls, so I was thinking |> . could have lower precedence and make for nicer looking code.

Thoughts?

I can create a PR for this if we come to a consensus!

@GabrielRatener GabrielRatener changed the title Proposal: Functional piping + OO chaining proposal Proposal: Functional piping + OO chaining Apr 25, 2020
@FelipeSharkao
Copy link

This is a very cool feature that I'd love to see in CoffeeScript, but it was already reject once, I didn't get why it wouldn't now.

@GeoffreyBooth
Copy link
Collaborator

There’s a proposal for adding this in JavaScript: https://github.com/tc39/proposal-pipeline-operator

If that reaches Stage 4, we should support it; but our version would just output to the equivalent syntax in JavaScript.

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

No branches or pull requests

3 participants