Discussion:
sliced insert in M2R10
(too old to reply)
Xin Wang
2016-03-15 03:55:22 UTC
Permalink
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
trijezdci
2016-03-15 11:30:12 UTC
Permalink
Post by Xin Wang
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.
This may be a typo. The insertion syntax shouldn't have an upper bound:

array1[4..] := { 5, 6 };

which results in { 1, 2, 3, 4, 5, 6, 7, 8, 9 }

I will see to it that this is updated accordingly. Remind me again in a couple of weeks if this hasn't happened. Thanks,
Xin Wang
2016-03-16 13:22:07 UTC
Permalink
在 2016年3月15日星期二 UTC+8下午7:30:12,trijezdci写道:
Post by trijezdci
Post by Xin Wang
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.
array1[4..] := { 5, 6 };
which results in { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
I will see to it that this is updated accordingly. Remind me again in a couple of weeks if this hasn't happened. Thanks,
I still prefer Python's semantic, where the result will be {1, 2, 3, 4, 5, 6}.

Actually, I find it a bit difficult to relate assignment to *insertion*, which I think *replacement* is kind of more nature.
trijezdci
2016-03-16 19:51:54 UTC
Permalink
Post by Xin Wang
Post by trijezdci
array1[4..] := { 5, 6 };
which results in { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
I still prefer Python's semantic, where the result will be {1, 2, 3, 4, 5, 6}.
Actually, you should be able to do the same in Python with the same result

array1[4:4] = [ 5, 6 ]
Post by Xin Wang
Post by trijezdci
Post by Xin Wang
array1
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Insert is insert, replacement is replacement. Both are available in M2 R10. Each has its own syntax. This particular syntax is for inserts, [n..] in M2 R10, [n:n] in Python. Replacement is [n..m], or [n:m] in Python.
Xin Wang
2016-03-17 00:58:41 UTC
Permalink
在 2016年3月17日星期四 UTC+8上午3:51:54,trijezdci写道:
Post by trijezdci
Post by Xin Wang
Post by trijezdci
array1[4..] := { 5, 6 };
which results in { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
I still prefer Python's semantic, where the result will be {1, 2, 3, 4, 5, 6}.
Actually, you should be able to do the same in Python with the same result
array1[4:4] = [ 5, 6 ]
Post by Xin Wang
Post by trijezdci
Post by Xin Wang
array1
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Insert is insert, replacement is replacement. Both are available in M2 R10. Each has its own syntax. This particular syntax is for inserts, [n..] in M2 R10, [n:n] in Python. Replacement is [n..m], or [n:m] in Python.
Clear now, both syntax need to be described on report. Currently that code sample is both incorrect and incomplete.
Loading...