[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 2/2] compare function
From: |
Edward Welbourne |
Subject: |
Re: [PATCH 2/2] compare function |
Date: |
Tue, 9 Jun 2020 10:32:10 +0000 |
On Tue, Jun 9, 2020 at 10:53 AM Edward Welbourne <edward.welbourne@qt.io> wrote:
>> I note that your implementation only supports numeric lhs and rhs.
>> Might it be worth falling back, if either of them isn't numeric, to
>> doing a strcmp() comparison instead ? Possibly after stripping
>> leading space from each. There is value in lexical comparison, too,
>> after all.
Jouke Witteveen (9 June 2020 11:45)
> I am not sure it is wise to mix integer and string contexts. Having a
> $(compare) function that deals exclusively with integer context has
> some appeal to me.
I can sympathise with that ;^)
> $(if) also has a minimum of two arguments, so let's not make $less
> optional.
Works for me.
> Having $5 default to $4 was entirely deliberate. It means that the
> semantics of the four-argument version mimics $(if) in the sense that
> the expression evaluates to either the third or the fourth argument.
> If you want the behavior you describe, you could simply add a ','
> after the fourth argument, i.e. specify an empty fifth argument.
> Because this is so short, I think it is far more elegant than wrapping
> everything in an $(if).
Fair enough, you make a compelling case.
> Indeed, the proposals for sum and product make sense, but those for
> subtraction and division cannot be made to work, since the operations
> are not associative.
That doesn't preclude the "put op between each pair of words in $2" way
of doing things; this works and is easy to understand, albeit with one
wrinkle:
> It is not the syntax that bothers me, but the semantics. As stated a
> few times now,
>
> $(math -,3)
>
> could evaluate to '3' (in line with your expansion rule above) or to
> '-3' (in line with intuition and because we would need a way to do
> unary negation anyway). The design tries to achieve too many things at
> once and will always fail to accomplish some of them.
I'm definitely gunning for $(math -, 0 3) being the way to write -3; I
recognise it's the place that'll need the most care in documentation, as
folk are apt to expect $(math -, 3) to be -3 otherwise. It' still the
thing it's easiest to specify in a uniform and uncomplicated way. Once
folk are over the initial hurdle, it'll be obvious and clear what's
meant. This will be particularly relevant for $(math /, numer
denoms...) where - as you note - the fact that we're doing integer-only
maths means $(math /,3) *must* mean 3, not 1/3; so, for consistency,
$(math -,3) must also mean 3, not -3.
Suggested documentation fragment:
$(math op, args) combine args by inserting binary operator op between
words and evaluating the result arithmetically. Every word of args
must be an integer and the arithmetic is integer arithmetic, with
division rounding towards zero when relevant. The supported operators
for use as op are +, -, * (for multiplication) and / (for division).
Note that inserting op between words implies that passing a single
number as args (so there are no two numbers to put an operator
between) evaluates to that number, regardless of the op used. For
example, $(math -, 3) is 3; if you want -3 you need to write $(math -,
0 3) to subtract the 3 from 0. The op is only used to combine values,
never as a unary operator.
Which may "feel odd", to most, at first but is easy to make sense of and
use; once learned, it will "come naturally" and soon enough "feel
intuitive".
Eddy.