Super-dee-duper D features

Bill Baxter dnewsgroup at billbaxter.com
Tue Feb 13 18:58:37 PST 2007


kris wrote:
> Bill Baxter wrote:
>> kris wrote:
>>
>>> Bill Baxter wrote:
>>>
>>>> kris wrote:
>>>>
>>>>> kris wrote:
>>>>>
>>>>>> Bill Baxter wrote:
>>>>>>
>>>>>>> Frits van Bommel wrote:
>>>>>>>
>>>>>>>> By the way, would the new loop syntax allow more than two 
>>>>>>>> collections to be simultaneously iterated?
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Whoa!  I certainly hope so.  It hadn't even occurred to me that 
>>>>>>> Andrei might mean this syntax can only be used for just two 
>>>>>>> collections.  If that's the case then ... ick.
>>>>>>>
>>>>>>> --bb
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> InterleavedIterator does multiple collections via using multiple 
>>>>>> instances of InterleavedIterator. It's simple to use, and only 
>>>>>> needs to be written once. Would be better to implement some basic 
>>>>>> iterator needs than to introduce some tricky new syntax?
>>>>>>
>>>>>> - Kris
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Should have given an example. Simple case with 2 entities:
>>>>>
>>>>> auto two = InterleavedInterator (x, y);
>>>>> foreach (x; two) ...
>>>>>
>>>>> more than 2:
>>>>>
>>>>> auto three = InterleavedInterator (two, z);
>>>>> foreach (x; three) ...
>>>>
>>>>
>>>>
>>>> Should have also mentioned where one can find this mythical 
>>>> InterleavedIterator.
>>>>
>>>> --bb
>>>
>>>
>>> There is no 'standard' one at this time that I know of (judging by 
>>> the discussion on it a while back). However, Tango does have this 
>>> beastie in the collections package. The point is, coming up with a 
>>> lightweight core Iterator approach would likely provide a simpler and 
>>> more dependable solution.
>>
>>
>> Ok that wasn't clear to me.  It sounded like you were talking about 
>> code I could type in today and have it work given suitable (but not 
>> specified) imports.
>>
>>> in the above example, x, y, and z are all iterators themselves. If D 
>>> had a core notion of Iterator, that's what those would be. For 
>>> instance, D iterators might map to a delegate (which is what the body 
>>> of a foreach actually is).
>>
>>
>> Yeh, basically it's the same as the Python izip that mentioned.  
>> That's python's name for InterleavedIterator.
>>
>> I think the issue with D right now is that the 'x' returned by a 
>> hypothetical InterleavedIterator would ideally be a tuple.  And you 
>> would access the elements with x[0],x[1],x[2] (int the 'three' case 
>> above).  Or you could do  foreach(x,y,z; three) and have it unpacked 
>> for you.
>>
>> I think it would be great if this kind of stuff worked.  I'm much less 
>> excited about a built-in syntax that _only_ knows how to do that one 
>> trick.
>>
>> --bb
> 
> 
> Why would it return a tuple? Would the collection content be of 
> differing types? If not, then the InterleavedIterator would likely have 
> an opApply() for use in the foreach? That's how the Tango one operates, 
> fwiw.


I must not understand what your InterleavedIterator does then.  I'm 
thinking of something like:
char[][] names = ["chuck", "barney", "bart"];
int[] ids = [12983, 32345, 39284];

foreach (x; InterleavedIterator(names,ids)) {
    writefln("Name=%s id=%s", x[0], x[1]);
}

--bb



More information about the Digitalmars-d mailing list