Last active
June 30, 2024 14:22
-
-
Save trikitrok/e79a9e73f153143f6369 to your computer and use it in GitHub Desktop.
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 makeSubstitute = function (substitutionDescriptions) { | |
var substituteOne = (function () { | |
var substitutions = [], i; | |
function makeSubstitution (pred, replacement) { | |
return function substituteNumber(acc, number) { | |
if ( pred(number) ) { | |
acc += replacement; | |
} | |
return acc; | |
}; | |
} | |
for(i = 0; i < substitutionDescriptions.length; i++) { | |
substitutions.push(makeSubstitution( | |
substitutionDescriptions[i].pred, | |
substitutionDescriptions[i].replacement)); | |
} | |
return function (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 function (numbers) { | |
return numbers.map(substituteOne).join(" "); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment