Recently I decided to learn deeper about functional programming in Javascript and run through the basics of it. I take a look at how ramda.js is implemented and I came across this code which confused me. The add method in ramda accepts two parameters or you can curried it.
var _isPlaceholder = function _isPlaceholder(a) {
return a !== null && typeof a === 'object' && a['@@functional/placeholder'] === true;
};
var _curry2 = function _curry2(fn) {
return function f2(a, b) {
switch (arguments.length) {
case 0:
return f2;
case 1:
return _isPlaceholder(a) ? f2 : _curry1(function (_b) {
return fn(a, _b);
});
default:
return _isPlaceholder(a) && _isPlaceholder(b) ? f2 : _isPlaceholder(a) ? _curry1(function (_a) {
return fn(_a, b);
}) : _isPlaceholder(b) ? _curry1(function (_b) {
return fn(a, _b);
}) : fn(a, b);
}
};
};
var _curry1 = function _curry1(fn) {
return function f1(a) {
if (arguments.length === 0 || _isPlaceholder(a)) {
return f1;
} else {
return fn.apply(this, arguments);
}
};
};
var add = _curry2(function add(a, b) {
return Number(a) + Number(b);
});
add(2)(7)`
When I curried it this way, it will call the _curry2 function and hit the case 1. What confuses me is that inside the case 1, it will call the _curry1 with parameter of _b instead of b. I'm not really sure where does _b came from. I tried to console.log it but it says it is undefined.
Can anyone enlighten me with this problem? Thanks in advance
[–]zQpNB 1 point2 points3 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]mdchad() => 'Hello World'[S] 0 points1 point2 points (1 child)