Loops/Foreach: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 22: Line 22:
*   [[Loops/Wrong ranges]]
*   [[Loops/Wrong ranges]]
<br><br>
<br><br>

=={{header|ACL2}}==
=={{header|ACL2}}==


Line 222: Line 222:
for (i, v in list(2, 3, 5, 7, 11, 13, 17, 18)) {
for (i, v in list(2, 3, 5, 7, 11, 13, 17, 18)) {
}</lang>
}</lang>



=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 719: Line 718:
}
}
</lang>
</lang>

=={{header|C sharp|C#}}==
<lang csharp>string[] things = {"Apple", "Banana", "Coconut"};

foreach (string thing in things)
{
Console.WriteLine(thing);
}</lang>


=={{header|C++}}==
=={{header|C++}}==
Line 751: Line 758:
{
{
element += 42;
element += 42;
}</lang>

=={{header|C sharp|C#}}==
<lang csharp>string[] things = {"Apple", "Banana", "Coconut"};

foreach (string thing in things)
{
Console.WriteLine(thing);
}</lang>
}</lang>


Line 1,008: Line 1,007:


<code>cl.el</code> also offers a <code>loop</code> macro similar in style to [[#Common Lisp|Common Lisp]].
<code>cl.el</code> also offers a <code>loop</code> macro similar in style to [[#Common Lisp|Common Lisp]].



=={{header|Erlang}}==
=={{header|Erlang}}==
Line 1,015: Line 1,013:
However, to iterate over each element of a list, Erlang uses <tt>lists:map/2</tt>, except in the case of IO where <tt>lists:foreach/2</tt> has to be used as the evaluation order is defined to be the same as the order of the elements in the list.
However, to iterate over each element of a list, Erlang uses <tt>lists:map/2</tt>, except in the case of IO where <tt>lists:foreach/2</tt> has to be used as the evaluation order is defined to be the same as the order of the elements in the list.
<lang erlang>lists:foreach(fun(X) -> io:format("~p~n",[X]) end, Collection).</lang>
<lang erlang>lists:foreach(fun(X) -> io:format("~p~n",[X]) end, Collection).</lang>



=={{header|ERRE}}==
=={{header|ERRE}}==
Line 1,091: Line 1,088:
2
2
</pre>
</pre>

=={{header|F_Sharp|F#}}==
We can use ''for'' directly or list iteration.
<lang fsharp>for i in [1 .. 10] do printfn "%d" i

List.iter (fun i -> printfn "%d" i) [1 .. 10]</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Line 1,109: Line 1,112:
}</lang>
}</lang>


=={{header|friendly interactive shell}}==
=={{header|Forth}}==
<lang forth>create a 3 , 2 , 1 ,
Unlike, bash or csh, the PATH variable is automatically converted to real array.
: .array ( a len -- )
<lang fishshell>for path in $PATH
cells bounds do i @ . cell +loop ; \ 3 2 1</lang>
echo You have $path in PATH.
end</lang>

Sample output:
<pre>
You have /bin in PATH.
You have /usr/bin in PATH.
</pre>


=={{header|Fortran}}==
=={{header|Fortran}}==
Line 1,139: Line 1,135:
end program main</lang>
end program main</lang>


=={{header|Forth}}==
=={{header|friendly interactive shell}}==
Unlike, bash or csh, the PATH variable is automatically converted to real array.
<lang forth>create a 3 , 2 , 1 ,
<lang fishshell>for path in $PATH
: .array ( a len -- )
echo You have $path in PATH.
cells bounds do i @ . cell +loop ; \ 3 2 1</lang>
end</lang>


Sample output:
<pre>
You have /bin in PATH.
You have /usr/bin in PATH.
</pre>


=={{header|Frink}}==
=={{header|Frink}}==
Line 1,152: Line 1,154:
println[n]
println[n]
</lang>
</lang>

=={{header|F_Sharp|F#}}==
We can use ''for'' directly or list iteration.
<lang fsharp>for i in [1 .. 10] do printfn "%d" i

List.iter (fun i -> printfn "%d" i) [1 .. 10]</lang>


=={{header|GAP}}==
=={{header|GAP}}==
Line 1,234: Line 1,230:
=={{header|Hy}}==
=={{header|Hy}}==
<lang clojure>(for [x collection] (print x))</lang>
<lang clojure>(for [x collection] (print x))</lang>

=={{header|Io}}==
<lang io>collection foreach(println)</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Line 1,248: Line 1,241:
<lang Icon>every write(!L)</lang>
<lang Icon>every write(!L)</lang>


=={{header|Io}}==
<lang io>collection foreach(println)</lang>


=={{header|J}}==
=={{header|J}}==
Line 1,363: Line 1,358:
*/</lang>
*/</lang>


=={{header|Jsish}}==
Jsi supports ''for of'' for looping over element of an array.
<lang javascript>for (str of "alpha beta gamma delta".split(' ')) { puts(str); }</lang>

{{out}}
<pre>alpha
beta
gamma
delta</pre>

=={{header|Julia}}==
{{trans|Python}}
<lang julia>for i in collection
println(i)
end</lang>
The Julia <code>for</code> statement is always a "foreach", and the built-in <code>start:end</code> or <code>start:step:end</code> "range" syntax can be used for iteration over arithmetic sequences. Many Julia objects support iteration: arrays and tuples iterate over each item, strings iterate over each character, dictionaries iterate over (key,value) pairs, numeric scalars provide a length-1 iteration over their value, and so on.
=={{header|jq}}==
=={{header|jq}}==
'''Iterables''':
'''Iterables''':
Line 1,410: Line 1,389:


In both cases, the result is the stream of values: "a", "b", "c".
In both cases, the result is the stream of values: "a", "b", "c".

=={{header|Jsish}}==
Jsi supports ''for of'' for looping over element of an array.
<lang javascript>for (str of "alpha beta gamma delta".split(' ')) { puts(str); }</lang>

{{out}}
<pre>alpha
beta
gamma
delta</pre>

=={{header|Julia}}==
{{trans|Python}}
<lang julia>for i in collection
println(i)
end</lang>
The Julia <code>for</code> statement is always a "foreach", and the built-in <code>start:end</code> or <code>start:step:end</code> "range" syntax can be used for iteration over arithmetic sequences. Many Julia objects support iteration: arrays and tuples iterate over each item, strings iterate over each character, dictionaries iterate over (key,value) pairs, numeric scalars provide a length-1 iteration over their value, and so on.

=={{header|K}}==
<lang K> {`0:$x} ' !10</lang>
<lang K> _sin ' (1; 2; 3;)</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
Line 1,429: Line 1,429:
</pre>
</pre>


=={{header|LabVIEW}}==
[[LabVIEW]] has a feature known as an Auto-Indexed Tunnel. It is the very small orange box on the lower left of the for loop.<br/>{{VI solution|LabVIEW_Loops_Foreach.png}}

=={{header|Lang5}}==
<lang lang5>: >>say.(*) . ;
5 iota >>say.</lang>


=={{header|langur}}==
=={{header|langur}}==
Line 1,467: Line 1,473:
<lang Lasso>array(1,2,3) => foreach { stdoutnl(#1) }</lang>
<lang Lasso>array(1,2,3) => foreach { stdoutnl(#1) }</lang>
<lang Lasso>with i in array(1,2,3) do { stdoutnl(#i) }</lang>
<lang Lasso>with i in array(1,2,3) do { stdoutnl(#i) }</lang>

=={{header|K}}==
<lang K> {`0:$x} ' !10</lang>
<lang K> _sin ' (1; 2; 3;)</lang>


=={{header|LFE}}==
=={{header|LFE}}==
Line 1,479: Line 1,481:
(lists:seq 1 10))
(lists:seq 1 10))
</lang>
</lang>

=={{header|LabVIEW}}==
[[LabVIEW]] has a feature known as an Auto-Indexed Tunnel. It is the very small orange box on the lower left of the for loop.<br/>{{VI solution|LabVIEW_Loops_Foreach.png}}

=={{header|Lang5}}==
<lang lang5>: >>say.(*) . ;
5 iota >>say.</lang>


=={{header|LIL}}==
=={{header|LIL}}==
Line 1,750: Line 1,745:
dolor
dolor
</pre>
</pre>

=={{header|Objeck}}==
<lang objeck>fruits := ["Apple", "Banana", "Coconut"];
each(i : fruits) {
fruits[i]->PrintLine();
};</lang>


=={{header|Objective-C}}==
=={{header|Objective-C}}==
Line 1,771: Line 1,772:
// do something with object i
// do something with object i
}</lang>
}</lang>

=={{header|Objeck}}==
<lang objeck>fruits := ["Apple", "Banana", "Coconut"];
each(i : fruits) {
fruits[i]->PrintLine();
};</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 1,787: Line 1,782:
(fun i -> Printf.printf "%d\n" i)
(fun i -> Printf.printf "%d\n" i)
collect_array</lang>
collect_array</lang>

=={{header|Oforth}}==

<lang Oforth>: printMonths | m | Date.Months forEach: m [ m . ] ;</lang>

But, apply can be used instead of a loop :
<lang Oforth>#. Date.Months apply</lang>


=={{header|Octave}}==
=={{header|Octave}}==
Line 1,811: Line 1,799:
disp(val);
disp(val);
endfor</lang>
endfor</lang>

=={{header|Oforth}}==

<lang Oforth>: printMonths | m | Date.Months forEach: m [ m . ] ;</lang>

But, apply can be used instead of a loop :
<lang Oforth>#. Date.Months apply</lang>


=={{header|Ol}}==
=={{header|Ol}}==
Line 1,889: Line 1,884:
print "I like $l\n";
print "I like $l\n";
}</lang>
}</lang>

=={{header|Perl 6}}==
{{works with|Rakudo|2015.10-40}}
<lang perl6>say $_ for @collection;</lang>
Perl 6 leaves off the <tt>each</tt> from <tt>foreach</tt>, leaving us with <tt>for</tt> instead. The variable <tt>$_</tt> refers to the current element, unless you assign a name to it using <tt>-></tt>.
<lang perl6>for @collection -> $currentElement { say $currentElement; }</lang>
Perl 6 will do it's best to put the topic at the right spot.
<lang perl6>.say for @collection;
for @collection { .say };</lang>
Iteration can also be done with hyperoperators. In this case it's a candidate for autothreading and as such, execution order may vary. The resulting list will be in order.
<lang per6>@collection>>.say;
@collection>>.=&infix:<+>(2); # increment each element by 2</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,008: Line 1,991:
true.
true.
</lang>
</lang>



=={{header|Python}}==
=={{header|Python}}==
Line 2,062: Line 2,044:
(displayln i))
(displayln i))
</lang>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.10-40}}
<lang perl6>say $_ for @collection;</lang>
Perl 6 leaves off the <tt>each</tt> from <tt>foreach</tt>, leaving us with <tt>for</tt> instead. The variable <tt>$_</tt> refers to the current element, unless you assign a name to it using <tt>-></tt>.
<lang perl6>for @collection -> $currentElement { say $currentElement; }</lang>
Perl 6 will do it's best to put the topic at the right spot.
<lang perl6>.say for @collection;
for @collection { .say };</lang>
Iteration can also be done with hyperoperators. In this case it's a candidate for autothreading and as such, execution order may vary. The resulting list will be in order.
<lang per6>@collection>>.say;
@collection>>.=&infix:<+>(2); # increment each element by 2</lang>


=={{header|REBOL}}==
=={{header|REBOL}}==
Line 2,161: Line 2,156:
end</lang>
end</lang>
There are various flavours of <code>each</code> that may be class-dependent: [http://www.ruby-doc.org/core/classes/String.html#M000862 String#each_char], [http://www.ruby-doc.org/core/classes/Array.html#M002174 Array#each_index], [http://www.ruby-doc.org/core/classes/Hash.html#M002863 Hash#each_key], etc
There are various flavours of <code>each</code> that may be class-dependent: [http://www.ruby-doc.org/core/classes/String.html#M000862 String#each_char], [http://www.ruby-doc.org/core/classes/Array.html#M002174 Array#each_index], [http://www.ruby-doc.org/core/classes/Hash.html#M002863 Hash#each_key], etc




=={{header|Rust}}==
=={{header|Rust}}==
Line 2,324: Line 2,317:
display "`i'"
display "`i'"
}</lang>
}</lang>

=={{header|Suneido}}==
=={{header|Suneido}}==
<lang Suneido>for i in #(1, 2, 3)
<lang Suneido>for i in #(1, 2, 3)
Line 2,368: Line 2,362:
puts "$i,$x,$y"
puts "$i,$x,$y"
}</lang>
}</lang>



=={{header|Trith}}==
=={{header|Trith}}==
Line 2,443: Line 2,436:
echo i
echo i
endfor</lang>
endfor</lang>



=={{header|Wart}}==
=={{header|Wart}}==