digitalmars.D - indexing tuples using strings
- llee (2/2) Dec 01 2008 Is is possible to index the elements within a tuple using strings?
- Jarrett Billingsley (3/5) Dec 01 2008 No, because that makes no sense.
- Bill Baxter (10/16) Dec 01 2008 Tuples have an annoying "auto-flattening" behavior which means that
- Jason House (2/22) Dec 01 2008 ... Or you can just loop over the tuple and return the matching index......
- BCS (21/49) Dec 01 2008 you can't loop over a template outside of a function.
- Sergey Gromov (2/24) Dec 04 2008 Master!
- bearophile (5/7) Dec 01 2008 This has to change in D2. Perl shows why that's bad. To replace all the ...
- Bill Baxter (13/19) Dec 01 2008 Yeh, probably this is the idea that comes to anyone familiar with
- BCS (5/16) Dec 01 2008 Yes, that works well. I make lots of use of that in dparse. I end up wit...
- Bill Baxter (56/72) Dec 01 2008 I thought I'd seen it somewhere before. But I couldn't resist a
- bearophile (14/16) Dec 02 2008 You can find a note here:
- Bill Baxter (16/29) Dec 02 2008 Wow, I guess it really has been a long time for me and Perl. Don't
- bearophile (5/6) Dec 02 2008 I presume my version is "better" :-)
- Bill Baxter (7/11) Dec 02 2008 I'm sure if you have spent that much time on your lib that it can do
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
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
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: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. --bbIs 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
Bill Baxter Wrote:On Tue, Dec 2, 2008 at 6:40 AM, Jarrett Billingsley <jarrett.billingsley gmail.com> wrote:... Or you can just loop over the tuple and return the matching index... No recursive template-foo neededOn Mon, Dec 1, 2008 at 4:19 PM, llee <larry workingwondersus.com> wrote: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. --bbIs 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
Reply to Jason,Bill Baxter Wrote: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!!!On Tue, Dec 2, 2008 at 6:40 AM, Jarrett Billingsley <jarrett.billingsley gmail.com> wrote:... Or you can just loop over the tuple and return the matching index... No recursive template-foo neededOn Mon, Dec 1, 2008 at 4:19 PM, llee <larry workingwondersus.com> wrote: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. --bbIs 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
Mon, 1 Dec 2008 22:49:24 +0000 (UTC), BCS wrote:Reply to Jason,Master!No recursive template-foo neededtemplate 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")); }
Dec 04 2008
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
On Tue, Dec 2, 2008 at 9:39 AM, bearophile <bearophileHUGS lycos.com> wrote:Bill Baxter:My Perl is rusty. What about Perl shows that auto-flattening is bad?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).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 thatTime 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
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. :-) --bbYes, 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
On Tue, Dec 2, 2008 at 10:14 AM, BCS <ao pathlink.com> wrote:Reply to Bill,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)) """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. :-) --bbYes, 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
Bill Baxter:What about Perl shows that auto-flattening is bad?<You can find a note here: http://steve.yegge.googlepages.com/ancient-languages-perlLarry 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
On Tue, Dec 2, 2008 at 7:26 PM, bearophile <bearophileHUGS lycos.com> wrote:Bill Baxter:Wow, I guess it really has been a long time for me and Perl. Don't recall that little quirk at all.What about Perl shows that auto-flattening is bad?<You can find a note here: http://steve.yegge.googlepages.com/ancient-languages-perlLarry 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). <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).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.) --bbIsn'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
Dec 02 2008
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
On Tue, Dec 2, 2008 at 10:01 PM, bearophile <bearophileHUGS lycos.com> wrote:Bill Baxter: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. --bband I implemented it again this morning.I presume my version is "better" :-) I have written it along one year of time.
Dec 02 2008