role LinearInterpolation { multi method Num postcircumfix:«[]» ( Num @self is constant : Num $real_index) { int $integer_index = int $real_index; Num $first_value = @self[$integer_index];
return $first_value if $integer_index == $real_index;
Num $second_value = @self[$integer_index+1];
Num $delta = $second_value - $first_value; Num $weight = $real_index - $integer_index;
return $first_value + $delta * $weight; } }
my @test does LinearInterpolation;
@test[0] = 1; @test[1] = 3;
assert @test[0] == 1; assert @test[0.5] == 2; assert @test[1] == 3;
If I later decare a sub as
sub foo ( @in does LinearInterpolation ) { ... }
Would I be able to pass a normal (non-interpolating) array to this sub, and then access it using non-integer indices (i.e. is the data in the array independent of the interface through wich it is accessed).
Dave.