Jump to content

List comprehensions: Difference between revisions

(Remove outdated and no longer supported OCaml solutions)
(→‎{{header|Raku}}: add output)
Line 2,067:
Raku has single-dimensional list comprehensions that fall out naturally from nested modifiers; multidimensional comprehensions are also supported via the cross operator; however, Raku does not (yet) support multi-dimensional list comprehensions with dependencies between the lists, so the most straightforward way is currently:
<syntaxhighlight lang="raku" line>my $n = 20;
say gather for 1..$n -> $x {
for $x..$n -> $y {
for $y..$n -> $z {
Line 2,074:
}
}</syntaxhighlight>
{{out}}
<pre>((3 4 5) (5 12 13) (6 8 10) (8 15 17) (9 12 15) (12 16 20))</pre>
 
Note that <tt>gather</tt>/<tt>take</tt> is the primitive in Raku corresponding to generators or coroutines in other languages. It is not, however, tied to function call syntax in Raku. 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.
1,934

edits

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