-
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
Bug using += #1508
Comments
The documentation is inaccurate. The RHS of update-assignment doesn't receive the LHS as input. The actual behaviour is more like It probably makes more sense to update the documentation than break compatibility, since this is arguably the more useful operation: you're more likely to be doing arithmetic on values at the same level than, say, merging a field of an object with the object itself. e.g. |
Actually, I think this is a bug indeed!
The problem is that
Compare to the
I don't know if we can fix this now. It's always been thus. @stedolan? |
I think this should not be considered a bug. |
Re-thinking this now, I think perhaps it would have been best to pass the current value of the On the other hand, we have documented what the input to the I think most likely we could make the code match the docs and it should more than likely not break any existing jq code because most jq code using OTOH, this has been like this for a long time, and we could just leave it as-is and instead fix the docs to say that the input to the RHS is unspecified, thus letting us wait until whatever release follows 1.7 to change this. |
For what it's worth, I stand by the position that it's more useful to document the current behavior for the arith-assign ops so that e.g. For the //= issue, my (not too considered) thought is that the best unifying semantic might be for |
@nicowilliams A common way I use #!/bin/sh --
# \
exec jq -nRrf "$0" -- "$@"
[
inputs / " -> " |
map(. / "," | map(tonumber)) |
range(0; length - 1) as $i |
.[$i:$i + 2]
] |
(map(.[][0]) | min) as $minx |
(map(.[][0]) | max) as $maxx |
(map(.[][1]) | max) as $maxy |
[ 500 - $minx, 0 ] as $sandIn |
.[][][0] -= $minx |
reduce .[] as $line ([];
def xrange: $line | map(.[0]) | range(min; max + 1);
def yrange: $line | map(.[1]) | range(min; max + 1);
.[xrange][yrange] = "#"
) |
def gridToString:
transpose |
(.[][] | nulls) = "." |
map(join("")) |
join("\n");
{ grid: ., $sandIn, units: [] } |
while(.grid[.sandIn[0]][.sandIn[1]] == null;
.units += [ .sandIn ] |
def directions($grid):
select(.[1] <= $maxy) |
.[1] += 1 |
.[0] += (0, -1, 1) |
select(.[0] < 0 or $grid[.[0]][.[1]] == null);
reduce (.units | keys_unsorted[]) as $k ({ offset: 0, v: . };
def unit: .v.units[$k + .offset];
.v.grid as $grid |
[ unit | first(directions($grid)) ] as $dir |
if $dir == [] then
.v.grid[unit[0]][unit[1]] //= "o" |
del(unit) |
.offset -= 1
else
unit = $dir[0] |
select($dir[0][0] < 0) |= (
.v.grid |= [ [] ] + . |
(.v | .sandIn, .units[])[0] += 1
)
end
) |
.v
) |
reduce .units[] as $u (.grid; .[$u[0]][$u[1]] = "%") |
gridToString,
"" Some example inputs (describe the map in which the sand falls):
But I have many other more pratical examples in which I use In my scripts, I can also find many instances of me using Also, if you need something like |
Oh, hmm, no, |
@emanuele6 and @muhmuhten have the winning arguments. Let's fix the docs then. |
It could also be Note that all non- What used to confuse me a bit is that empty = (.bar | map(.baz) | debug) As a more convenient way to use |
Oh, yeah, I definitely overlooked Unifying //= to not evaluate in a way that doesn't bork And yeah, sending empty to the rhs of //= would be spectacularly silly :p |
I'm as annoyed by that as you.
Yes.
That's because
I'm failing to make sense of that.
I often write |
If someone is using |
|
@itchyny fixed that! It now yields
I've struggled with this before. Basically I try never to use non-literals as the RHS of An interesting change we could make in the future would be to let the RHS see all of the value being "modified", the path where it's modified, and the
:) |
? Interesting! Well, anyways, we should add a nicer version of |
@nicowilliams wrote:
I've found this debug/1 to be both simple and serviceable:
The nice thing is that it does not preclude a debug/2 :-) Any other suggestions based on the existing |
I've added wiki pages on the internals of assignments and path expressions. |
@emanuele6 wrote:
The above remark was not made w.r.t. Please also consider that there has been a nice alignment between jq, gojq and jaq, in that all three give the same results for:
In short, I think something like the following ought to be in the documentation:
After all, jq is no longer in alpha :-) p.s. Maybe these too:
|
I believe that will hold on to a reference on the message
But don't quote me on this. I'm a bit rusty. |
@nicowilliams out of curiosity: i've usually used this |
If you have |
I've created #2708 related to this. The idea is that maybe ASIDE: Imagine a "pure" jq where there's no syntactic sugars. No |
Continued curiosity: would some basic static analysis be able to see that |
Yes, that could be done, and it would be amazing. This is why I have a branch with a JSON dump of block representation of programs: to help me reason about static analysis. |
(Sorry I edited your comment, @wader, I meant to quote-reply and flubbed up. I've restored your comment.) |
@nicowilliams no worries! |
CORRECT:
INCORRECT:
This behavior can be observed with jq1.4, jq 1.5, and the master version
(jq-1.5rc2-245-g7b81a83).
The text was updated successfully, but these errors were encountered: