loading...

Functional pipe

James Robb on March 04, 2020

When we are building applications, we generally end up chaining the result of one function into another function, for example the following is not ... [Read Full]
markdown guide
 

Tiny note; the pipe function behavior is not entirely as described here:

"Parameter 1 must be either of type Function[] or if multiple parameters are provided then each one should be of type Function but this requirement has not been met.");

With the current implementation, parameter 2 (or 3, or 4, ...) could also be a Function[].

For example, the following is valid, while it shouldn't be according to the above error:

pipe(
        () => "first function",
        [
          () => "second function, inside array",
          () => "third function, inside array"
        ]
      )

I had some fun setting up my first CodeSandbox to demo this with 1 additional test (open the sandbox page separately, because the dev.to embed doesn't allow the tests to be shown...)

The reason is that the concat trick const parameters = fns.reduce((output, value) => output.concat(value), []); will flatten just any value it gets, not only the first one.

I'd say this behavior is desirable. It could be useful! But it's not as described.

 

I updated the check because actually the jsdoc expects just an array of functions to be in the fns variable and so I updated the code accordingly, open to feedback but I think this is cleaner either way. I also added your extra test with some modifications to the article.

 

Looks good to me! Now it's consistent.
The spread operator can also be used when calling pipe, like pipe(...[f1, f2], ...[f3, f4], f5), so if you know for sure there will be an array (or arrays) of functions to input, that's still possible this way.

Exactly. Appreciate the input though! 👍

Exactly which still matches the initial intention anyway and keeps things more consistent too.

 

pipe(()=>1, ()=>2, "nope");

Parameter 1 must be of type Function[]

The phrasing suggests that the first argument must be an array of functions, which is not so. Should be something like: "All arguments must be functions"

Side note 1. Typical error reporting is typically phrased about arguments, not parameters, such as in TypeError

an operand or argument passed to a function is incompatible with the type expected by that operator or function; or

Side note 2. Please consider using TypeError which reflects the situations more precisely.

 

I am aware of the common TypeError usage and I am also aware of the phrasing being slightly off but it is being used as a spread parameter and would always be an array when the parameters are provided which I why I ended up choosing this wording but I will update the text to be a little clear never the less. Thanks for the comment!

 

Your wording causes me to suspect you are not clear on difference between parameters and arguments.

would always be an array when the parameters are provided

Arguments will be provided.

See stackoverflow.com/questions/156767...

Parameter 1 must be either of type Function[] or if multiple parameters are provided then each one should be of type Function but this requirement has not been met.

Again, "All arguments must be functions" or something of that sort would be more correct.

No since the function does accept an array of functions or all arguments can be functions. The wording is correct thusly.

Pass a Function[] and then all parameters as individual ‘Functions` and it works both ways, heed the wording.

Furthermore parameters and arguments are the same thing in English and have been used interchangeably between people in every role I’ve ever had, thus such elitism over terminology helps no one. The point of the article is to be educational and introduce people to a new concept and it does that. The tests are there, the implementation works exactly as it should and the terminology is accurate. Such pedantry is unwelcome and wholly unnecessary from your side in this case 🤷‍♂️

Pass a Function[] and then all parameters as individual ‘Functions` and it works both ways, heed the wording.

Correct! My mistake. First element of fns can be array of functions. You see? Easy. "My mistake". No shame in this. Everybody does mistakes. I did miss the functionality of const parameters = fns.reduce((output, value) => output.concat(value), []); .

Furthermore parameters and arguments are the same thing in English and have been used interchangeably between people in every role I’ve ever had

... and as sad as it is, all of them were wrong. How much evidence you need to be convinced that this is incorrect?

Instead of correcting your colleagues, you are ignoring opportunity to learn. Stop for a minute and think about this.

elitism over terminology helps no one

Being correct and using terminology consistent with MDN in an article that people are supposed to learn from does help ... a lot.

Being incorrect and defending that definitely does not help.

The point of the article is to be educational and introduce people to a new concept and it does that.

Doesn't mean it should ignore terminology.

The tests are there,

Not covering fns[0] being an array, which is not a very good example to learn from or reference in an argument.

terminology is accurate

Not true. How can I convince you otherwise?

Such pedantry is unwelcome and wholly unnecessary from your side in this case

My aim is for programmers in general to be more professional. I think it will be a better world. Articles that people are supposed to learn from are the place to use correct terminology.

Learn from Kabir: "Uptil now, I thought that arguments and parameters were the same. But I just looked them up on the web again and understand the difference now. Shall correct it."

Your argument about arguments vs parameters is whatever. I know the difference, I learned it years ago as did most developers but the difference between you and I is that I accept language evolves and is used differently to how it is “standardised” every day. People are not technical books nor are they pedants about it like you seem to be. If people want to say one thing or another, so long as it is understood clearly by all parties, I couldn’t care less what terms they use and neither do most reasonable people.

“ Being correct and using terminology consistent with MDN” - sure thing, tell you what, I’ll just copy paste everything from there next time 😂

“Not covering fns[0] being an array, which is not a very good example to learn from or reference in an argument.“ - Firstly the code is covered on all branches and lines so I don’t care plus even if it wasn’t at 100% I’d be fine with 80%+, even in TDD the aim isn’t 100% coverage, you should know that being such a proficient reader of technical documents. Secondly I could add an example for illustration but I chose not to although I may change that stance in a future update if I do one.

“My aim is for programmers in general to be more professional. I think it will be a better world. Articles that people are supposed to learn from are the place to use correct terminology.“ - correct to you coming from a formal and standardised world but that’s not the world we live in and to be accessible to the most people possible, simple and understandable language used on the ground will always be preferable to me.

I’m finished with this conversation, I appreciate the comment but I’m finished talking about such pedantic things.

 

very good! I've been doing similar techniques for a long time, just putting the functions in an array. One technique I do sometimes is to do something similar to a. reduce and the output of a previous function is input of a new one. I'm glad to see developers with this same line of thinking.

 

Glad you liked the article and nice to hear that you’ve been trying this style for some time, I find it super helpful in many situations also! Thanks for stopping by and leaving a comment 👏

code of conduct - report abuse