Forums

Tuples

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

Tuples

Postby Caligari » Wed Jul 08, 2009 6:10 pm

Do we have support for tuples of mixed types in Cobra?

I've found mention of such things in the .NET documentation, but I don't seem to be able to do:

Code: Select all
var dataStore as Dictionary<of String, Tuple<of Regex, bool, int?, int?, int?>>

If I'm using all the same type, or I don't want to check types, I can probably use a List in place of the Tuple, but I'd like all that such a data structure would promise. Am I missing something we already have?
Caligari
 
Posts: 33

Re: Tuples

Postby Charles » Wed Jul 08, 2009 7:21 pm

We don't have typed tuples as shown in your example. You would have to define a custom class or live with List<of dynamic> or List<of Object>.

What .NET documentation are you referring to? Thanks.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Tuples

Postby Caligari » Wed Jul 08, 2009 10:57 pm

I should have been more clear in what .NET stuff I was referring to. There seems to be some Framework 4 Beta material on the MS website: http://msdn.microsoft.com/en-us/library/system.tuple(VS.100).aspx

I didn't mean to indicate that this was something we would already have inherited from .NET, only that it was a good idea.

I'm using a List<of Object?> at the moment, which is fine as far as it goes. It just feels as though we should be able to provide a typed Tuple...
Caligari
 
Posts: 33

Re: Tuples

Postby Charles » Thu Jul 09, 2009 12:22 am

I would be happier using List<of dynamic>.

I see on the MS page they have types:
Code: Select all
Tuple<(Of <(T1>)>)
Tuple<(Of <(T1, T2>)>)
Tuple<(Of <(T1, T2, T3>)>)
Tuple<(Of <(T1, T2, T3, T4>)>)
Tuple<(Of <(T1, T2, T3, T4, T5>)>)
Tuple<(Of <(T1, T2, T3, T4, T5, T6>)>)
Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7>)>)
Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>

So they are not so much adding a real tuple type, but simulating one with multiple generic class overloads. We can do the same actually. If someone wants to crank out a Tuple library for Cobra, I think it's doable today.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Tuples

Postby onodera » Sat Jul 18, 2009 12:26 pm

The thing is, F# can transparently nest tuples is they are long enough (that TRest is a tuple) and perform pattern-matching on them. I don't think Cobra has pattern-matching, does it?
onodera
 
Posts: 8

Re: Tuples

Postby Charles » Sun Jul 19, 2009 11:39 am

No it does not.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Tuples

Postby hopscc » Sat Jun 22, 2013 1:56 am

Now that we're pushed up to .Net4.0 which supports tuples is there any interest/intention/thoughts in supporting
Tuple Literals (of mixed types) as a cobra builtin type ( like List, Dict or Set - [], {:}, {} ) ?
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Tuples

Postby Charles » Sat Jun 22, 2013 10:23 am

Random thoughts on the subject:

(1)
I was coding the other day and wrote something like:
def foo
test
cases = [
['aoeu', true],
['asdf', false],
]
for input, expected in cases ...
body
...

The type for "cases" ends up being List<of List<of dynamic>> because ['aoeu', true] is heterogenous. And it works okay, but stronger typing gives better error checking, performance and autocompletion. If ('aoeu', true) gave an inferred tuple type of (String, bool) that would be more productive as the for-loop vars would get specific types.

(2)
What's the syntax for a tuple literal? ...for a tuple type? Python using parens for the literal:
Code: Select all
t = (1, 2, 3)

I know from the experience of using Python that this works well enough.

Python has no syntax for the type since everything is dynamic all the time. But the same syntax could work:
class X

def nextCase as (String, bool)
return ('foo', true)
# or
def nextCase as Tuple<of String, bool>
return ('foo', true)
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Tuples

Postby hopscc » Sun Jun 23, 2013 1:38 am

re 1) To my mind the point of tuples is preserving the exact Types and all the benefits that flow from that

2) Literal Syntax
Yeeeees - following python works and is familiar but against their choice is the possibility of (visual/reading) confusion with braced expressions or comma separated lists ( as in method invocations) ,
IIRC In python theres some unexpected syntactic weirdness to distinguish between a list of expressions vs a tuple ( something really obvious like use of a trailing comma from memory )...

Seems to me that a Tuple is a most like a List (wrt cobra and existing literals) - except it is immutable and its items are strongly individually typed , that seems to suggest some permutation of List literal ([])

In my notes I've been using something like *[] ( which sorta has a relationship to streams as well) or (analogous to the string variants) a single char prefix to delineate from Lists
e.g. t[] (or T[]) - (t/T for tuple)

l = [a,b,c]
# l is a List of gcd type of a,b,c
tupl = *[a,b,c]
# OR
tupl = t[a,b,c]
# tupl is a tuple of 3 items Typed to Types of a,b,c


In above most likely to be constructing using literals anyway

l = [1,"mike",33.2]
# l is a List of (probably Object/dynamic)
t = *[1,"mike",33.2]
t = t[1,"mike",33.2]
# t is a Tuple<int, String, Decimal>


Also theres the idea of a named tuple (or data Structure) where the individual items have names and the tuple items can be tagged and accessed by name as well as position:
here I'm thinking [:] ( or *[:], t[:] ) where the name precedes the value - analogous to Dict/Maps
namedTuple = [id:1, name:'mike',  avTemp:33.2]
assert namedTuple[id] ==1
assert namedTuple[name] =='mike'
assert namedTuple[avTemp] == 33.2


(alternatively the names could be explicitly strings like a Dict/Map<String:??>
nt = ['id':1, 'name':'mike',  'avTemp':33.2]
assert nt['name'] == 'mike'


Hmm assuming most useful/common use is the named tuple variant (??) then perhaps unnamed should also use ':' explicitly
emptyTuple = [:] # what does this mean ??

namedTuple = [ id:1, name:'mike', avTemp:33.2]
assert namedTuple[2] == namedTuple[name] # or assert namedTuple[2] == namedTuple['name']

anonTuple = [:1, :'mike', :33.2]
assert anonTuple[2] == 'mike'


that would allow us to distinguish between List and Tuple without a prefix char on Tuples...

3) Operations
- indexing by position (and name for named Tuples)
- count/length
- Enumerability
- slicing - slicing a Tuple (by position only) gives a subset tuple

t = t[1, 'mike', 33.2]
t1 = t[:1]
assert t1[0]==1 and t1[1] =='mike'
assert t1[0].typeOf == t[0].typeOf and t1[1].typeOf == t[1].typeOf
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Tuples

Postby nerdzero » Mon Jun 24, 2013 8:56 am

In Python, it's the comma that makes a tuple. The parenthesis are only required for an empty tuple or to resolve parsing ambiguity such as when passing an unnamed tuple into a function.

Tuples for me are most useful for returning multiple values from a function. Automatic unpacking is also nice and kind of goes along with that.

class WishThisWorked

def getData as Tuple<of String, DateTime>
return "foo", DateTime.now

def main
data, timeStamp = .getData
print "Created [data] @ [timeStamp.toString]"

Another example:
point = 5.2, 1.3, -9.0
x, y, z = point

I haven't used named tuples very often and tend to prefer dictionaries in those cases, but I can see their appeal.

For reference:
http://docs.python.org/3/library/stdtypes.html#tuple
http://docs.python.org/3/library/collec ... namedtuple
http://docs.python.org/3/tutorial/datas ... -sequences
nerdzero
 
Posts: 286
Location: Chicago, IL

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 89 guests

cron