Hans L on Sun, 19 Jul 2020 23:01:02 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Feature suggestion: "ifcmp" convenience control statement


When writing functions in gp, I think it is fairly common to need to
do 3 different things depending on a comparison of two values.
This can of course be done with nested if() but it feels verbose and
can get messy depending on how complicated the terms are:
if(a<b, do_lt(), if(a>b, do_gt(), do_eq() ))

I am suggesting to add a new builtin control statement that basically
combines "cmp" and "if" so the call signature would look like this:
ifcmp(a, b, {seq1}, {seq2}, {seq3})

"Evaluates the expression sequence seq1 if a < b, seq2 if a==b, or
seq3 if a > b ."

In addition to making it simpler to write such functions, it might
even be more efficient than evaluating separate ifs? (Say if a and b
were some complicated expressions which take some time to evaluate, or
even just the time taken during comparisons)  Although I admit I know
nothing about how scripts are evaluated internally so maybe it already
smartly caches results of identical expressions used more than once
like that.

Does anyone else think this would be a useful addition to the language?

- Hans