How to write this snippet more functionality by avoiding repetitions of function creation like this in JavaScript? Just to give a context, I am trying find if the movement from current value to final value has already been achieved.. deltaValue is positive if the movement is towards a higher value and negative if its towards a lower value.
if (deltaValue > 0) {
maxPossibleValue = function(current, final) {
return current > final ? final : current;
}
} else {
maxPossibleValue = function(current, final) {
return current < final ? final : current;
}
}
Assuming there existed < and > as functions, lt and gt in JavaScript, I could have just evaluated this with a single function where predicate is lt and gt dealing with higher order functions. However there are no such functions natively in JS, so is the above method the only way?
maxPossibleValue = function(predicate) {
return function(c, f) {
return predicate(c, f) ? c : f }
}
This can be thought as just templating the required predicate function and returning a new function. I have seen such patterns in Scheme.
maxPossibleValue) and try to be as generic as you can (like in the inner function) :)