<div class="gmail_quote">On Sun, Nov 8, 2009 at 21:14, dsimcha<span dir="ltr"></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">
> function like in python. Which does something like<br>
> zip(iota(1,bar.length),bar).<br>
<br>
</div>> I think the index variant would better be done as an enumerate()<br>Enumerate is a great idea. It's probably much better than requiring every range<br>
struct to mix something in to enable this behavior. Thanks.<br>
<br></blockquote><div>FWIW, I coded enumerate and such for D2, and will post them on dsource as soon as I can get my hands on svn. In a few days at most, I hope.<br><br>Personally, I then use a 'tuplify' template which takes a standard function and transforms it into a tuple-accepting one:<br>
so if I have:<br><br>int foo(int a, double b, string c) {...}<br>tuplify!foo is :<br>int tfoo(Tuple!(int, double, string) t) { /* extracts a,b & c from t, and returns foo(a,b,c) */ }<br><br><br>so, as I can't unpack the tuple:<br>
<br>foreach(a,b,c ; zip(range1, range2, range3) {<br> /* do something with foo on (a,b,c) */<br>}<br><br>I tuplify foo and do:<br><br>foreach (tup; zip(range1, range2, range3) {<br> /* do something with tuplify!foo(tup) */<br>
}<br>There is no real limit on the number of ranges that can be acted upon in parallel that way, though I admit the syntax is a bit cumbersome.<br><br>I also use unzip!index( someZip) to get back the original range from inside zip.<br>
<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I guess the same thing could be applied to unpack(). Instead of making the range<br>
implementer mixin something to enable this (he/she will probably forget and it<br>
could lead to ambiguities), do it on the caller end.<br>
<br>
Makes me wonder why noone thought of this until now, or maybe someone did and I<br>
forgot. How's:<br>
<br>
foreach(fooElem, barElem; unpack(zip(foo, bar))) {}, or:<br>
<br>
foreach(i, elem; enumerate(chain(foo, bar))) {} ?<br>
<br>
</blockquote></div><br>Can that be done for more than two ranges? The syntax is so nice, I'd be deligthed to have that.<br><br><br> Philippe<br><br>