Super-dee-duper D features

Bill Baxter dnewsgroup at billbaxter.com
Tue Feb 13 19:35:54 PST 2007


Bill Baxter wrote:
>>> --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]);
> }
> 

And here's a partial hypothetical implementation

struct InterleavedIterator(Types...)
{
    alias GetElementTypes!(Types) ElemTypes;

    static InterleavedIterator opCall(Types arg) {
       InterleavedIterator lists = arg;
    }

    int opApply( int delegate(inout ElemTypes) body) {
        ElemTypes x;
        for(uint i=0; i<; i++) {
            foreach(j,inout L; lists) x[j] = L[i];
            int ret = body(x);
            if (ret) return ret;
        }
        return 0;
    }

    Types lists;
}

Obviously it doesn't work right now because you can't do some of those 
things with tuples.  But it would be nice if you could.  We're really 
not too far from something like that working.  Especially when inout 
gets sorted out.

Making tuples more powerful and general, IMHO, would have many benefits.

In the static,strongly typed functional languages like ML and haskell, 
lists and tuples are the core data structures.  They're pretty similar 
to D tuples, except in ML and haskell you can return them from functions 
and otherwise manipulate them as first-class entities.

--bb



More information about the Digitalmars-d mailing list