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

Idiom #196 Pass a sub-array

Given an integer array a of size n, pass the first, third, fifth and seventh, ... up to the m th element to a routine foo which sets all these elements to 42.

! Caller:
  integer, dimension(n) :: a
  call foo(a(1:m:2))

! Callee:

  subroutine foo(a)
    integer, dimension(:), intent(inout) :: a
    a = 42
  end subroutine foo
import std.range;
void foo(Range)(Range r) {
	r.fill(42);
}

foo(a.indexed(iota(0,m,2)));
sub foo  {
    my ($A, @i) = @_;
    @$A[@i] = (42) x @i;
    return $A;
}
foo($A, grep { 0 == $_ % 2 } 0 .. $m);

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

Idiom created by

tkoenig