Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

Idiom #257 Traverse list backwards

Print each index i and value x from the list items, from the last down to the first.

for(int i = items.Count - 1; i >= 0; i--)
{
    Console.WriteLine($"Index = {i}, Item = {items[i]}");
}
var i = items.length ;
for (var e in items.reversed) {
  i--;
  print("$i, $e");
}
do i=size(items),1,-1
  print *,i,items(i)
end do
import "fmt"
for i := len(items) - 1; i >= 0; i-- {
	x := items[i]
	fmt.Printf("Item %d = %v \n", i, x)
}
[...items].reverse().forEach((item, index) => 
console.log(Math.abs(index -= items.length), item));
for(int i = items.length-1;i>=0;i--){
	System.out.println("Index:"+ i +", Value:" + items[i]); 
}
for i := items.count-1 downto 0 do writeln(i,' ',items[i]);
for ($i = $#items; $i >= 0; $i--) {
    print "$i $items[$i]\n";
}
for i in range(len(items)-1, -1, -1):
    print(i, items[i])
items.each_with_index.reverse_each{|x, i| puts "#{i} #{x}" }
for (i, item) in items.iter().enumerate().rev() {
    println!("{} = {}", i, *item);
}
(define (reversed-inspect items)
  (foldl (lambda (x i)
          (display (format "~a ~a\n" i x))
          (sub1 i))
         (sub1 (length items))
        (reverse items)))

New implementation...
< >
programming-idioms.org