Logo

Programming-Idioms

  • Fortran
  • Dart
  • Rust

Idiom #257 Traverse list backwards

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

for (i, item) in items.iter().enumerate().rev() {
    println!("{} = {}", i, *item);
}
  for (i, x) in items.iter().rev().enumerate() {
    println!("{i} = {x}");
  }
do i=size(items),1,-1
  print *,i,items(i)
end do
var i = items.length ;
for (var e in items.reversed) {
  i--;
  print("$i, $e");
}
for(int i = items.Count - 1; i >= 0; i--)
{
    Console.WriteLine($"Index = {i}, Item = {items[i]}");
}

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