Reputation: 449
How can i convert following function to arrow function? I am using currying here
function mergeString(str){
return function(str1){
if(str1){
return mergeString(str + ' ' + str1);
}
else
{
return str;
}
}
}
Upvotes: 0
Views: 378
Reputation: 138267
Actuall this is a good usecase for rest parameters:
const mergeStrings = (...strings) => strings.join(" ");
Usable as:
mergeString(
"one",
"two",
"three"
)
Upvotes: 1
Reputation: 386570
You could chain the function heads and then the function body for all.
const mergeString = str => str1 => str1 ? mergeString(str + ' ' + str1) : str;
console.log(mergeString('a')());
console.log(mergeString('a')('b')('c')());
console.log(mergeString('this')('should')('work')('as')('well')());
Upvotes: 4