Forums

sets

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

sets

Postby DelphiGuy » Tue Jan 15, 2013 8:35 pm

Code: Select all
x = {a, b, c}
while y in x
    dosomething


Code: Select all
while y==a or y==b or y==c
    dosomething


i say these two code snippets are (or should be) functionally identical. yes?
DelphiGuy
 
Posts: 116

Re: sets

Postby nerdzero » Wed Jan 16, 2013 10:56 am

On the surface, I think so. Is this some kind of trap? :?
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: sets

Postby bertpritchard » Wed Jan 16, 2013 11:49 am

Lol. That's the same thought I had. I wondered if it was a trick question. ;)
bertpritchard
 
Posts: 7

Re: sets

Postby Charles » Wed Jan 16, 2013 12:18 pm

Same here. The following code works. I just used ints. Are you having problems with your own defined classes? Let us know.

"""
cobra set-in-or.cobra
"""

class P

def main
.one
.two
print 'done.'

def one
print 'one'
a, b, c = 1, 2, 3
x = {a, b, c}
y = a
trace y, x
while y in x
.doSomething
if y == a, y = b
else, y = .d
trace y, x
print 'done with one'

def two
print 'two'
a, b, c = 1, 2, 3
y = a
trace y, a, b, c
while y==a or y==b or y==c
.doSomething
if y == a, y = b
else, y = .d
trace y, a, b, c
print 'done with two'

def doSomething
pass

def d as int
return 4
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: sets

Postby DelphiGuy » Wed Jan 16, 2013 12:32 pm

LOL! yes, i'm having trouble tracking down the apparently conflicting behavior of the two formats, but before i give the debugging effort more time (including cleaning up and presenting an actual example of the conflict -- i'm a VERY slow programmer), i just wanted to make sure it wasn't "operator error".

sorry for the misleading tone. no trick question. just "am i chasing my tail in circles simply because i am unreasonably expecting cobra to do something other than what i'm (inadvertently) explicitly asking it to do?"

thanks for the heads up.

on an entirely different topic, i'm finally about to wade into mac/monotouch, figuring that i find programming so challenging, i might as well be challenged by my true target platform (iPhone, iPod) instead of win 7.
DelphiGuy
 
Posts: 116

Re: sets

Postby DelphiGuy » Sat Jan 19, 2013 5:09 am

Code: Select all
class P

    def main
        .three
        print 'done.'
        Console.readKey
           
    def three
      print 'three'
      a, b, c = 1, 2, 3
      x = {a, b, c}
      trace a, x
      a = 4
      trace a, x
      print 'done with three'


ok, now i get it. so assignments to variables of type "set" are by value, not by reference. i couldn't understand the wacky behavior i was getting within my "while" loop (which used the set as a looping condition). i was assigning a new value to one of the variables that had been an element in the original set assignment prior to the loop, and wrongly expecting the value of that element of the set to be updated as a result. thanks for the piece of code, which helped me learn more about how to do a simple debug like this.
DelphiGuy
 
Posts: 116

Re: sets

Postby Charles » Sat Jan 19, 2013 12:07 pm

You are basically correct, although your terminology is off.

In the .NET world, sets and other objects are assigned by reference. Only structs and primitive types (ints, bool, char, float, decimal) are assigned by value.

Sets, like lists and dictionaries, are collections. Also, they have no tie back to variables. These two sets are equivalent:
set1 = {1, 2}

x, y = 1, 2
set2 = {x, y}

assert set1 == set2 # equal because contents are the same
assert set1 is not set2 # not the same object
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 104 guests

cron