polserver/testsuite/escript/array/reduce.src
Kevin Eady d1a1bd3afe
Additional escript array functional methods (#674)
* Implementation change for continuation handling

* Implement array methods find, findIndex, map, reduce

* Add tests

* update filter test for coverage

* update docs

* change implementation to not copy

* update tests; add tests for "polyfill" comparison

* update escript guide
2024-08-02 20:39:32 +02:00

48 lines
1.3 KiB
Text

// Test matrix:
// Array length (=0, =1, >1) x Accumulator (none, with) = 6 prints
// >1 length, no accumulator
print( array{ 1, 2, 3, 4, 5 }.reduce( @Sum ) ); // 15
// >1 length, with accumulator
print( array{ 1, 2, 3, 4, 5 }.reduce( @Sum, -15 ) ); // 0
// =1 length, no accumulator
print( array{ 1 }.reduce( @DoNotCall ) ); // 1
// =1 length, with accumulator
print( array{ 1 }.reduce( @Sum, -10 ) ); // -9
// =0 length, no accumulator
print( array{}.reduce( @DoNotCall ) ); // Returns an error
// =0 length, with accumulator
print( array{}.reduce( @DoNotCall, 3.25 ) ); // 3.25
// Final test uses all callback parameters
// (0+4+1+4) -> (9+5+2+5) -> (21+6+3+6) = 36
print( array{ 4, 5, 6 }.reduce( @UsesAllParameters, 0 ) );
// Test with a name_arr'd array
var arr := array{ "hello" };
arr.+name := "hello";
print( arr.reduce( @DoNotCall ) );
// Errors
print( array{}.reduce() );
print( array{}.reduce( "foo" ) );
print( "Finished!" );
function Sum( accumulator, currentValue )
return accumulator + currentValue;
endfunction
function UsesAllParameters( accumulator, currentValue, currentIndex, arr )
return accumulator + currentValue + currentIndex + arr[currentIndex];
endfunction
// If this function gets called, script will exit, without printing the final "Finished!"
function DoNotCall()
exit;
endfunction