by epidemian · 15y ago · 0 comments
The example on Types is not compiling as it is. The last line should be something like:
console.log (getName {firstName: "John", lastName: "Doe"})No partial application allowed here, so what is the point? =\
let add a b = a + b
let add2 = add 2
Does not work. let fact a =
if a == 0 then
1
else
(fact (a-1)) * a
Does not support recursion like this either. The "functional" description brought my expectations too high I think.It compiles to very lean Javascript though, so that's a big plus.
let fact a =
if a == 0 then
1
else
(fact (a - 1)) * a
console.log (fact 5)
Now, it's true that the Haskell syntax does not make that much sense if there is no partial application. I think that the variadic-arguments-are-always-allowed nature of JavaScript is not very compatible with partial application though.It could be implemented by compiling Roy functions to chains of 1-arg functions, but the amount of recursion and funcalling would probably be expensive (JS is not very good at these).
A SmartEnoughCompiler® you could also see partial application points and craft a partial application right there:
let add x y = x + y
console.log (add 1 32)
let add2 = add 2
console.log (add2 5)
var add = function (x, y) { return x + y; };
console.log(add(1, 32));
var add2 = function (y) { return add(2, y); };
console.log(add2(5));
As long as the number of arguments can be understood at compile time (which it can), it's not that hard: if the number of arguments provided make the function's arity reach 0 you partially apply it (and decrease the arity of the result), otherwise you fully apply it.I'll have a go and see how well it works. Thanks!
That should be fixed.