This language bar is your friend. Select your favorite languages!

Idiom #6 Iterate over list values

Do something with each item x of an array-like collection items, regardless indexes.

1
for Item of Items loop
   Do_Something (Item);
end loop;
for (unsigned int i = 0 ; i < items_length ; ++i){
        Item* x = &(items[i]);
	DoSomethingWith(x);
}
(map do-something items)
for(const auto &&x : items) {
	doSomething(x);
}
foreach(var item in items)
{
	doSomething(item);
}
foreach(x; items) {
	doSomethingWith(x);
}
import std.algorithm.iteration;
items.each!(a => writeln(a))();
items.forEach(doSomething);
Enum.each(items, &IO.inspect/1)

for x <- items do
  IO.inspect(x)
end
lists:foreach(fun do_something/1, Items).
[do_something(X) || X <- Items]
for _, x := range items {
    doSomething( x )
}
1
import Control.Monad
forM_ items doSomething
for(var i = 0; i < items.length; i++) {
	var x = items[i];
	doSomething(x);
}
items.forEach(function(x) {
    doSomething(x);
});
for(int i=0;i<items.length;i++){
	doSomething( items[i] );
}
for(Item x : items ){
	doSomething( x );
}
for _, x in ipairs(items) do
	print(x)
end
foreach ($items as $x){
    doSomething( $x );
}
for x in items do ;
do_something($_) for @items;
for my $x (@items) {
     do_something($x);
     more_stuffs($x);
}
for x in items:
        doSomething( x )
items.each do |x|
  do_something( x )
end
items.each{|x| do_something( x )}
for x in items {
	do_something(x);
}
for (item <- items) doSomething(item)
(define (doSomething x)
    (if (not (null? x))
        (begin 
            (display "Item=")
            (display (car x))
            (newline)
            (doSomething (cdr x)))))

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms