Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ER: add abs/0; deprecate or discourage NUMBER | length #2672

Closed
pkoppstein opened this issue Jul 9, 2023 · 4 comments
Closed

ER: add abs/0; deprecate or discourage NUMBER | length #2672

pkoppstein opened this issue Jul 9, 2023 · 4 comments

Comments

@pkoppstein
Copy link
Contributor

Most programming languages have an abs or similarly named function for convenient computation of the absolute value of a number. In its stead, jq has length, which
for many people is at best surprising. It is also noteworthy that this oddity was
not documented in jq 1.6 documentation until a recent commit (dd5ce98 on 2023-06-21).

Since NUMBER | length has been around for a while, and since it has been documented,
I am not proposing that it be done away with, only that the recent above-mentioned update be further updated by deprecating or discouraging it. As @nicowilliams wrote: "Too much polymorphism in a dynamically typed language is a mistake."

In the context of jq, this is surely the pre-eminent example. AFAIK no other programming language uses the word length for absolute value, and even if that's not the case, abs more clearly indicates intention.

@nicowilliams
Copy link
Contributor

Well, we do have fabs. Certainly it could also be called abs.

@pkoppstein
Copy link
Contributor Author

@nicowilliams wrote:

Well, we do have fabs. Certainly it could also be called abs.

  1. Is jq absolutely guaranteed to have fabs in all environments and configurations?

  2. I think it would be better to have a builtin:

def abs: if . < 0 then - . else . end;

because:

(a) using jqMaster, that would ensure that x|abs does not unnecessarily convert x to an integer, as illustrated by:

$ jqMaster -nM 'def abs: if . < 0 then - . else . end; 10000000000000000000000000002 | abs'
10000000000000000000000000002

b) in some ways, it "future proofs" jq by pushing the semantics upstream.

In any case, if abs is implemented as fabs, the documentation should be written so as NOT to require gojq to follow suit.

Of course, if abs were defined as above, it would NOT be equivalent to length@number, another point which presumably should be documented as well.

@pkoppstein
Copy link
Contributor Author

pkoppstein commented Jul 9, 2023

See #2676

Note that this PR defines abs naively -- there is no type-checking.
This is mainly for speed and conciseness, but also because that is sufficient to raise an error in many cases, and because in other cases ("abc" | abs), I cannot think of anything very horribly disastrously wrong other than the presumptive "type error" itself.

@itchyny
Copy link
Contributor

itchyny commented Jul 31, 2023

Included by #2767.

@itchyny itchyny closed this as completed Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants