2

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.

5
  • 1
    Are you trying to do something like this? maxPosibleValue(function(c,f) { return c > f; })(c,f); Commented May 12, 2016 at 8:42
  • Does this achieve the goal? Though subtely I can use a logic to flip the variable and achieve what I want. Commented May 12, 2016 at 8:49
  • 1
    The inline function that I've wroted is one of the approaches that you seems to need (when deltaValue is greater than 0). Obviously you can change the logic of the function each time to get the behaviour that you want. Commented May 12, 2016 at 8:53
  • Keep in mind I don't want to repeat functions :) Commented May 12, 2016 at 9:00
  • 1
    Then store each function in variables (like maxPossibleValue) and try to be as generic as you can (like in the inner function) :) Commented May 12, 2016 at 9:02

2 Answers 2

2

@Nina's answer points you in the right direction but Math.max and Math.min are not predicates. And the answer is overly complicated.

I see no reason why the answer should be harder to follow than this

function calculateValue(delta, final, current) {
  return (delta > 0 ? Math.min : Math.max)(final, current);
}

calculateValue(-1, 5, 10); // 10
calculateValue(1, 5, 10);  // 5
Sign up to request clarification or add additional context in comments.

5 Comments

I agree. I feel I just made it overly complicated in the begining and Nina fixed one part of it by finding the Math.min, Math.max functions. But I think its subtley important to know that I was dealing with Math.min and Math.max and not write some custom logic for that :)
@Nishant you're not wrong about lt and gt as function either. JavaScript sucks for having these as operators instead of functions. I define them as const lt = x=> y=> y < x; and const gt = x=> y=> y > x;... as well as countless other functions that JavaScript decided to implement as operators.
Hope they add this to the language :)
@Nishant, it's highly unlikely they will do such a thing. JS is a multiparadigm language, so its interests are split trying to satisfy as many programmers as possible, while not fully satisfying anyone. It's more than just the operators, too. Did you ever notice there's countless other special syntaxes in JS, too? if,switch,for, while, etc don't even have return values. Unsurprisingly, this could all be implemented as functions too, but for some reason they're not. Not to mention the headaches around new and this.
Indeed. magic is not often good isn't it? Better to keep it straight forward so that language syntax doesn't bother you like in Scheme. I am starting to appreciate such languages, even though they are not directly accessible for ordinary programmers because they are taught the infix notation :)
1

You have already a mechanism for your requirement, Math.min and Math.max.

The code looks like this, but maxPossibleValue is missleading

var maxPossibleValue = function (predicate) {
        return function (c, f) {
            return predicate(c, f)
        }
    },
    comparer = maxPossibleValue(deltaValue > 0 ? Math.min : Math.max);

Working example:

var deltaValue = -1;
var maxPossibleValue = function (predicate) {
        return function (c, f) {
            return predicate(c, f)
        }
    },
    comparer = maxPossibleValue(deltaValue > 0 ? Math.min : Math.max);

document.write(comparer(2, 8));

3 Comments

Do you have any alternate suggestion for the function name? I used maxPossibleValue from the context of a transition function which is what I am doing here. Or okay maybe you mean it conflicts with Math.max :)
maybe applyPredicate?
Math.max and Math.min are not predicates. function(c,f) { return predicate(c,f); } simplifies to predicate (eta reduction)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.