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
next sibling 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 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 reply Sam Hu <samhu.samhu gmail.com> writes:
Hi downs,

 A Tuple [...] is a sequence of any mix of types, expressions or symbols.

So is it a class or a type or a variable or something else?The examples in the
D Spec. is used inside a template,so is it just limited to use inside  the
template or can use in a common function or anywhere else?
Thanks,
Sam
Sep 12 2008
parent reply downs <default_357-line yahoo.de> writes:
Sam Hu wrote:
 Hi downs,
 
  A Tuple [...] is a sequence of any mix of types, expressions or symbols.
 
 So is it a class or a type or a variable or something else?The examples in the
D Spec. is used inside a template,so is it just limited to use inside  the
template or can use in a common function or anywhere else?
 Thanks,
 Sam
It's a tuple; i.e. neither of the above. And now you'll ask "What is a tuple?" And the answer is still, a sequence of types, expressions or symbols. :p
Sep 12 2008
parent reply downs <default_357-line yahoo.de> writes:
downs wrote:
 Sam Hu wrote:
 Hi downs,

  A Tuple [...] is a sequence of any mix of types, expressions or symbols.

 So is it a class or a type or a variable or something else?The examples in the
D Spec. is used inside a template,so is it just limited to use inside  the
template or can use in a common function or anywhere else?
 Thanks,
 Sam
It's a tuple; i.e. neither of the above. And now you'll ask "What is a tuple?" And the answer is still, a sequence of types, expressions or symbols. :p
To pre-empt further questions; they appear in relation with templates because that's one of the most common ways to form type tuples: template Tuple(T...) { alias T Tuple; } In this case, most tuples formed this way will be type tuples, i.e. sequences of types. For another example, the ".tupleof" expression of, say, a struct or a class evaluates to a tuple (sequence) of the values of the tuple's members. The typeof([struct tuple]) is again a type tuple. Look at it like this: a [X] tuple is a list, or sequence, of [X]. A type tuple is a list of types. A value tuple is a list of values. Example (untested): template Tuple(T...) { alias T Tuple; } int add(int a, int b) { return a + b; } struct Pair(T) { T a, b; } void main() { Pair!(int) p; p.a = 2; p.b = 2; static assert(is(typeof(p.tupleof) == Tuple!(int, int))); // and a value tuple is a list of values assert(4 == add(p.tupleof)); // == assert(4 == add(/* just a list of values */ 2, 2)) }
Sep 12 2008
parent reply downs <default_357-line yahoo.de> writes:
downs wrote:
 downs wrote:
 Sam Hu wrote:
 Hi downs,

  A Tuple [...] is a sequence of any mix of types, expressions or symbols.

 So is it a class or a type or a variable or something else?The examples in the
D Spec. is used inside a template,so is it just limited to use inside  the
template or can use in a common function or anywhere else?
 Thanks,
 Sam
It's a tuple; i.e. neither of the above. And now you'll ask "What is a tuple?" And the answer is still, a sequence of types, expressions or symbols. :p
To pre-empt further questions; they appear in relation with templates because that's one of the most common ways to form type tuples: template Tuple(T...) { alias T Tuple; } In this case, most tuples formed this way will be type tuples, i.e. sequences of types. For another example, the ".tupleof" expression of, say, a struct or a class evaluates to a tuple (sequence) of the values of the tuple's members.
Er, the struct's members.
 
 The typeof([struct tuple]) is again a type tuple.
 
 Look at it like this: a [X] tuple is a list, or sequence, of [X].
 
 A type tuple is a list of types. A value tuple is a list of values.
 
 Example (untested):
 
 template Tuple(T...) { alias T Tuple; }
 
 int add(int a, int b) { return a + b; }
 
 struct Pair(T) { T a, b; }
 
 void main() {
   Pair!(int) p; p.a = 2; p.b = 2;
   static assert(is(typeof(p.tupleof) == Tuple!(int, int)));
   // and a value tuple is a list of values
   assert(4 == add(p.tupleof)); // == assert(4 == add(/* just a list of values
*/ 2, 2))
 }
Something else to keep in mind: A tuple has no "binding power" of its own. Some people get confused when they compare LISP tuples and D tuples. It is important to keep in mind that a tuple is not a single expression. For instance: Tuple!(Tuple!(int, int), int) is _EXACTLY_ the same thing as Tuple!(int, int, int) I actually think this might be why the D specs use the term "sequence", and not "list". A list suggests a distinct element. A sequence is just some things following one another.
Sep 12 2008
parent Sam Hu <samhu.samhu gmail.com> writes:
Hi downs,

Thanks so much for your further explanations although I am COMPLETELY confused
at this moment,not becoz your explanation,but my stupid I think,-:)

Sam
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