A variadic Curry function is a more sophisticated Curry function that is able to take variable number of arguments per call.
In my mind your example is simply the more general concept of partial application.
"The ability to apply only some of a function’s arguments is called partial application."
[Haskell Programming from First Principles, Chapter 5: Types - Partial Application; p.129]
A variadic curry typically requires a superpositioned result, i.e.:
a function capable of accepting an additional argument AND
Currying solves a problem that JavaScript doesn't have:
Each lambda can only bind one parameter and can only accept one argument. Functions that require multiple arguments have multiple, nested heads. When you apply it once and eliminate the first (leftmost) head, the next one is applied and so on. This formulation was originally discovered by Moses Schönfinkel in the 1920s but was later rediscovered and named after Haskell Curry and is commonly called currying.
How exactly does Haskell implement the said superposition?
I don't know. If we are talking about the same thing, Haskell will automatically perform a partial application if you provide more than one but less than all of the required arguments.
In principle superposition just implies that two results occupy the same space, i.e. a function and a primitive value. In reality that is smoke and mirrors.
sum simply returns an object that is callable (i.e. a function object) but also makes its primitive value accessible via Object.prototype.valueOf().
Does Function.prototype.bind do superposition in JS?
No, it simply creates a bound function (which is not possible with arrow functions).
Just be careful because the first this binding wins:
functionsum(a,b,c){returnthis.value+a+b+c;}constone={value:1,};constten={value:10,};sum2=sum.bind(one,2);console.log(sum2(3,4));// 10sum23=sum2.bind(ten,3);console.log(sum23(4));// 10 not 19!
To make this work, variadic functions must be declared within the context of another non-nullary function. When a variadic function is applied, we evaluate its body with the proper substitution as though it were a regular unary function. However, we also construct a new version of the variadic function with an updated environment. In this environment, the value bound during application of the parent function is replaced with the result of the variadic application. This is kept around in the variadic closure for subsequent applications (if any).
This is wealth of info. It talks about an enclosing context, updated environment.
I suppose in your sum, you're creating the said "equally probable environment" with recursion ?
In my mind your example is simply the more general concept of partial application.
"The ability to apply only some of a function’s arguments is called partial application."
[Haskell Programming from First Principles, Chapter 5: Types - Partial Application; p.129]
A variadic curry typically requires a superpositioned result, i.e.:
λvc: Variadic Auto-Curried Lambda Calculus
Example:
Currying solves a problem that JavaScript doesn't have:
Haskell Programming from First Principles
Currying versus partial application (with JavaScript code):
How exactly does Haskell implement the said superposition? Does
Function.prototype.bind
do superposition in JS?I think I just got another reason to hate Physics.
I don't know. If we are talking about the same thing, Haskell will automatically perform a partial application if you provide more than one but less than all of the required arguments.
In principle superposition just implies that two results occupy the same space, i.e. a function and a primitive value. In reality that is smoke and mirrors.
sum
simply returns an object that is callable (i.e. a function object) but also makes its primitive value accessible viaObject.prototype.valueOf()
.No, it simply creates a bound function (which is not possible with arrow functions).
Just be careful because the first
this
binding wins:Nice, thanks!
This is wealth of info. It talks about an enclosing context, updated environment.
I suppose in your
sum
, you're creating the said "equally probable environment" with recursion ?I've seen examples using recursion both with and without
Function.prototype.bind
, but kind of wondering how superposition is being dealt with ?Not sure what that means.
It's not really recursion in the sense of "body recursion" but more in the sense of trampolining.
It's all just conceptual.
Basically we are breaking the expected general contract of a function object by monkey patching the instance to shadow the expected implementations of
Object.prototype.valueOf()
andFunction.object.toString()
available on prototype chain.A more honest representation would be: