Reputation: 35
It may be a very strange question, but I need to be sure if it really exists or not. I've got the recruitment task for a PHP developer position with the following task:
Make a function to sum two numbers a and b but it must be called as sum(a)(b)
I've never seen anything like that and can't find anything about functions like the above. The recruiter says it's not a typo; I'm confused.
Upvotes: 3
Views: 88
Reputation: 214949
This assignment expects you to understand partial application. Basically, your sum
function should return a function that accepts one argument and takes the other one from the surrounding closure:
// Javascript
function sum(a) { return function(b) { return a + b } }
sum(3)(4) // 7
You can do almost the same in php:
function sum($a) {
return function($b) use($a) {
return $a + $b;
};
}
but since php doesn't allow two calls in a row, you'll need a temporary variable:
$add3 = sum(3);
print $add3(4); // 7
This
sum(3)(4);
is not possible in php (as of 5.5) - parse error.
I'm pretty sure your interviewer actually means javascript, not php (note the lack of $
's in the assignment).
Upvotes: 10