Loop with negative count
[1/12] from: tim-johnsons:web at: 19-Dec-2006 17:21
Hello:
on Core 2.6.2.4.2
>> help loop
makes no reference to negative values for the 'count argument.
consider the following console session
>> test: []
== []
>> loop -4 [append test "hello"]
>> test
== []
Can we reliably expect that a negative argument will be ignored?
thanks
tim
--
Tim Johnson <tim-johnsons-web.com>
http://www.alaska-internet-solutions.com
[2/12] from: greggirwin:mindspring at: 27-Dec-2006 19:02
Hi Tim,
>>> help loop
TJ> makes no reference to negative values for the 'count argument.
TJ> consider the following console session
>>> test: []
TJ> == []
>>> loop -4 [append test "hello"]
>>> test
TJ> == []
TJ> Can we reliably expect that a negative argument will be ignored?
So far. :) In the case of LOOP and REPEAT, it seems safe to say that
negative values make no sense, and will always be ignored. Others may
argue that REBOL should throw an error, but I imagine Carl considered
that behavior and chose not to do it, so you're probably safe. Maybe
we can get a quick answer from him to be sure though.
I wouldn't feel safe saying the same thing about all other functions.
For example, FORSKIP/FORALL changed, but hopefully for the better
since negative numbers would loop forever before, and now throw an
error.
-- Gregg
[3/12] from: pwawood::gmail::com at: 28-Dec-2006 10:50
Tim
Why don't you protect your code against changes in undocumented
behaviour by either using:
Loop max 0 possible-negative-count
or
if positive? count [loop count [code]]
I thought that there would be a time penalty in doing so but my tests
show it is hardly noticeable. (Providing my tests are correct).
Regards
Peter
Test Source:
REBOl []
count: to integer! ask "Enter an integer - positive of negative: "
times: 100000
start: now/time/precise
loop times
[
test: copy []
loop count [append test "hello"]
]
end: now/time/precise
print "test 1"
print [times "loops with count of " count " took: " subtract end start]
start: now/time/precise
loop times
[
test: copy []
loop max 0 count [append test "hello"]
]
end: now/time/precise
print "test 2"
print [times "loops with count of " count " took: " subtract end start]
start: now/time/precise
loop times
[
test: copy []
if positive? count [loop count [append test "hello"]]
]
end: now/time/precise
print "test 3"
print [times "loops with count of " count " took: " subtract end start]
>> do %tt.r
Script: "Untitled" (none)
Enter an integer - positive of negative: 2
test 1
100000 loops with count of 2 took: 0:00:02.404771
test 2
100000 loops with count of 2 took: 0:00:02.365083
test 3
100000 loops with count of 2 took: 0:00:02.41553
>> do %tt.r
Script: "Untitled" (none)
Enter an integer - positive of negative: 8
test 1
100000 loops with count of 8 took: 0:00:08.297555
test 2
100000 loops with count of 8 took: 0:00:07.476637
test 3
100000 loops with count of 8 took: 0:00:07.236025
>> do %tt.r
Script: "Untitled" (none)
Enter an integer - positive of negative: -4
test 1
100000 loops with count of -4 took: 0:00:00.843843
test 2
100000 loops with count of -4 took: 0:00:00.683992
test 3
100000 loops with count of -4 took: 0:00:00.466519
>> do %tt.r
Script: "Untitled" (none)
Enter an integer - positive of negative: -55
test 1
100000 loops with count of -55 took: 0:00:00.902979
test 2
100000 loops with count of -55 took: 0:00:00.600534
test 3
100000 loops with count of -55 took: 0:00:00.513414
>> do %tt.r
Script: "Untitled" (none)
Enter an integer - positive of negative: 30
test 1
100000 loops with count of 30 took: 0:00:37.077424
test 2
100000 loops with count of 30 took: 0:00:33.583799
test 3
100000 loops with count of 30 took: 0:00:38.293701
[4/12] from: tim-johnsons:web at: 27-Dec-2006 19:20
* Peter Wood <pwawood-gmail.com> [061227 18:08]:
> Tim
>
> Why don't you protect your code against changes in undocumented
> behaviour by either using:
Thanks. Of course.
Also it could be redefined probably with some
performance penalty.
loop: func[count[integer!] code[block!]][
if positive? count[
system/words/loop count code
]
] ;; untested and "unbenchmarked", but not
;; "unfooledaroundwith" :-)
> Loop max 0 possible-negative-count
> or
<<quoted lines omitted: 105>>
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
--
Tim Johnson <tim-johnsons-web.com>
http://www.alaska-internet-solutions.com
[5/12] from: tim-johnsons::web::com at: 27-Dec-2006 19:23
* Gregg Irwin <greggirwin-mindspring.com> [061227 17:14]:
> Hi Tim,
> TJ> Can we reliably expect that a negative argument will be ignored?
<<quoted lines omitted: 3>>
> that behavior and chose not to do it, so you're probably safe. Maybe
> we can get a quick answer from him to be sure though.
to throw or not to throw, that is the question ...
actually, always a good one to ask. I write my own code with
a lot of throws.....
> I wouldn't feel safe saying the same thing about all other functions.
> For example, FORSKIP/FORALL changed, but hopefully for the better
> since negative numbers would loop forever before, and now throw an
> error.
Thanks Gregg
<grin> BTW: that posting was sent on December 19. Where've y'all
been? </grin>
tim
> -- Gregg
>
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
--
Tim Johnson <tim-johnsons-web.com>
http://www.alaska-internet-solutions.com
[6/12] from: lmecir:mbox:vol:cz at: 28-Dec-2006 9:17
Hi Tim,
> * Gregg Irwin <greggirwin-mindspring.com> [061227 17:14]:
>> Hi Tim,
<<quoted lines omitted: 25>>
>> lists at rebol.com with unsubscribe as the subject.
>>
I received the post(s) only now (don't know why). I will add the
negative value tests requiring zero repetitions to test suite, which
seems the most expected behaviour to me and which is truly the case now.
-Ladislav
[7/12] from: pwawood::gmail::com at: 28-Dec-2006 21:55
Ladislav
That is good news. Will your tests eventually be published? They are no
doubt an excellent document of Rebol's behaviour.
I think Tim's original post got delayed somewhere in the list processor:
From http://mail.rebol.net/cgi-bin/mail-list.r
41498 27-Dec-2006/19:02:35-7:00 Gregg Irwin (greggirwin) 971 Re:
Loop with negative count
41497 19-Dec-2006/17:21:29-9:00 Tim Johnson (tim) 481 Loop with
negative count
41496 27-Dec-2006/13:33:25-8:00 Brian Wisti (brian.wisti) 1209 Re:
Setting default font for all View sessions
But from http://mail.rebol.net/maillist/
41498 Re: Loop with negative count Gregg Irwin (greggirwin)
27-Dec-2006/21:02:36-5:00
41497 Loop with negative count Tim Johnson
(tim) 27-Dec-2006/20:45:48-5:00
41496 Re: Setting default font for all View sessions Brian Wisti
(brian.wisti) 27-Dec-2006/16:33:29-5:00
Perhaps Tim's message was deferred to a moderator?
Regards
Peter
On Thursday, December 28, 2006, at 04:17 pm, Ladislav Mecir wrote:
[8/12] from: tim-johnsons::web::com at: 28-Dec-2006 9:29
* Peter Wood <pwawood-gmail.com> [061228 05:09]:
> Perhaps Tim's message was deferred to a moderator?
:-) And probably because I'm such a noob...
tj
--
Tim Johnson <tim-johnsons-web.com>
http://www.alaska-internet-solutions.com
[9/12] from: lmecir:mbox:vol:cz at: 29-Dec-2006 12:14
Hi Peter,
...
> Ladislav
>
> That is good news. Will your tests eventually be published? They are no
> doubt an excellent document of Rebol's behaviour
I guess that there will be a way how to contribute new tests. (and
eventually read the existing ones). Until then here is a specimen FOR
test, which passes:
[
success: true
num: 0
for i 1 10 1 [
num: num + 1
success: i = num and success
]
10 = num and success
]
For an illustration here is a FOR test that fails:
; infinite loop tests
[
num: 0
for i b: [1] tail b 1 [
num: num + 1
if num > 2 [break]
]
num <= 2
]
This is a LOOP test inspired by Tim:
[
success: true
loop -1 [success: false]
success
]
-L
[10/12] from: greggirwin::mindspring::com at: 29-Dec-2006 12:32
Hi Ladislav,
LM> I guess that there will be a way how to contribute new tests. (and
LM> eventually read the existing ones). Until then here is a specimen FOR
LM> test, which passes:
LM> [
LM> success: true
LM> loop -1 [success: false]
LM> success
LM> ]
So, tests are just blocks that fail if they return false or none?
-- Gregg
[11/12] from: pwawood::gmail::com at: 30-Dec-2006 11:48
Hi Ladislav
Thanks for the insight into the RT tests. I think it would be good if
there was a way for the community to submit tests. I guess submitting a
test that fails would be a good way to report a bug via RAMBO if the
test was in a standard form.
Personally, I've started using Chris Coussement's Rebol Unit to build a
set of tests for Rebol.org. One thing that I've found extremely useful
is how it lets you organise tests in separate files. I have been able
to build a hierarchy of tests so that I can easily run the tests for a
single function, a single object!, a single source file or all the
tests.
Regards
Peter
On Friday, December 29, 2006, at 07:14 pm, Ladislav Mecir wrote:
[12/12] from: lmecir::mbox::vol::cz at: 29-Dec-2006 22:33
Gregg Irwin napsal(a):
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted