www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Tuple basics

reply Sam Hu <samhu.samhu gmail.com> writes:
Hi,

I seached questions regarding Tuple in this newsgroup but did not find the the
same question I have.Since I am a newbie ,they are too advanced to me at this
moment.My question is just :What is tuple?what can tuple do in D while  other
languae like c++ can not do or can not do well?Could somebody please post a
very simple code segment and an simple but enough explanation so that I can
understand what the hell a tuple is .

Thank,
Sam
Sep 12 2008
next sibling parent reply "Bill Baxter" <wbaxter gmail.com> writes:
Did you read this already?
http://www.digitalmars.com/d/1.0/template.html#TemplateTupleParameter

--bb

On Sat, Sep 13, 2008 at 10:34 AM, Sam Hu <samhu.samhu gmail.com> wrote:
 Hi,

 I seached questions regarding Tuple in this newsgroup but did not find the the
same question I have.Since I am a newbie ,they are too advanced to me at this
moment.My question is just :What is tuple?what can tuple do in D while  other
languae like c++ can not do or can not do well?Could somebody please post a
very simple code segment and an simple but enough explanation so that I can
understand what the hell a tuple is .

 Thank,
 Sam

Sep 12 2008
parent reply Sam Hu <samhu.samhu gmail.com> writes:
Thanks BB,

Acctually I have a copy of D spec. but still don't understand the section of
Tuple.As you know not so many people using or studying D,esp in my country,so
it seems that here is the best place to raise question-:)

Sam
Sep 12 2008
parent downs <default_357-line yahoo.de> writes:
Sam Hu wrote:
 Thanks BB,
 
 Acctually I have a copy of D spec. but still don't understand the section of
Tuple.As you know not so many people using or studying D,esp in my country,so
it seems that here is the best place to raise question-:)
 
 Sam

A Tuple [...] is a sequence of any mix of types, expressions or symbols. What is unclear about that?
Sep 12 2008
prev sibling next sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
On Sat, Sep 13, 2008 at 11:50 AM, Sam Hu <samhu.samhu gmail.com> wrote:
 Thanks BB,

 Acctually I have a copy of D spec. but still don't understand the section of
Tuple.As you know not so many people using or studying D,esp in my country,so
it seems that here is the best place to raise question-:)

No problem. I just asked because often it is hard to find the relevant sections in the spec. No point in re-explaining all that if you problem was just that you couldn't find the doc. But anyway, knowing you've read that, and didn't get it, gives us a better idea what your current level of understanding is. I'll try to explain the basics. [Though I got distracted and downs jumped in in the mean time... but here's what I wrote anyway] The starting point is variadic templates. Often it is useful to write a template that takes a variable number of arguments. In D you can write this: void TupleTaker(alias fn, Tup...)(Tup theTuple) { // do some logging or something // now call the function with supplied args fn(theTuple); } You can call that like this: TupleTaker!(writefln)("hi there customer %s, I'm %s", 32, "Bob"); And then it's just like you called writefln directly. But you could put any number of args there. So that's maybe the biggest use of Tuples. Forwarding args to functions, and saving argument lists to call functions later. Inside TupleTaker, Tup is a *type* tuple: - Tup[0] is the type of the first arg passed in (string) - Tup[1] is the type of the second (int), - Tup[2] is string again. theTuple is a *value* tuple. It consists of one item of each type in the type tuple - theTuple[0] is "hi there customer %s" - theTuple[1] is 32 - theTuple[2] is "Bob" In some ways a tuple is kind of like an anonymous struct, in that it contains various elements of various types. However you access the parts of a tuple by index rather than by name. In fact you can make a tuple out of a struct by using .tupleof, as Downs pointed out. That means you can call an N argument function using an N member struct like so: n_arg_function(my_n_arg_struct.tupleof); ---bb
Sep 12 2008
prev sibling next sibling parent Benji Smith <dlanguage benjismith.net> writes:
Sam Hu wrote:
 Hi,
 
 I seached questions regarding Tuple in this newsgroup but did not find the the
same question I have.Since I am a newbie ,they are too advanced to me at this
moment.My question is just :What is tuple?what can tuple do in D while  other
languae like c++ can not do or can not do well?Could somebody please post a
very simple code segment and an simple but enough explanation so that I can
understand what the hell a tuple is .
 
 Thank,
 Sam

The way I understand it (which could be totally wrong; others can correct me) is that a tuple is basically just an anonymous struct, at least, from an implementation standpoint. The fancy thing about tuples (which differentiates them from other structs) is that you can create a tuple of types, and evaluate the contents of that tuple at compile-time, within template declarations. One important thing to keep in mind is that you *can't* return a tuple from a function: public Tuple!(int, int) myFunction() { ... } (At least, I think that's true. But again, I could be wrong.) --benji
Sep 13 2008
prev sibling next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Fri, 12 Sep 2008 21:34:25 -0400, Sam Hu wrote:

 Hi,
 
 I seached questions regarding Tuple in this newsgroup but did not find the the
same question I have.Since I am a newbie ,they are too advanced to me at this
moment.My question is just :What is tuple?what can tuple do in D while  other
languae like c++ can not do or can not do well?Could somebody please post a
very simple code segment and an simple but enough explanation so that I can
understand what the hell a tuple is .

I think that in D, a tuple is a lexical construct and not a run-time construct. This means that it only exists while the application is being complied and once compiled a tuple has no run-time representation. It is an aid to writing generic code because it allows you to specify a list/se/sequence of Types that you can address indivually as if it was an array of types. They are Types and not Data values. Of course, I could be totally wrong as I haven't needed to use them yet. -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Sep 13 2008
parent downs <default_357-line yahoo.de> writes:
Derek Parnell wrote:
 On Fri, 12 Sep 2008 21:34:25 -0400, Sam Hu wrote:
 
 Hi,

 I seached questions regarding Tuple in this newsgroup but did not find the the
same question I have.Since I am a newbie ,they are too advanced to me at this
moment.My question is just :What is tuple?what can tuple do in D while  other
languae like c++ can not do or can not do well?Could somebody please post a
very simple code segment and an simple but enough explanation so that I can
understand what the hell a tuple is .

I think that in D, a tuple is a lexical construct and not a run-time construct. This means that it only exists while the application is being complied and once compiled a tuple has no run-time representation. It is an aid to writing generic code because it allows you to specify a list/se/sequence of Types that you can address indivually as if it was an array of types. They are Types and not Data values.

A list of anything, actually. Types, compile-time values, expressions, or any mix of the previous. Tuples of the same kind are the most common though. (for instance, struct_var.tupleof is a run-time value tuple :)
 
 Of course, I could be totally wrong as I haven't needed to use them yet.
 

Sep 13 2008
prev sibling parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Sam Hu <samhu.samhu gmail.com> wrote:
 I seached questions regarding Tuple in this newsgroup but did not find 
 the the same question I have.Since I am a newbie ,they are too advanced 
 to me at this moment.My question is just :What is tuple?what can tuple 
 do in D while  other languae like c++ can not do or can not do well?
 Could somebody please post a very simple code segment and an simple but 
 enough explanation so that I can understand what the hell a tuple is .

Many already tried to explain, let me try, too. :) I'll put it this way. When you write a regular program, you pass values to functions. Sometimes you want to pass many values as a single entity. Then you use lists/arrays/structs/classes. When you write a template, you pass types to other templates. Sometimes you want to pass many types as a single entity. Then you use tuples. Because templates can have value arguments, tuples can contain compile- time values, too. Usually you create tuples with a special template syntax, when you say you want to get a tuple instead of individual template agruments. Very much like in vararg function you receive all arguments in a sort of list instead of as individual entities. There are also language constructs or library functions which allow you to create tuples of struct types, struct values, function argument types etc. They're all intended for passing as template arguments at compile time. There's a special case, or trick, that if you specify a tuple which contains only compile-time values as an argument to a regular function, this is the same as if you specified all those values as separate arguments to that function. All this is quite hard to get a grip on if you don't actually need and write complex template code, so you can safely ignore all that tuple stuff.
Sep 13 2008
parent Sam Hu <samhu.samhu gmail.com> writes:
Thanks downs,Bill Baxter,Benji Simith,Derek Parnell and Sergey Gromov

So many ppls here tried to help me ,really appreciated!!!As during the past 2
days it is the traditional Chinese holiday:Mid-Autumn Festival,I am not
online,sorry for the late reply to all of you.

All of your explanations are helpful to me and Sergey Gromov's is the very good
start point to me,with his explanation,I think the door is opened -:),and now I
think  I can catch ,very slowly,the deeper one from BS,DP,BB and downs.

Finally I found the cpu in my own mind is so slow and is hard to turn
around,sad~~

Thanks you all and regards!
Sam
Sep 15 2008