Last active
June 30, 2024 14:32
-
-
Save trikitrok/0e2d68e571abe5d46be5 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 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 (function f (acc, remainingNumbers) { | |
if (remainingNumbers.length == 0) { | |
return acc; | |
} else { | |
next = substituteOne(remainingNumbers[0]); | |
if (remainingNumbers.length == 1) { | |
return acc + next; | |
} else { | |
return f (acc + next + " ", remainingNumbers.slice(1)); | |
} | |
} | |
})("", numbers); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment