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

tonumber doesn't work on true or false #3112

Open
davidfetter opened this issue May 7, 2024 · 4 comments
Open

tonumber doesn't work on true or false #3112

davidfetter opened this issue May 7, 2024 · 4 comments

Comments

@davidfetter
Copy link
Contributor

It should because arithmetic on booleans is often a clear way to get complicated things done.

davidfetter added a commit to davidfetter/jq that referenced this issue May 7, 2024
@itchyny
Copy link
Contributor

itchyny commented May 7, 2024

Any example how useful it is? I'm not sure how common this conversion is, and I'm afraid using this tends to make query less readable.

@pkoppstein
Copy link
Contributor

pkoppstein commented May 7, 2024

Any example ...

As it happens, I was just working on def kruskal
which requires converting a (strictly) boolean array to an array of 0s and 1s. I would have most likely written
map(tonumber) if that had been possible.

However, I am not sure the pros in this case outweigh the cons, partly because "tonumber" was mainly intended for
parsing numeric strings. If it were to be extended to boolean, why not to other types, notably "null"? Also, even in cases such as def krusal, there is a potential downside to using tonumber in that a stray value such as 2 or "2" would not be caught as an error.

I'm not trying to make a general case against polymorphism, but there is a general case to be made against increasing polymorphism - the point being that it raises issues about "backward compatibility" if broadly understood.

@itchyny
Copy link
Contributor

itchyny commented May 7, 2024

How about making length work on booleans? I've never heard of length of booleans, but neither of length of null, which already works.

@davidfetter
Copy link
Contributor Author

Any example ...

As it happens, I was just working on def kruskal which requires converting a (strictly) boolean array to an array of 0s and 1s. I would have most likely written map(tonumber) if that had been possible.

I've been managing to punch out of my weight in speeding things up by sneaking around the monster known to speeders-up of code as The Branch Predictor. Looking at the code folks have written along this line, it's started to look like a nice way to do things even when speeding-up is not the highest goal.

I suppose I could invent some BS about how mapping bools to bits is somehow important in LLMs, but I just won't do that for fashion. I figure it's cool and shouldn't make things worse for those who don't use it.

However, I am not sure the pros in this case outweigh the cons, partly because "tonumber" was mainly intended for parsing numeric strings.

Stuff gets repurposed all the time, so "primarily intended" goes to a reverence for the past that would be a lot easier to justify if jq were all about the strict mathematical definitions, program proving, and so on. Is that the direction the project is going?

If it were to be extended to boolean, why not to other types, notably "null"? Also, even in cases such as def krusal, there is a potential downside to using tonumber in that a stray value such as 2 or "2" would not be caught as an error.

I'm sure I could come up with some strained analogies that didn't apply to the case at hand if I really wanted to, but I don't do that because I don't see them as advancing a discussion, fun as doing that can be.

I'm not trying to make a general case against polymorphism, but there is a general case to be made against increasing polymorphism - the point being that it raised issues about "backward compatibility" if broadly understood.

Serious question. Are there jq code bases whose needs we need to look after with something other than a changelog where this is a big concern?

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