Repost: make foreach(i, a; range) "just work"
w0rp
devw0rp at gmail.com
Tue Feb 25 00:11:33 PST 2014
On Saturday, 22 February 2014 at 03:51:19 UTC, Jesse Phillips
wrote:
> On Thursday, 20 February 2014 at 19:34:17 UTC, w0rp wrote:
>> I suppose the next step after that would be to
>> support nested unpacking, but that would require a change in
>> syntax so it would be much more complicated.
>
> You mean this?
>
> void main() {
> import std.typecons : tuple;
> import std.range : repeat;
> foreach(k,v1, v2; tuple(1, tuple(2, 3)).repeat(4))
> {}
> }
>
> Yeah, that works.
Ah, I didn't realise you could decompose tuples that way.
I suppose this in Python...
l = [((1, 2), (3, 4, 5))]
for (x, y), (a, b, c) in l:
print (x, y, a, b, c)
... kind of becomes this in D.
auto left = tuple(1, 2);
auto right = tuple(3, 4, 5);
auto both = tuple(left, right);
auto l = [both];
// identity map function to trick the array into being just a
range.
foreach(x, y, a, b, c; l.map!(x => x)) {
writeln(x, y, a, b, c);
}
Then static typing makes you not worry about which thing comes
from which tuple.
More information about the Digitalmars-d
mailing list