Currying: Handling One Argument at a Time
What would happen if we called the avg function but only gave it one argument? Surely we’d get some sort of error:
| let avg = (a: float, b: float) : float => { |
| (a +. b) /. 2.0; |
| }; |
| |
| Js.log2("Average of 3 and 4", avg(3.0, 4.0)); |
| Js.log2("Average of 3?!", avg(3.0)); |
| code/functions> bsb -make-world |
| ninja: Entering directory `lib/bs' |
| [3/3] Building src/Currying.mlast.d |
| [1/1] Building src/Currying-Simplefunctions.cmj |
| code/functions> node src/Currying.bs.js |
| Average of 3 and 4 3.5 |
| Average of 3?! function (param) { |
| return avg(3.0, param); |
| } |
It compiles without errors, and when we run it, we get a function as output ...
Get Web Development with ReasonML now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.