The future of foreach| or just ...

Bill Baxter dnewsgroup at billbaxter.com
Sun Dec 23 05:42:29 PST 2007


downs wrote:
> Or just do this:
> 
>> module lockstep;
>> import std.stdio;
>>
>> struct _lockstep(T) {
>>   T[] a, b;
>>   int opApply(int delegate(size_t id, ref T, ref T) dg) {
>>     foreach (id, entry; a) if (auto res=dg(id, entry, b[id])) return res;
>>     return 0;
>>   }
>>   int opApply(int delegate(ref T, ref T) dg) {
>>     foreach (id, entry; a) if (auto res=dg(entry, b[id])) return res;
>>     return 0;
>>   }
>> }
>>
>> _lockstep!(T) lockstep(T)(T[] a, T[] b) { _lockstep!(T) res; res.a=a; res.b=b; return res; }
>>
>> void main() {
>>   foreach (entry1, entry2; lockstep([1, 2, 3], [4, 5, 6]))
>>     writefln(entry1, " - ", entry2);
>> }
>>
> 
> :)
> 
>  --downs

Just two arrays downs? C'mon man!  Don't you have a variadic template 
version up your sleeves somewhere?

--bb



More information about the Digitalmars-d mailing list