Forums

Tuples

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

Re: Tuples

Postby Charles » Mon Jun 24, 2013 4:27 pm

That's right about Python not requiring parens. Though I wouldn't be opposed to requiring them outside of "return" and assignment. I'm trying to think of other places in Python where they are not required. If you want to pass them as arguments, they certainly need parens:
Code: Select all
# python
obj.foo((1, 2), (2, 1))  # 2 arguments, each a tuple of two ints
obj.foo(1, 2, 2, 1)  # 4 arguments, each an int


Btw the funny syntax that hopscc was thinking of comes into play when your tuple has only one element:
Code: Select all
# python
t = (1,)

Without a trailing comma, it would not be a tuple.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Tuples

Postby Chasm » Tue Jun 25, 2013 5:33 am

Code: Select all
# python
t = (1,)

Without a trailing comma, it would not be a tuple.


That's really ugly
Chasm
 
Posts: 33

Re: Tuples

Postby hopscc » Tue Jun 25, 2013 7:03 am

(snap)
And also sorta confusing...
It may be a tuple if it has enclosing () but it doesnt always have to, sometime you need trailing commas but mostly not.
Its probably lexically convenient to tokenise/parse this way(in python) but its not very convenient for human recognition/learning/use.

I see Tuples as always useful for some things (multi-method returns with multi arg unpacking i.e ticket:315) and occasionally useful for some other things - like quick and dirty (temporary) immutable value class uses - you can do that with full blown classes but having Tuples available is faster, easier and more satisfying.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Tuples

Postby Chasm » Tue Jun 25, 2013 2:20 pm

It's probably a stupid question, but couldn't tuples be something like:

variable = {'text', 14, 12.5f, DateTime.Now}
variable = function({'text', 14, 12.5f, DateTime.Now})


I'm really tired, so I'm not really thinking at this point, but it just seems like a neat use for curly braces (they need some love too), make it obvious that it's a tuple and support implicit tuples at the same time, no?

Just a thought I wanted to share before going to sleep. :D
Chasm
 
Posts: 33

Re: Tuples

Postby hopscc » Wed Jun 26, 2013 1:35 am

Cobra already uses {} for Set {} and Dict/Map {:} literals

oddLt10 = {1, 3, 5, 7, 9} # Set of Integers
mapto = {1:'Wun', 3:'free', 5:'Fyfe', 7:'sefun', 9:'nyne' } # map of int key to String value

inp = .gettaNumber
if inp in oddLt10
print 'Odd and less than 10 "[inp]"'
print 'input [inp] maps to', mapto[inp]
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Tuples

Postby Chasm » Wed Jun 26, 2013 2:36 am

Whoops, totally forgot. Sorry for that :oops:
Chasm
 
Posts: 33

Re: Tuples

Postby hopscc » Wed Jun 26, 2013 4:30 am

No prob - theres a lot in cobra to forget ;)
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Previous

Return to Discussion

Who is online

Users browsing this forum: No registered users and 21 guests

cron