suggestion: more readable function chaining for collections
(pipe, chain, data-last)
#4386
Labels
feedback welcome
We want community's feedback on this issue or PR
Is your feature request related to a problem? Please describe
I've recently been trying to use the
collections
functions and realized that composing multiple calls ends up creating code that is hard to read and maintain due to nested function calls.It seems these functions were inspired by Kotlin, but the big difference is Kotlin has extension functions so you can chain calls using a fluent api.
Simple example, that just gets more complicated with more function calls:
I see there have been discussions in the past around this, but it seems like there are no great solutions afaict. The proposed JS pipeline operator (currently stage 2) would potentially solve this, but it seems like a long way off or may never happen. The other options are to try and combine a functional user land library with
@std/collections
or not use@std
at all for this sort of thing.Describe the solution you'd like
I'm not suggesting that
std
needs to turn into a full functional like library likeramda
, etc. but it would be nice to have something that avoids needing to rely on nested function chaining or reach for another library for common cases.Supporting point-free piping like below would be ideal, but I realize this is both possibly contentious and unlikely without some drastic API changes.
That said, I think there could be some potential options (one or more) for creating some incremental building blocks that would allow moving in this direction.
1. Introduce a
pipe
functionInspired by remeda's pipe, this would allow for:
2. Introduce
data-last
function signaturesremeda has a concept of
data-first
&data-last
functions. This allows you to mix and match FP styles with more traditional usage. This would be a way to make the syntax ofpipe
look like:3. Introduce a
chain
functionThis would be similar to lodash's
chain
that would wrap the data object and allow for chaining of calls. This would actually provide a similar experience to Kotlin & C# extension functions. Although since it's not a native JS feature, I feel this would only work with functions registered with the wrapper making it less extensible (or require boilerplate to do so).4. Introduce a function that can invert the positional arguments and curry
Remeda has a purry function that creates a function that supports both
data-first
&data-last
signatures and allows for currying of args to support the previous example. The problem is in order to make this properly types you need to create a wrapper function.Describe alternatives you've considered
I'm sure there are alternatives, so I'm eager to here other ideas 😄
The text was updated successfully, but these errors were encountered: