Logo

Programming-Idioms

History of Idiom 7 > diff from v47 to v48

Edit summary for version 48 by :
[Haskell] Shorter, more idiomatic, less duplication

Version 47

2016-02-17, 11:50:39

Version 48

2016-02-18, 16:57:56

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Imports
with Ada.Text_IO;
use Ada.Text_IO;
Imports
with Ada.Text_IO;
use Ada.Text_IO;
Code
for I in Items'Range loop
   X := Items (I);
   Put_Line (Integer'Image (I) & " " & Integer'Image (X));
end loop;
Code
for I in Items'Range loop
   X := Items (I);
   Put_Line (Integer'Image (I) & " " & Integer'Image (X));
end loop;
Comments bubble
Assuming Items is an array of integers.
Comments bubble
Assuming Items is an array of integers.
Imports
using System;
Imports
using System;
Code
for (int i = 0; i < items.Length; i++)
{
    Console.WriteLine("{0} {1}", i, items[i]);
}
Code
for (int i = 0; i < items.Length; i++)
{
    Console.WriteLine("{0} {1}", i, items[i]);
}
Imports
#include <iostream>
#include <vector>
Imports
#include <iostream>
#include <vector>
Code
for(const auto & item : items) {
  static auto idx = 0;
  std::cout << "Item " << idx++ << " = " << item << std::endl;
}
Code
for(const auto & item : items) {
  static auto idx = 0;
  std::cout << "Item " << idx++ << " = " << item << std::endl;
}
Comments bubble
Uses the C++11 features "range based for loop" and "auto"
Comments bubble
Uses the C++11 features "range based for loop" and "auto"
Code
mapM_ print (zip [0..] items)
Code
mapM_ print (zip [0..] items)
Imports
Classes, SysUtils
Imports
Classes, SysUtils
Code
var Iter:Integer;
    Items: array of TObject;
[...]
  for Iter := 0 to high(Items) do
    if assigned(Items[Iter]) then
      WriteLn(Format('Item %d = %s', [Iter, Items[Iter].ToString]));
Code
var Iter:Integer;
    Items: array of TObject;
[...]
  for Iter := 0 to high(Items) do
    if assigned(Items[Iter]) then
      WriteLn(Format('Item %d = %s', [Iter, Items[Iter].ToString]));
Comments bubble
If you have an TObjects descendent, you can use the ToString method to get it as a String.
Comments bubble
If you have an TObjects descendent, you can use the ToString method to get it as a String.
Code
items.forEach(function(val,idx,ary){
  console.log("index=" + idx + ", value=" + val);
});
Code
items.forEach(function(val,idx,ary){
  console.log("index=" + idx + ", value=" + val);
});
Comments bubble
This is the "functional way" of iterating.
Comments bubble
This is the "functional way" of iterating.
Code
int i;
for(i=0 ; i<n ; i++){
  T x = items[i];
  printf("Item %d = %s\n", i, toString(x) );
}
Code
int i;
for(i=0 ; i<n ; i++){
  T x = items[i];
  printf("Item %d = %s\n", i, toString(x) );
}
Comments bubble
The loop variable i is the index. Inside the loop, access the value with items[i]
Comments bubble
The loop variable i is the index. Inside the loop, access the value with items[i]
Imports
import "fmt"
Imports
import "fmt"
Code
for i, x := range items {
    fmt.Printf("Item %d = %v \n", i, x)
}
Code
for i, x := range items {
    fmt.Printf("Item %d = %v \n", i, x)
}
Comments bubble
The range clause gives you index and value at the same time as loop variables
Comments bubble
The range clause gives you index and value at the same time as loop variables
Doc URL
https://golang.org/doc/effective_go.html#for
Doc URL
https://golang.org/doc/effective_go.html#for
Demo URL
http://play.golang.org/p/GH_8f06DX5
Demo URL
http://play.golang.org/p/GH_8f06DX5