Slice
Synopsis
Assign to a slice of a list or string.
Syntax
Assignable [ Exp₁ .. Exp3_ ] = Exp₄
Assignable [ Exp₁, Exp₂ .. Exp₃ ] = Exp₄
Exp₁
and Exp₃
are optional
Description
A slice assignment is defined for List, String and Node and aims to replace a slice from the old value of the assignable by a new value. See Slice, Slice or Slice for a more detailed explanation of slicing.
Let V be the current value of Assignable.
Assignable [ Exp₁ .. Exp₃ ] = Exp₄
: The slice[ Exp₁ .. Exp₃ ]
determines two indicesbegin
(inclusive) andend
(exclusive) in V. A new value V' is computed that is a copy of V but with all the elements in V withbegin <= index < end
replaced by the elements of the value of Exp₄. Note that the size of V and V' may differ. V' is assigned to the Assignable.Assignable [ Exp₁, Exp₂ .. Exp₃ ] = Exp₄
: The slice[ Exp₁, Exp₂ .. _Exp₃ ]
determines two indicesbegin
(inclusive) andend
(exclusive) and astep
between indices in V. A new value V' is computed that is a copy of V but with all the elements in V with indicesbegin
,begin+step
. ...end-step
<=index < end
replaced by the successive elements of the value of Exp₄. Note that the size of V and V' may differ. V' is assigned to the Assignable. If the number of indices in the slice and the number of elements in the value of Exp₄ is not equal the following is done:- If the number of elements in the slice is larger: the elements of Exp₄ are used in a circular manner.
- If the number of elements in the slice is smaller: the remaining elements of Exp₄ is inserted after the last index in the slice.
Examples
Replace the elements with index 3, 4, 5 in L
:
rascal>L = [0,1,2,3,4,5,6,7,8,9];
list[int]: [0,1,2,3,4,5,6,7,8,9]
rascal>L[3..6] = [100,200,300,400,500];
list[int]: [0,1,2,100,200,300,400,500,6,7,8,9]
Replace the elements with index 1, 3, 5, 7 in L
(note how the elements from [100,200]
are used in a circular way):
rascal>L = [0,1,2,3,4,5,6,7,8,9];
list[int]: [0,1,2,3,4,5,6,7,8,9]
rascal>L[1,3..8] = [100,200];
list[int]: [0,100,2,200,4,100,6,200,8,9]
Replace the elements with index 1, 3, 5, 7 in L
(note how the unused elements from [100,200,300,400,500]
are insert at index 7):
rascal>L = [0,1,2,3,4,5,6,7,8,9];
list[int]: [0,1,2,3,4,5,6,7,8,9]
rascal>L[1,3..8] = [100,200,300,400,500];
list[int]: [0,100,2,200,4,300,6,400,500,8,9]
Similar examples for slicing assignment on strings:
rascal>S = "abcdefghij";
str: "abcdefghij"
---
abcdefghij
---
rascal>S[3..6] = "UVWXYZ";
str: "abcUVWXYZghij"
---
abcUVWXYZghij
---
rascal>S = "abcdefghij";
str: "abcdefghij"
---
abcdefghij
---
rascal>S[1,3..8] = "XY";
str: "aXcYeXgYij"
---
aXcYeXgYij
---
rascal>S = "abcdefghij";
str: "abcdefghij"
---
abcdefghij
---
rascal>S[1,3..8] = "UVWXYZ";
str: "aUcVeWgXYZij"
---
aUcVeWgXYZij
---
Replace the elements with index 3, 4, 5 in node N
:
rascal>N = "f"(0,true,2,"abc",4,5.5,6,{7,77},8,{9,99,999});
node: "f"(
0,
true,
2,
"abc",
4,
5.5,
6,
{7,77},
8,
{999,9,99})
rascal>N[3..6] = [100,200,300,400,500];
node: "f"(
0,
true,
2,
100,
200,
300,
400,
500,
6,
{7,77},
8,
{999,9,99})
Replace the elements with index 1, 3, 5, 7 in L
(note how the elements from [100,200]
are used in a circular way):
rascal>N = "f"(0,true,2,"abc",4,5.5,6,{7,77},8,{9,99,999});
node: "f"(
0,
true,
2,
"abc",
4,
5.5,
6,
{7,77},
8,
{999,9,99})
rascal>N[1,3..8] = [100,200];
node: "f"(
0,
100,
2,
200,
4,
100,
6,
200,
8,
{999,9,99})
Replace the elements with index 1, 3, 5, 7 in L
(note how the unused elements from [100,200,300,400,500]
are insert at index 7):
rascal>N = "f"(0,true,2,"abc",4,5.5,6,{7,77},8,{9,99,999});
node: "f"(
0,
true,
2,
"abc",
4,
5.5,
6,
{7,77},
8,
{999,9,99})
rascal>N[1,3..8] = [100,200,300,400,500];
node: "f"(
0,
100,
2,
200,
4,
300,
6,
400,
500,
8,
{999,9,99})