Integer sequence: Difference between revisions
→{{header|jq}}: update
m (→{{header|Wren}}: Changed to Wren S/H) |
(→{{header|jq}}: update) |
||
Line 1,596:
=={{header|jq}}==
<syntaxhighlight lang="jq">def iota: ., (. + 1 | iota);▼
Consider, for example:
<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.
▲
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
|