-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
nth/2 semantic is strange #1867
Comments
I agree. With builtin modules we could have a For example, I think allowing multiple functions with the same names but different arities turns out to be a mistake because the |
@muhmuhten - In my opinion, the current def of nth/2 is simply wrong. @nicowilliams - I don't really see a connection between muhmuten's observations (which I take to be that nth/2 is either misnamed or incorrectly implemented) and your remarks regarding ,/; confusability. |
@pkoppstein Hmm, well, maybe. I've been worried that it was a mistake. Good to know someone doesn't agree with that. |
I think we can just fix |
@pkoppstein I think first/0, last/0, and nth/1 operating on arrays is a mistake and probably shouldn't have existed in the first place, but too late... Some notion of multiple arguments is clearly necessary, and it's hard to get around the confusion for something like limit without flat-out having explicit syntax for expressions that can be multi-valued. It's also decent substitute for having default parameters; the main pain points are Most of the latter are gratuitous array functions... (e.g. imo the right any/1 is On the other hand, the only really salient one is range/1, which felt like the #1 builtin I missed during that stint of C-builtins-only jq despite the workaround being trivial. |
def nth(n; g): last(limit(n + 1; g));
does not match my intuition for what "the nth output of g" means when there are fewer than n+1 outputs of g.Expected behavior
nth($n; exp)
should probably be analogous to[exp][$n]
(i.e.([exp]|.[$n])
), except less expensive and without evaluating the $n+1th and subsequent outputs of exp.One thing to note is that
$array[$n] != $array[:$n][-1]
, but more closely matches$array[$n:][0]
... If $n is greater than the number of outputs, I'd expect to get back either empty or null. This implies something more like the following:Additional context
I thought I'd found a more efficient implementation of
nth
in the following, but well, the above:This would be kind of a gratuitous incompatibility but might be nice for 2.0.
(The above first/drop implementation runs just as fast but reads nicer IMO.)
The text was updated successfully, but these errors were encountered: