You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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|>add2# 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!
The text was updated successfully, but these errors were encountered:
GabrielRatener
changed the title
Proposal: Functional piping + OO chaining proposal
Proposal: Functional piping + OO chaining
Apr 25, 2020
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.
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!
The text was updated successfully, but these errors were encountered: