Integer sequence: Difference between revisions

m (→‎{{header|Wren}}: Changed to Wren S/H)
 
Line 1,596:
 
=={{header|jq}}==
Currently,The Go implementation of jq does not support infinitearbitrary-precision integer arithmetic, but verycurrently large(2024) integersthe areC convertedimplementation toof floating-point numbers, so the followingjq will continueresort to generatefloating-point integersarithmetic (beginningfor withvery 0)large indefinitely in recent versions of jq that have tail recursion optimization:integers.
<syntaxhighlight lang="jq">def iota: ., (. + 1 | iota);
0 | iota</syntaxhighlight>In versions of jq which have <tt>while</tt>, one could also write:<syntaxhighlight lang="jq">0 | while(true;. + 1)</syntaxhighlight>This idiom is likely to be more useful as <tt>while</tt> supports <tt>break</tt>.
 
Consider, for example:
Another technique would be to use <tt>recurse</tt>:
 
<syntaxhighlight lang="jq">0 | recurse(. + 1)</syntaxhighlight>For generating integers, the generator, <tt>range(m;n)</tt>, is more likely to be useful in practice; if m and n are integers, it generates integers from m to n-1, inclusive.
<syntaxhighlight lang="jq">0 | recurse(. + 1)</syntaxhighlight>
 
Using gojq, this will indefinitely generate a stream of integers beginning with 0, but jq (the C implementation) will eventually lose precision.
<syntaxhighlight lang="jq">0 | recurse(. + 1)</syntaxhighlight>For generating integers, the generator,built-in function <tt>range(m;n)</tt>, is more likely to be useful in practice; if m and n are integers, it generates integers from m to n-1, inclusive. `range(m; infinite)` is also valid for any integer.
 
The C implementation of jq supports tail recursion optimization, and thus the following tail-recursive definition could be used:
<syntaxhighlight lang="jq">def iota: ., (. + 1 | iota);
0 | iota</syntaxhighlight>
 
One could also write:<syntaxhighlight lang="jq">0 | while(true; . + 1)</syntaxhighlight>
 
Integers can of course also be represented by strings of decimal digits, and if this representation is satisfactory, a stream of consecutive integers thus represented can be generated using the same technique as is employed on the
2,442

edits