Skip to content

Commit

Permalink
fix reduce syntax to emit results for each initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Nov 16, 2024
1 parent b685aac commit 8a09826
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
43 changes: 43 additions & 0 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3379,6 +3379,22 @@
expected: |
4.5
- name: reduce with select in update
args:
- 'reduce range(5) as $x (0; . + $x | select($x != 2))'
input: 'null'
expected: |
8
- name: reduce with query in start
args:
- 'reduce range(5) as $x (range(3); . + $x)'
input: 'null'
expected: |
10
11
12
- name: reduce with variable binding
args:
- 'reduce .[] as $x (0; . + $x) as $x | $x'
Expand Down Expand Up @@ -3438,6 +3454,33 @@
0
1
- name: foreach with select in update
args:
- -c
- 'foreach range(5) as $i (0; . + $i | select($i != 2); [$i, .])'
input: 'null'
expected: |
[0,0]
[1,1]
[3,4]
[4,8]
- name: foreach with query in start
args:
- -c
- 'foreach range(3) as $i (range(3); . + $i; [$i, .])'
input: 'null'
expected: |
[0,0]
[1,1]
[2,3]
[0,1]
[1,2]
[2,4]
[0,2]
[1,3]
[2,5]
- name: foreach with unary operator
args:
- '[-foreach -.[] as $i (0; . + $i)]'
Expand Down
6 changes: 3 additions & 3 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,6 @@ func (c *compiler) compileTry(e *Try) error {
func (c *compiler) compileReduce(e *Reduce) error {
c.appendCodeInfo(e)
defer c.newScopeDepth()()
setfork := c.lazy(func() *code {
return &code{op: opfork, v: len(c.codes)}
})
c.append(&code{op: opdup})
v := c.newVariable()
f := c.newScopeDepth()
Expand All @@ -737,6 +734,9 @@ func (c *compiler) compileReduce(e *Reduce) error {
}
f()
c.append(&code{op: opstore, v: v})
setfork := c.lazy(func() *code {
return &code{op: opfork, v: len(c.codes)}
})
if err := c.compileQuery(e.Query); err != nil {
return err
}
Expand Down

0 comments on commit 8a09826

Please sign in to comment.