Forums

For issue

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

For issue

Postby Csaba » Sun Apr 12, 2009 6:17 am

Hi,

In this simple for demo program:
Code: Select all
class ForIssue
   def main is shared
      for i in 1 : 5
         print i

The output is:
>C:\Cobra\Cobra.bat "ForIssue.cobra"
1
2
3
4
>Exit code: 0

Note, the upper limit, 5 in this case, is not printed..
In other words, the test seems to be <upper limit, not <=upper limit.
The behavior is the same when counting down (for I in 5 :-5 :-1)
Is it intended to be that way in Cobra?

Regards
Csaba
Csaba
 
Posts: 42

Re: For issue

Postby Charles » Sun Apr 12, 2009 11:43 am

Yes, it's the open half interval approach where the upper bound is exclusive. It works well in languages where valid indexes are 0 through n-1 as in:
t = [1, 2, 3]
for i in 0 : t.count
t[i] = -t[i]

# btw you can leave out the start when it's zero:
for i in t.count
t[i] = -t[i]


Cobra ranges, which are modeled after Python ranges, are the same way. t[i:j] --> [t[i], t[i+1], ..., t[j-1]]
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: For issue

Postby hopscc » Sun Apr 12, 2009 7:18 pm

Theres an existing discussion/explanation of this back in
Cobra and the Newbie
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: For issue

Postby Csaba » Mon Apr 13, 2009 2:13 am

Hi,
The c language has something like “for(0,<a.cont,1)” or you if you want you can write “for(0, a.cont-1,1)” so this is clearer (I never thought I ever in my life would use an C example to show that something is clearer! :o ).

In Cobra, the implicit <a.count is OK when counting up:
Code: Select all
      fileNames = ["a.xls", "b.ppt", "c.ppt", "a.doc"]
      for i in 0 : fileNames.count # OK
         print i, fileNames[i]

But see what it looks like when counting down:
Code: Select all
      for i in fileNames.count-1: 0-1 :-1         
        print i, fileNames[i]

In my opinion, this looks terrible and is error prone. To avoid this, Cobra could have an iterator that is guaranteed in order (have no better wording for this right now). Iterating up is not really needed, see above (except for symmetry reason), but for iterating down, it would be nice to avoid the example above:
Code: Select all
      forup i, fileName in fileNames
         print i, fileName

      fordown i, fileName in fileNames
         print i, fileName

Or does it already exist? (I think I have somewhere read a discussion about in-order iterators). Or are there better ways already? Anyhow, the count down example in an array has to be documented for newbies to Cobra.

Regards
Csaba
Csaba
 
Posts: 42

Re: For issue

Postby Charles » Mon Apr 13, 2009 7:08 pm

Here is what I came up with:
class X

def main is shared
print
for i in 0 : 5 : 1
print i stop
print

# same as above:
print
for i in 5
print i stop
print

# reversed:
print
for i in 5 : 0 : -1
print i stop
print

print
t = ['a', 'b', 'c', 'd', 'e']
for i in t.count : 0 : -1
print t[i-1] stop
print

print
t = ['a', 'b', 'c', 'd', 'e']
i = t.count
while i > 0
print t[i-1] stop
i -= 1
print
And the output is:
Code: Select all

01234

01234

54321

edcba

edcba

Note that the while loop starts at t.count and stops at > 0. Consequently, it has to use i-1. The for loop is no different.

Also, the for loops use of "start : stop : step" is modeled directly after Python's slicing and it's range functionality and matches their "end excluded" semantics.

Whether you want to use "i-1" in the loop, as I did, or adjust the boundaries as you did:
for i in fileNames.count-1 : -1 : -1
...
is a matter of style. Actually the "-1 : -1 : -1" forms a neat visual pattern. Pull the "0-" out and make the spacing around ":" uniform and I don't think it looks so bad.

Another approach:
for i, name in fileNames.reversed.numbered
print i, name

I supposed we could make a more efficient method by having a version of .numbered that goes in reverse without having to produce a new list. Is .reverseNumbered an okay name for that?

I agree this needs documentation.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: For issue

Postby hopscc » Tue Apr 14, 2009 12:23 am

.numberedDown (pref)
or
.numberedBack/.numberedBackwards
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: For issue

Postby Csaba » Tue Apr 14, 2009 11:08 am

Hi Chuck,
Thank you for the answers! I have not programmed so much in Python so I did not know that the For-statements worked like this. I think it is good to mention in the documentation that the behavior is inherited from Python.

For counting down, I would also prefer the suggestion from hopscc: numberedDown.

Regards
Csaba
Csaba
 
Posts: 42

Re: For issue

Postby Charles » Tue Apr 14, 2009 7:41 pm

.numberedDown it is and checked in just now.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 56 guests