Logo

Programming-Idioms

  • JS
  • Elixir
  • Pascal

Idiom #264 Automated passing of array bounds

Pass a two-dimensional integer array a to a procedure foo and print the size of the array in each dimension. Do not pass the bounds manually. Call the procedure with a two-dimensional array.

type
  T2DArray = array of array of Integer;

procedure foo(A: T2DArray);
begin
  writeln(Length(A),', ', Length(A[0]));
end;

var A: T2DArray;

begin
  A := T2DArray.Create([1,2,3],[1,2,3]);
  foo(A);  //prints 2, 3
end.
foo(List<List<int>> a) => print("${a.length} ${a[0].length}");
var a = [
    [1, 2],
    [3, 4],
    [5, 6]
  ];
foo(a);

New implementation...
< >
tkoenig