Skip to content

Instantly share code, notes, and snippets.

@joedski
Created July 13, 2017 23:52
Show Gist options
  • Save joedski/e137317b7a90b0c989dc637e288098f2 to your computer and use it in GitHub Desktop.
Save joedski/e137317b7a90b0c989dc637e288098f2 to your computer and use it in GitHub Desktop.
Multiparam Memoization: createApplier
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