From da28628edd37c2f629326c32ed4d0c7373a44e1b Mon Sep 17 00:00:00 2001
From: Han Jiang <jhcarl0814@gmail.com>
Date: Mon, 27 Jan 2025 23:45:27 +1300
Subject: [PATCH] Fix redundant subexpression in Generators and iterators
 example in doc (#3239)

---
 docs/content/manual/dev/manual.yml | 5 ++---
 jq.1.prebuilt                      | 2 +-
 tests/man.test                     | 2 +-
 3 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/docs/content/manual/dev/manual.yml b/docs/content/manual/dev/manual.yml
index 38b8c5bfbe..3abd4892b6 100644
--- a/docs/content/manual/dev/manual.yml
+++ b/docs/content/manual/dev/manual.yml
@@ -3223,9 +3223,8 @@ sections:
                     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));
+                        else empty end;
+                    if init == upto then empty elif by == 0 then init else init|_range end;
                 range(0; 10; 3)'
             input: 'null'
             output: ['0', '3', '6', '9']
diff --git a/jq.1.prebuilt b/jq.1.prebuilt
index fadad46cc5..50de569a86 100644
--- a/jq.1.prebuilt
+++ b/jq.1.prebuilt
@@ -3644,7 +3644,7 @@ All jq functions can be generators just by using builtin generators\. It is also
 .
 .nf
 
-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)\'
+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)\'
    null
 => 0, 3, 6, 9
 
diff --git a/tests/man.test b/tests/man.test
index 5489c5e20e..12957c8fc4 100644
--- a/tests/man.test
+++ b/tests/man.test
@@ -927,7 +927,7 @@ foreach .[] as $item (0; . + 1; {index: ., $item})
 {"index":2,"item":"bar"}
 {"index":3,"item":"baz"}
 
-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)
+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)
 null
 0
 3