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

[Docs] Clarify the behvior of first, last, and nth on empty generators #3177

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

mousetail
Copy link

I was confused by this behavior so thought it might be good to document it.

@@ -3017,10 +3017,16 @@ sections:

The `nth(n)` function extracts the nth value of any array at `.`.

`first` and `nth` output `empty` if the generator has insufficient
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if "produces no output" or "outputs no value" is better then "output empty"? seems to be what is used in other places

Copy link
Author

@mousetail mousetail Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the documentation for try and break also uses the phrase "output/produce empty". In this case I think it's a bit more explicit than "outputs no value".

Copy link
Member

@wader wader Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, i mostly want consistency but i think i personally prefer "no output" as mentioning empty feel a bit confusing, ex would we document the function empty as producing empty? if we want to use the word empty maybe it should not be written as a function but instad be something like "the empty generator" or "empty output" but then i think i prefer "no output" 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the docs to say "produce no values" instead

Copy link
Member

@wader wader Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 let's see what the other maintainers think

But i do wonder what the reasoning was for last/1 to return null?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One potential "fixed" last that outputs no values for an empty generator would be this:

def last(g): reduce g as $item ([0,null]; [first+1,$item])|if first>0 then last else empty end;

A lot less elagent than the current implementation though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1869. This is a bug that should be fixed in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@itchyny 👍 oh long discussion, seems fascinatingly messy :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That branch is 5 years old, I doubt it will still be merged

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use byte code to avoid boxing like the PR even if it makes more messy.

@itchyny
Copy link
Contributor

itchyny commented Sep 14, 2024

Maybe you want to update the docs for last/1 but editing the wrong place. jq "[range(.)] | [[first], [last], [nth(5)]]" <<< "0" yields [[null],[null],[null]].

@mousetail
Copy link
Author

@itchyny I moved the doc update to the correct section

@pkoppstein
Copy link
Contributor

My understanding is that PR currently adds the text:

`last` returns `null` instead.

Of course, that is true of the current implementation, but if this behavior is to be documented in the manual, there should be a disclaimer to the effect that the behavior may change in the future.

(As @itchyny recently pointed out:

See #1869. This is a bug that should be fixed in the future.
)

Copy link
Member

@wader wader left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm debating with myself if it's consistent that first/0 and last/0 output null for an empty array. It makes sense because they operate on an array instead of a generator?

@mousetail
Copy link
Author

first/0 having the same behavior as .[0] seems logical to me.

The existence of first/last/nth seems a bit pointless anyways since you can just use .[0], .[-1], .[n] which are shorter. But that's an entirely seperate point.

Copy link
Contributor

@pkoppstein pkoppstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding a disclaimer. Perhaps, however, it would be better if the documentation were agnostic as to whether it's a bug or not, though I would agree that simply describing the behavior as "unspecified" would be a bit silly. I believe it would be sufficient to say that currently last(empty) evaluates to null, but that in future it is likely to evaluate to the empty stream.

@itchyny
Copy link
Contributor

itchyny commented Sep 18, 2024

I submitted a patch for the issue: #3179.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants