Skip to content

Redundant subexpression in Generators and iterators example in doc #3222

@jhcarl0814

Description

@jhcarl0814

Describe the bug
jq 1.7 Manual / Generators and iterators

To Reproduce
In the first example:
jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)); range(0; 10; 3)'

  1. Redundant operation: else . produces an extra value beyond upto, then | select((by > 0 and . < upto) or (by < 0 and . > upto)) filters it.
  2. Should result in empty when init == upto.

Expected behavior
Can be simplified and corrected to:
jq 'def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)'

Environment (please complete the following information):
/

Additional context
/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions