getify / Functional-Light-JS

Pragmatic, balanced FP in JavaScript. @FLJSBook on twitter.
http://FLJSBook.com
Other
16.6k stars 1.96k forks source link

Chapter 3 Adapting Arguments to Parameters #203

Closed andelkocvjetkovic closed 2 years ago

andelkocvjetkovic commented 2 years ago

Hello Getify, first I'd like to thank you for all the hard work on this book, it is a really great book. I create a demo where you can see that partial applied function in your snippet doesn't create a unary function rather n-arity function since the partial uses gather args in the parameter list.

https://codesandbox.io/s/frosty-shadow-fbwec?file=/src/index.js

Yes, I promise I've read the Contributions Guidelines (please feel free to remove this line -- if you leave this line here, I'm going to assume you didn't actually read it).

Screenshot 2021-12-12 at 17 18 39
getify commented 2 years ago

I think you may be confusing the notion of "arity" with "variadic", which are related but distinct concepts.

The underlying add(..) function, as adapted by the partial(..) utility, strictly reduces from being "binary" (handles 2 named parameters) to being a new function that is "unary" (handles only one parameter).

The chosen implementation of that general partial(..) utility does indeed allow additional arguments passed through, whereas the unary(..) utility presented earlier in this chapter would have restricted the allowable arguments to just one. But the notion that the function could accept more arguments is not really even related much to the specific partial(..) utility, because in reality, all functions in JS are "variadic". This means that no matter what's declared (named params), a call site can always choose to pass fewer, the same, or more arguments, and JS will never "prevent" that. The adapted function is variadic in that it would allow any additional arguments passed in to flow into the underlying add(..) function. But I claim that this doesn't make it not "unary" because, semantically, the add(..) function is still only naming 2 distinct parameters that it will do anything with, and once it's been adapted by partial(..), that arity is reduced to 1.

add(..) will ignore any extra arguments passed in, so the nature of how partial(..) will allow extra arguments doesn't affect the arity of that function (or its adapted counterpart).

andelkocvjetkovic commented 2 years ago

I get it, thanks 👍