-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path47.ReadonlyArrays.ts
55 lines (41 loc) · 1.97 KB
/
47.ReadonlyArrays.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Immutable Arrays and Coordinate Pairs
// This function takes an array of integers, sorts it, and then reverses it.
// However, it mutates the original array.
function sortAndInvert(numbers: number[]): number[] {
return numbers.sort().reverse();
}
const initialNumbers = [3, 6, 2, 8, 4];
const invertedNumbers = sortAndInvert(initialNumbers);
console.log(invertedNumbers); // [8, 6, 4, 3, 2]
console.log(initialNumbers); // [8, 6, 4, 3, 2] - Note! The original array has changed.
// This function is a modified version that prevents mutation of the original array.
function sortAndInvertImmutable(numbers: readonly number[]): number[] {
return numbers.slice().sort().reverse();
}
const initialImmutable = [3, 6, 2, 8, 4];
const invertedImmutable = sortAndInvertImmutable(initialImmutable);
console.log(initialImmutable); // [3, 6, 2, 8, 4]
console.log(invertedImmutable); // [8, 6, 4, 3, 2]
// A type representing a 2D coordinate.
type Coordinate = [number, number];
// This function moves a coordinate by x and y values.
// However, it mutates the original coordinate.
function shiftCoordinate(coord: Coordinate, deltaX: number, deltaY: number): Coordinate {
coord[0] += deltaX;
coord[1] += deltaY;
return coord;
}
const originalCoord: Coordinate = [2, 2];
const shiftedCoord = shiftCoordinate(originalCoord, 4, 4);
console.log(originalCoord); // [6, 6]
console.log(shiftedCoord); // [6, 6]
// A modified version of the Coordinate type that is read-only.
type ImmutableCoordinate = readonly [number, number];
// This function is a modified version that prevents mutation of the original coordinate.
function shiftImmutableCoordinate(coord: ImmutableCoordinate, deltaX: number, deltaY: number): Coordinate {
return [coord[0] + deltaX, coord[1] + deltaY];
}
const originalImmutableCoord: Coordinate = [2, 2];
const shiftedImmutableCoord = shiftImmutableCoordinate(originalImmutableCoord, 4, 4);
console.log(originalImmutableCoord); // [2, 2]
console.log(shiftedImmutableCoord); // [6, 6]