Loops/Wrong ranges: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m →{{header|Perl 6}}: meh, use defined instead of boolean true |
No edit summary |
||
Line 184: | Line 184: | ||
Range(0, 0, 0) -> [0 0 0 0 0 0 0 0 0 0] |
Range(0, 0, 0) -> [0 0 0 0 0 0 0 0 0 0] |
||
</pre> |
</pre> |
||
=={{header|Huginn}}== |
|||
[https://huginn.org/ Huginn] has the Range generator in Algorithms package. |
|||
Instantiation of an a priori invalid range is a fatal error. |
|||
<lang huginn>import Algorithms as algo; |
|||
class Example { |
|||
_start = none; |
|||
_stop = none; |
|||
_step = none; |
|||
_comment= none; |
|||
} |
|||
main() { |
|||
examples = [ |
|||
Example( -2, 2, 1, "Normal" ), |
|||
Example( 2, 2, 0, "Start equal stop: zero increment" ), |
|||
Example( 0, 0, 0, "Start equal stop equal zero: zero increment" ), |
|||
Example( 2, 2, 1, "Start equal stop: positive increment" ), |
|||
Example( 2, 2, -1, "Start equal stop: negative increment" ), |
|||
Example( -2, 2, 10, "First increment is beyond stop value" ), |
|||
Example( -2, 2, 0, "Zero increment, stop greater than start" ), |
|||
Example( -2, 2, -1, "Increments away from stop value" ), |
|||
Example( 2, -2, 1, "Start more than stop: positive increment" ) |
|||
]; |
|||
for ( ex : examples ) { |
|||
print( |
|||
"{}\nRange( {}, {}, {} ) -> ".format( |
|||
ex._comment, ex._start, ex._stop, ex._step |
|||
) |
|||
); |
|||
r = algo.range( ex._start, ex._stop, ex._step ); |
|||
print( |
|||
"{}\n\n".format( |
|||
algo.materialize( algo.slice( r, 22 ), list ) |
|||
) |
|||
); |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Normal |
|||
Range( -2, 2, 1 ) -> [-2, -1, 0, 1] |
|||
Start equal stop: zero increment |
|||
Range( 2, 2, 0 ) -> [] |
|||
Start equal stop equal zero: zero increment |
|||
Range( 0, 0, 0 ) -> [] |
|||
Start equal stop: positive increment |
|||
Range( 2, 2, 1 ) -> [] |
|||
Start equal stop: negative increment |
|||
Range( 2, 2, -1 ) -> [] |
|||
First increment is beyond stop value |
|||
Range( -2, 2, 10 ) -> [-2] |
|||
Zero increment, stop greater than start |
|||
Range( -2, 2, 0 ) -> ./range.hgn:32:17: Invalid range. |
|||
Exit 3</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Although Kotlin's 'for' statement can deal with a range of integers, the increment must be positive and so it cannot be used for this task. We therefore use instead a 'while' statement to generate the same sequence as a C language 'for' statement would (limited to a maximum of 10 elements as some sequences will be infinite) and wrap it in a function. |
Although Kotlin's 'for' statement can deal with a range of integers, the increment must be positive and so it cannot be used for this task. We therefore use instead a 'while' statement to generate the same sequence as a C language 'for' statement would (limited to a maximum of 10 elements as some sequences will be infinite) and wrap it in a function. |