Created
February 6, 2014 07:36
Refactored substitute using Array.map and Array.join
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var makeSubstitution = function (divisor, substitute) { | |
return function substituteNumberBy(acc, number) { | |
if (number % divisor == 0) { | |
acc += substitute; | |
} | |
return acc; | |
}; | |
}; | |
var substitute = function (numbers, substitutions) { | |
function substituteOne (number) { | |
var res = (function f(acc, remainingSubstitutions) { | |
if (remainingSubstitutions.length == 0) { | |
return acc; | |
} else { | |
acc = remainingSubstitutions[0](acc, number); | |
return f(acc, remainingSubstitutions.slice(1)); | |
} | |
}("", substitutions)); | |
if (res != "") | |
return res; | |
return String(number); | |
} | |
return numbers.map(substituteOne).join(" "); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment