Jump to content

List comprehensions: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Swift}}: Maybe prefix with a statement that the language does not have LCs but the result maybe computed by...)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 166:
+3 +4 +5 +5 +12 +13 +6 +8 +10 +8 +15 +17 +9 +12 +15 +12 +16 +20
</pre></div>
 
 
=={{header|AppleScript}}==
Line 1,665 ⟶ 1,664:
print "@$t\n";
}</lang>
 
=={{header|Perl 6}}==
Perl 6 has single-dimensional list comprehensions that fall out naturally from nested modifiers; multidimensional comprehensions are also supported via the cross operator; however, Perl&nbsp;6 does not (yet) support multi-dimensional list comprehensions with dependencies between the lists, so the most straightforward way is currently:
<lang perl6>my $n = 20;
gather for 1..$n -> $x {
for $x..$n -> $y {
for $y..$n -> $z {
take $x,$y,$z if $x*$x + $y*$y == $z*$z;
}
}
}</lang>
 
Note that <tt>gather</tt>/<tt>take</tt> is the primitive in Perl&nbsp;6 corresponding to generators or coroutines in other languages. It is not, however, tied to function call syntax in Perl&nbsp;6. We can get away with that because lists are lazy, and the demand for more of the list is implicit; it does not need to be driven by function calls.
 
=={{header|Phix}}==
Line 1,989 ⟶ 1,975:
(list x y z))
</lang>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
Perl 6 has single-dimensional list comprehensions that fall out naturally from nested modifiers; multidimensional comprehensions are also supported via the cross operator; however, Perl&nbsp;6 does not (yet) support multi-dimensional list comprehensions with dependencies between the lists, so the most straightforward way is currently:
<lang perl6>my $n = 20;
gather for 1..$n -> $x {
for $x..$n -> $y {
for $y..$n -> $z {
take $x,$y,$z if $x*$x + $y*$y == $z*$z;
}
}
}</lang>
 
Note that <tt>gather</tt>/<tt>take</tt> is the primitive in Perl&nbsp;6 corresponding to generators or coroutines in other languages. It is not, however, tied to function call syntax in Perl&nbsp;6. We can get away with that because lists are lazy, and the demand for more of the list is implicit; it does not need to be driven by function calls.
 
=={{header|Rascal}}==
Line 2,304:
returns
<lang supercollider>[ [ 3, 4, 5 ], [ 5, 12, 13 ], [ 6, 8, 10 ], [ 8, 15, 17 ], [ 9, 12, 15 ], [ 12, 16, 20 ] ]</lang>
 
 
=={{header|Swift}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.