http://qs321.pair.com?node_id=824412


in reply to Re^5: in search of a more elegant if then else
in thread in search of a more elegant if then else

Yes, that's more or less a list comprehension syntax. To make it a bit more concrete, let's suppose you only want unequal chars; you could do it any of these ways in Perl 6:
my @filtered = grep { .[0] ne .[1] }, do [ $0, $1 ] while $s ~~ m:g[(. +)(.)]; my @filtered = do [ $0, $1 ] if $0 ne $1 while $s ~~ m:g[(.)(.)]; my @filtered = ([ $0, $1 ] if $0 ne $1 for $s.comb(/(.)(.)/)); my @filtered = map -> $a, $b {[ $a, $b ] if $a ne $b }, $s.comb;
We get list comprehensions more or less for free in Perl 6 because loops are basically just maps in disguise, and because we allow conditional modifiers inside of looping modifiers. That modifier nesting is something that Perl 5 could easily steal back from Perl 6, even if the loop doesn't automatically return its values.