www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - indexing tuples using strings

reply llee <larry workingwondersus.com> writes:
Is is possible to index the elements within a tuple using strings?
Something similar to the way that associative arrays allow elements to be
indexed using strings.
Dec 01 2008
next sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Mon, Dec 1, 2008 at 4:19 PM, llee <larry workingwondersus.com> wrote:
 Is is possible to index the elements within a tuple using strings?
 Something similar to the way that associative arrays allow elements to be
indexed using strings.
No, because that makes no sense. What does tuple["string"] mean, anyway?
Dec 01 2008
prev sibling parent reply "Bill Baxter" <wbaxter gmail.com> writes:
On Tue, Dec 2, 2008 at 6:40 AM, Jarrett Billingsley
<jarrett.billingsley gmail.com> wrote:
 On Mon, Dec 1, 2008 at 4:19 PM, llee <larry workingwondersus.com> wrote:
 Is is possible to index the elements within a tuple using strings?
 Something similar to the way that associative arrays allow elements to be
indexed using strings.
No, because that makes no sense. What does tuple["string"] mean, anyway?
Tuples have an annoying "auto-flattening" behavior which means that it's difficult to create very advanced data structures out of them. But you can create a flat tuple a-list with entries like "fred", int, "barney", double, "wilma", ireal Then you can write some template functions that will scan through the list for a particular string and return the following type if there's a match. You will need to use lots of recursive template-fu. --bb
Dec 01 2008
next sibling parent reply Jason House <jason.james.house gmail.com> writes:
Bill Baxter Wrote:

 On Tue, Dec 2, 2008 at 6:40 AM, Jarrett Billingsley
 <jarrett.billingsley gmail.com> wrote:
 On Mon, Dec 1, 2008 at 4:19 PM, llee <larry workingwondersus.com> wrote:
 Is is possible to index the elements within a tuple using strings?
 Something similar to the way that associative arrays allow elements to be
indexed using strings.
No, because that makes no sense. What does tuple["string"] mean, anyway?
Tuples have an annoying "auto-flattening" behavior which means that it's difficult to create very advanced data structures out of them. But you can create a flat tuple a-list with entries like "fred", int, "barney", double, "wilma", ireal Then you can write some template functions that will scan through the list for a particular string and return the following type if there's a match. You will need to use lots of recursive template-fu. --bb
... Or you can just loop over the tuple and return the matching index... No recursive template-foo needed
Dec 01 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Jason,

 Bill Baxter Wrote:
 
 On Tue, Dec 2, 2008 at 6:40 AM, Jarrett Billingsley
 <jarrett.billingsley gmail.com> wrote:
 
 On Mon, Dec 1, 2008 at 4:19 PM, llee <larry workingwondersus.com>
 wrote:
 
 Is is possible to index the elements within a tuple using strings?
 Something similar to the way that associative arrays allow elements
 to be indexed using strings.
 
No, because that makes no sense. What does tuple["string"] mean, anyway?
Tuples have an annoying "auto-flattening" behavior which means that it's difficult to create very advanced data structures out of them. But you can create a flat tuple a-list with entries like "fred", int, "barney", double, "wilma", ireal Then you can write some template functions that will scan through the list for a particular string and return the following type if there's a match. You will need to use lots of recursive template-fu. --bb
... Or you can just loop over the tuple and return the matching index... No recursive template-foo needed
you can't loop over a template outside of a function. OTOH: template IndexOf(char[] str, Strs...) { const int IndexOf = typeof( *{ foreach(int i, char[] s; Strs) { static char[i] r; static if(Strs[i] == str) return &r; } }()).length; } import std.stdio; void main() { writef("%d\n", IndexOf!("hello", "go", "say", "hello", "to", "bob")); } But Gahh!!!
Dec 01 2008
parent Sergey Gromov <snake.scaly gmail.com> writes:
Mon, 1 Dec 2008 22:49:24 +0000 (UTC), BCS wrote:

 Reply to Jason,
 
 No recursive template-foo needed
template IndexOf(char[] str, Strs...) { const int IndexOf = typeof( *{ foreach(int i, char[] s; Strs) { static char[i] r; static if(Strs[i] == str) return &r; } }()).length; } import std.stdio; void main() { writef("%d\n", IndexOf!("hello", "go", "say", "hello", "to", "bob")); }
Master!
Dec 04 2008
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Bill Baxter:
 Tuples have an annoying "auto-flattening" behavior which means that
 it's difficult to create very advanced data structures out of them.
This has to change in D2. Perl shows why that's bad. To replace all the current semantics you can just to add a syntax that performs as an 'apply' (Python uses a * for this, but D probably needs a different syntax). Time ago I have created some machinery to represent arbitrarily nested "arrays" into a tuple, using an encoding that can represent all the lengths of the sub-sub-sub-etc "arrays", but it's complex, and probably uses too much resources during compile-time, so something more native is better. Bye, bearophile
Dec 01 2008
parent reply "Bill Baxter" <wbaxter gmail.com> writes:
On Tue, Dec 2, 2008 at 9:39 AM, bearophile <bearophileHUGS lycos.com> wrote:
 Bill Baxter:
 Tuples have an annoying "auto-flattening" behavior which means that
 it's difficult to create very advanced data structures out of them.
This has to change in D2. Perl shows why that's bad.
My Perl is rusty. What about Perl shows that auto-flattening is bad?
 To replace all the current semantics you can just to add a syntax that
performs as an 'apply' (Python uses a * for this, but D probably needs a
different syntax).
Yeh, probably this is the idea that comes to anyone familiar with Python. Preserve the packaging of a tuple by default, require some op to "explode" the contents back out into a list. But then that
 Time ago I have created some machinery to represent arbitrarily nested
"arrays" into a tuple, using an encoding that can represent all the lengths of
the sub-sub-sub-etc "arrays", but it's complex, and probably uses too much
resources during compile-time, so something more native is better.
Isn't there an easier way to fake it using structs? Like template Tuple(T...) { alias T Tuple } struct STuple(T...) { alias T tuple; } alias Tuple!(STuple(int,float), STuple(string,double), int, creal) CantFlattenThis; And you can write flatteners and nesters for that. Seems like fun, maybe I'll waste a few minutes working on that. :-) --bb
Dec 01 2008
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Bill,

 Isn't there an easier way to fake it using structs? Like
 template Tuple(T...) { alias T Tuple }
 struct STuple(T...) { alias T tuple; }
 alias Tuple!(STuple(int,float), STuple(string,double), int, creal)
 CantFlattenThis;
 
 And you can write flatteners and nesters for that. Seems like fun,
 maybe I'll waste a few minutes working on that. :-)
 
 --bb
 
Yes, that works well. I make lots of use of that in dparse. I end up with a struct type that contains the whole AST defining the grammar to be parsed. OTOH there is issues (bugs) with aliases when you try to access it under some conditions.
Dec 01 2008
parent "Bill Baxter" <wbaxter gmail.com> writes:
On Tue, Dec 2, 2008 at 10:14 AM, BCS <ao pathlink.com> wrote:
 Reply to Bill,

 Isn't there an easier way to fake it using structs? Like
 template Tuple(T...) { alias T Tuple }
 struct STuple(T...) { alias T tuple; }
 alias Tuple!(STuple(int,float), STuple(string,double), int, creal)
 CantFlattenThis;

 And you can write flatteners and nesters for that. Seems like fun,
 maybe I'll waste a few minutes working on that. :-)

 --bb
Yes, that works well. I make lots of use of that in dparse. I end up with a struct type that contains the whole AST defining the grammar to be parsed. OTOH there is issues (bugs) with aliases when you try to access it under some conditions.
I thought I'd seen it somewhere before. But I couldn't resist a little early morning tuple coding challenge. So here it is. template Tuple(T...) { alias T Tuple; } struct STuple(T...) { alias T tuple; } /** This helper is necessary because the check is(T[0].tuple) gives a compiler error */ template Flatten1(T) { static if (is(T.tuple)) { alias T.tuple Flatten1; } else { alias T Flatten1; } } /// Remove STuples from the tuple T template Flatten(T...) { static if (T.length == 0) { alias T Flatten; } else static if (T.length == 1) { alias Flatten1!(T[0]) Flatten; } else { alias Tuple!(Flatten!(T[0]),Flatten!(T[1..$])) Flatten; } } /// Nest each element of T in a STuple template Nest(T...) { static if (T.length == 0) { alias T Nest; } else static if (T.length == 1) { alias STuple!(T) Nest; } else { alias Tuple!(STuple!(T[0]), Nest!(T[1..$])) Nest; } } void main() { alias Tuple!(STuple!(int,float), STuple!(string,double), int, creal) X; pragma(msg,X.stringof); alias Flatten!(X) FlatX; pragma(msg,FlatX.stringof); alias Nest!(FlatX) NestFlatX; pragma(msg, NestFlatX.stringof); } And the output: """ (STuple!(int,float), STuple!(char[],double), int, creal) (int, float, char[], double, int, creal) (STuple!(int), STuple!(float), STuple!(char[]), STuple!(double), STuple!(int), STuple!(creal)) """
Dec 01 2008
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Bill Baxter:

What about Perl shows that auto-flattening is bad?<
You can find a note here: http://steve.yegge.googlepages.com/ancient-languages-perl

Larry decided to flatten lists by default. Hence, if you write this:
 x = (1, 2, 3, (4, 5));
It automagically turns into (1, 2, 3, 4, 5).
<

With Google you can find many other pages that about that anti-feature.
The new version of Perl, that is in development from some years, fixes this, I
think.

The ability of creating trees with arbitrary nested tuples/arrays is really
handy (I don't know if the variant type of Tango allows to do this. The boxed
of Phobos doesn't allow it, I think).


Isn't there an easier way to fake it using structs?<
In my dlibs I've added a Record/record that can be used a little as a Python tuple, the Record can be found here: http://www.fantascienza.net/leonardo/so/dlibs/templates.html Bye, bearophile
Dec 02 2008
parent reply "Bill Baxter" <wbaxter gmail.com> writes:
On Tue, Dec 2, 2008 at 7:26 PM, bearophile <bearophileHUGS lycos.com> wrote:
 Bill Baxter:

What about Perl shows that auto-flattening is bad?<
You can find a note here: http://steve.yegge.googlepages.com/ancient-languages-perl

 Larry decided to flatten lists by default. Hence, if you write this:
  x = (1, 2, 3, (4, 5));
 It automagically turns into (1, 2, 3, 4, 5).
 <
Wow, I guess it really has been a long time for me and Perl. Don't recall that little quirk at all.
 The ability of creating trees with arbitrary nested tuples/arrays is really
handy (I don't know if the variant type of Tango allows to do this. The boxed
of Phobos doesn't allow it, I think).

Isn't there an easier way to fake it using structs?<
In my dlibs I've added a Record/record that can be used a little as a Python tuple, the Record can be found here: http://www.fantascienza.net/leonardo/so/dlibs/templates.html
Hmm, but just to play devils advocate here, you've implemented it, BCS implemented, and I implemented it again this morning. That would seem to argue that the current state is fine, the unflattenable tuple type just needs to be in the standard lib(s) so people don't have to keep rolling their own. (Personally I agree non-flattening would be better and argued for it soon after Tuples came out, but somehow I doubt Walter's going to be convinced to break a significant amount of Tuple-using template code without a more ... convincing argument. Something like a demonstration of some of BCS's compile-time parser written with and without auto-flattening tuples showing a dramatic reduction in code for the no-flattening case.) --bb
Dec 02 2008
parent reply bearophile <bearophileHUGS lycos.com> writes:
Bill Baxter:
 and I implemented it again this morning.
I presume my version is "better" :-) I have written it along one year of time. Bye, bearophile
Dec 02 2008
parent "Bill Baxter" <wbaxter gmail.com> writes:
On Tue, Dec 2, 2008 at 10:01 PM, bearophile <bearophileHUGS lycos.com> wrote:
 Bill Baxter:
 and I implemented it again this morning.
I presume my version is "better" :-) I have written it along one year of time.
I'm sure if you have spent that much time on your lib that it can do far more than flatten and unflatten a tuple. But I'm just talking about unflattenable tuples. I can't imagine that your version of the flatten/unflatten routines are that much different from what I wrote. There's only so many ways to skin that cat. --bb
Dec 02 2008