Created
July 13, 2017 23:52
-
-
Save joedski/e137317b7a90b0c989dc637e288098f2 to your computer and use it in GitHub Desktop.
Multiparam Memoization: createApplier
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
function createApplier(calculator, memoizers, totalValueCount, currValues) { | |
const memoize = memoizers[currValues.length]; | |
function applyNonFinalValue(value) { | |
const nextValues = [...currValues, value]; | |
return createApplier(calculator, memoizers, totalValueCount, nextValues); | |
} | |
function applyFinalValue(value) { | |
return calculator(...currValues, value); | |
} | |
const applyValue = ( | |
// If the next one applies the final value, that is it would bring values.length | |
// up to totalValueCount, use applyFinalValue. Otherwise, use | |
// applyNonFinalValue. | |
currValues.length >= totalValueCount - 1 ? applyFinalValue : applyNonFinalValue | |
); | |
return memoize(applyValue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment