digitalmars.D.learn - how to iterate over an AA by key-value pair (tuple)?
- Timothee Cour (6/6) Feb 13 2014 how to iterate over an AA by key-value pair (tuple)?
- timotheecour (6/14) Feb 13 2014 is there anything more efficient than this?
- bearophile (5/9) Feb 13 2014 zipping byKey and byValue could be faster, but there's no
- Steven Schveighoffer (8/20) Feb 13 2014 Of course. The a[b] lookup is significant in the complexity, it's
- Timothee Cour (21/47) Feb 13 2014 That's what I was suspecting,
- bearophile (8/10) Feb 14 2014 Some people want those pairs to be tuples. But D lacks built-in
- Meta (5/16) Feb 14 2014 I thought that the type of variadic template arg packs was
- bearophile (6/8) Feb 14 2014 It will not be fixed. Walter is against "fixing" that for
- Jakob Ovrum (4/6) Feb 14 2014 I really want it to happen, but I also want it to happen right.
how to iterate over an AA by key-value pair (tuple)? use case: avoid interrupting UFCS chains, eg: foo.generate_aa.byKeyValue.filter!(a=>a[0].isLower).map!(a=>a[1]) ... of course I could roll my own function but I was wondering if there's already a way.
Feb 13 2014
On Thursday, 13 February 2014 at 23:56:35 UTC, Timothee Cour wrote:how to iterate over an AA by key-value pair (tuple)? use case: avoid interrupting UFCS chains, eg: foo.generate_aa.byKeyValue.filter!(a=>a[0].isLower).map!(a=>a[1]) ... of course I could roll my own function but I was wondering if there's already a way.is there anything more efficient than this? auto byKeyValue(T)(T a)if(isAssociativeArray!T){ return a.byKey.map!(b=>tuple(b, a[b])); }
Feb 13 2014
timotheecour:is there anything more efficient than this? auto byKeyValue(T)(T a)if(isAssociativeArray!T){ return a.byKey.map!(b=>tuple(b, a[b])); }zipping byKey and byValue could be faster, but there's no guarantee its result is correct. Bye, bearophile
Feb 13 2014
On Thu, 13 Feb 2014 19:06:45 -0500, timotheecour <timothee.cour2 gmail.com> wrote:On Thursday, 13 February 2014 at 23:56:35 UTC, Timothee Cour wrote:Of course. The a[b] lookup is significant in the complexity, it's amortized constant, and the constant is not always small. Consider that all keys and values are already stored in structs inside the AA. With low-level access, it would be trivial to make an efficient tuple generator that did not need to lookup values by keys. -Stevehow to iterate over an AA by key-value pair (tuple)? use case: avoid interrupting UFCS chains, eg: foo.generate_aa.byKeyValue.filter!(a=>a[0].isLower).map!(a=>a[1]) ... of course I could roll my own function but I was wondering if there's already a way.is there anything more efficient than this? auto byKeyValue(T)(T a)if(isAssociativeArray!T){ return a.byKey.map!(b=>tuple(b, a[b])); }
Feb 13 2014
On Thu, Feb 13, 2014 at 5:50 PM, Steven Schveighoffer <schveiguy yahoo.com>wrote:On Thu, 13 Feb 2014 19:06:45 -0500, timotheecour <timothee.cour2 gmail.com> wrote: On Thursday, 13 February 2014 at 23:56:35 UTC, Timothee CourThat's what I was suspecting, I guess the code to modify would be here: druntime/import/object.di:455:5 auto byKeyValue(){...} auto byKey() { static struct Result { AARange r; property bool empty() { return _aaRangeEmpty(r); } property ref Key front() { return *cast(Key*)_aaRangeFrontKey(r); } void popFront() { _aaRangePopFront(r); } Result save() { return this; } } return Result(_aaRange(p)); } That seems like a worthy enhancement. If I/someone does it, will it be merged in?wrote:Of course. The a[b] lookup is significant in the complexity, it's amortized constant, and the constant is not always small. Consider that all keys and values are already stored in structs inside the AA. With low-level access, it would be trivial to make an efficient tuple generator that did not need to lookup values by keys. -Stevehow to iterate over an AA by key-value pair (tuple)? use case: avoid interrupting UFCS chains, eg: foo.generate_aa.byKeyValue.filter!(a=>a[0].isLower).map!(a=>a[1]) ... of course I could roll my own function but I was wondering if there's already a way.is there anything more efficient than this? auto byKeyValue(T)(T a)if(isAssociativeArray!T){ return a.byKey.map!(b=>tuple(b, a[b])); }
Feb 13 2014
Timothee Cour:auto byKeyValue(){...}It's probably better to call the method "byPair" that is shorter.If I/someone does it, will it be merged in?Some people want those pairs to be tuples. But D lacks built-in tuples, so you need to use typecons ones. But to use the typecons ones you need to import half Phobos. It seems there is no good solution. Bye, bearophile
Feb 14 2014
On Friday, 14 February 2014 at 09:30:30 UTC, bearophile wrote:Timothee Cour:I thought that the type of variadic template arg packs was considered D's tuple? The only problem is returning them from functions, but if that's fixed, would they not effectively be built-in tuples?auto byKeyValue(){...}It's probably better to call the method "byPair" that is shorter.If I/someone does it, will it be merged in?Some people want those pairs to be tuples. But D lacks built-in tuples, so you need to use typecons ones. But to use the typecons ones you need to import half Phobos. It seems there is no good solution. Bye, bearophile
Feb 14 2014
On Friday, 14 February 2014 at 15:36:30 UTC, Meta: The only problem is returning them fromfunctions, but if that's fixed, would they not effectively be built-in tuples?It will not be fixed. Walter is against "fixing" that for efficiency reasons. Bye, bearophile
Feb 14 2014
On Friday, 14 February 2014 at 07:35:34 UTC, Timothee Cour wrote:That seems like a worthy enhancement. If I/someone does it, will it be merged in?I really want it to happen, but I also want it to happen right. See the relevant pull request[1]. [1] https://github.com/D-Programming-Language/druntime/pull/574
Feb 14 2014