Xin Wang
2016-03-15 03:55:22 UTC
In M2R10 report[1], there is an example showing sliced insert feature of flexible array:
VAR array1, array2, array3 : FlexArray;
array2 := { 1, 2, 3, 4 }; array3 := { 7, 8, 9 };
array1 := array2 & array3; (* concatenation : { 1, 2, 3, 4, 7, 8, 9 } *)
array1[4..5] := { 5, 6 }; (* sliced insert : { 1, 2, 3, 4, 5, 6, 7, 8, 9 } *)
I expect its result would be `{1, 2, 3, 4, 5, 6, 9}`, treating this kind of assignment as replacement instead of insertion.
Python have a similar feature, as stated in reference[2]:
Finally, the sequence object is asked to replace the slice with the items of the assigned sequence. The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the target sequence allows it.
Additional, I'd like lhs and rhs subrange be same size, so array's size do not change after slice assignment, to keep safety characteristic of Modula-2.
[1] http://modula-2.info/m2r10/pmwiki.php/Spec/LanguageReport#ArrayTypes
[2] https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
VAR array1, array2, array3 : FlexArray;
array2 := { 1, 2, 3, 4 }; array3 := { 7, 8, 9 };
array1 := array2 & array3; (* concatenation : { 1, 2, 3, 4, 7, 8, 9 } *)
array1[4..5] := { 5, 6 }; (* sliced insert : { 1, 2, 3, 4, 5, 6, 7, 8, 9 } *)
I expect its result would be `{1, 2, 3, 4, 5, 6, 9}`, treating this kind of assignment as replacement instead of insertion.
Python have a similar feature, as stated in reference[2]:
Finally, the sequence object is asked to replace the slice with the items of the assigned sequence. The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the target sequence allows it.
Additional, I'd like lhs and rhs subrange be same size, so array's size do not change after slice assignment, to keep safety characteristic of Modula-2.
[1] http://modula-2.info/m2r10/pmwiki.php/Spec/LanguageReport#ArrayTypes
[2] https://docs.python.org/3/reference/simple_stmts.html#assignment-statements