Loops/Foreach: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(+D)
Line 24: Line 24:
std::for_each(container.begin(), container.end(), print_element);
std::for_each(container.begin(), container.end(), print_element);
</cpp>
</cpp>

=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<pre language="lisp">(loop for i in list do (print i))</pre>
<pre language="lisp">(loop for i in list do (print i))</pre>
=={{header|D}}==

This works if ''collection'' is an array/associative array type or a type that implements an appropriate ''opApply'' function.
<d>foreach(element ; collection)
writefln(element);</d>
=={{header|Forth}}==
=={{header|Forth}}==
create a 3 , 2 , 1 ,
create a 3 , 2 , 1 ,
Line 35: Line 37:
=={{header|Haskell}}==
=={{header|Haskell}}==
forM_ collect print
forM_ collect print

=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
Line 44: Line 45:
}</java>
}</java>
This works for any array type as well as any type that implements the Iterable interface (including all Collections).
This works for any array type as well as any type that implements the Iterable interface (including all Collections).

=={{header|JavaScript}}==
=={{header|JavaScript}}==
This works for any object, as well as arrays.
This works for any object, as well as arrays.
for (var a in o) print(o[a]);
for (var a in o) print(o[a]);

=={{header|Logo}}==
=={{header|Logo}}==
foreach [red green blue] [print ?]
foreach [red green blue] [print ?]

=={{header|MAXScript}}==
=={{header|MAXScript}}==
<pre>
<pre>
Line 70: Line 68:
(fun i -> Printf.printf "%d\n" i)
(fun i -> Printf.printf "%d\n" i)
collect_array</ocaml>
collect_array</ocaml>

=={{header|Perl}}==
=={{header|Perl}}==
<perl>foreach $i (@collect) {
<perl>foreach $i (@collect) {
Line 76: Line 73:
}</perl>
}</perl>
The keyword ''for'' can be used instead of ''foreach''. If a variable ($i) is not given, then $_ is used.
The keyword ''for'' can be used instead of ''foreach''. If a variable ($i) is not given, then $_ is used.

=={{header|PHP}}==
=={{header|PHP}}==
<php>foreach ($collect as $i) {
<php>foreach ($collect as $i) {
echo "$i\n";
echo "$i\n";
}</php>
}</php>

=={{header|Pop11}}==
=={{header|Pop11}}==
Iteration over list:
Iteration over list:
Line 90: Line 85:
endfor;
endfor;
</pre>
</pre>

=={{header|Python}}==
=={{header|Python}}==
<python>for i in collect:
<python>for i in collect:
print i</python>
print i</python>

=={{header|Ruby}}==
=={{header|Ruby}}==
<ruby>for i in collection do
<ruby>for i in collection do
Line 105: Line 98:
puts i
puts i
end</ruby>
end</ruby>

=={{header|V}}==
=={{header|V}}==
[1 2 3] [puts] step
[1 2 3] [puts] step

Revision as of 15:57, 6 June 2008

Task
Loops/Foreach
You are encouraged to solve this task according to the task description, using any language you may know.

Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop.

C++

C++ does not (yet) have a "for each" loop. The following is a generic loop which works with any standard container except for built-in arrays. The code snippet below assumes that the container type in question is typedef'd to container_type and the actual container object is named container. <cpp>

 for (container_type::iterator i = container.begin(); i != container.end(); ++i)
 {
   std::cout << *i << "\n";
 }

</cpp> However the idiomatic way to output a container would be <cpp>

 std::copy(container.begin(), container.end(),
           std::output_iterator<container_type::value_type>(std::cout, "\n"));

</cpp> There's also an algorithm named for_each. However, you need a function or function object to use it, e.g. <cpp> void print_element(container_type::value_type const& v) {

 std::cout << v << "\n";

}

...

 std::for_each(container.begin(), container.end(), print_element);

</cpp>

Common Lisp

(loop for i in list do (print i))

D

This works if collection is an array/associative array type or a type that implements an appropriate opApply function. <d>foreach(element ; collection)

 writefln(element);</d>

Forth

create a 3 , 2 , 1 ,
: .array ( a len -- )
  cells bounds do  i @ .  cell +loop ;     \ 3 2 1

Haskell

forM_ collect print

Java

Works with: Java version 1.5+

<java>Collection<Type> collect; ... for(Type i:collect){

  System.out.println(i);

}</java> This works for any array type as well as any type that implements the Iterable interface (including all Collections).

JavaScript

This works for any object, as well as arrays.

for (var a in o) print(o[a]);

foreach [red green blue] [print ?]

MAXScript

for i in collect do
(
    print i
)

OCaml

List of integers: <ocaml>List.iter

 (fun i -> Printf.printf "%d\n" i)
 collect_list</ocaml>

Array of integers: <ocaml>Array.iter

 (fun i -> Printf.printf "%d\n" i)
 collect_array</ocaml>

Perl

<perl>foreach $i (@collect) {

  print "$i\n";

}</perl> The keyword for can be used instead of foreach. If a variable ($i) is not given, then $_ is used.

PHP

<php>foreach ($collect as $i) {

  echo "$i\n";

}</php>

Pop11

Iteration over list:

lvars el, lst = [1 2 3 4 foo bar];
for el in lst do
   printf(el,'%p\n');
endfor;

Python

<python>for i in collect:

  print i</python>

Ruby

for i in collection do

 puts i

end

This is syntactic sugar for:

collection.each do |i|

 puts i

end

V

[1 2 3] [puts] step