Repository navigation
Expand file tree
/
Copy pathdestructuring.smash
More file actions
183 lines (150 loc) 路 5.03 KB
/
Copy pathdestructuring.smash
File metadata and controls
183 lines (150 loc) 路 5.03 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// destructuring.smash - Examples of destructuring and spread operator in SmashLang
// Array destructuring examples
print("\n=== Array Destructuring ===\n");
// Basic array destructuring
let [a, b] = [1, 2];
print(`a = ${a}, b = ${b}`); // a = 1, b = 2
// Skipping elements
let [first, , third] = [1, 2, 3];
print(`first = ${first}, third = ${third}`); // first = 1, third = 3
// Rest pattern
let [head, ...tail] = [1, 2, 3, 4];
print(`head = ${head}, tail = ${JSON.stringify(tail)}`); // head = 1, tail = [2, 3, 4]
// Default values
let [x = 5, y = 7] = [1];
print(`x = ${x}, y = ${y}`); // x = 1, y = 7
// Swapping variables
let c = 1;
let d = 2;
[c, d] = [d, c];
print(`After swap: c = ${c}, d = ${d}`); // c = 2, d = 1
// Object destructuring examples
print("\n=== Object Destructuring ===\n");
// Basic object destructuring
let person = { name: "John", age: 30 };
let { name, age } = person;
print(`name = ${name}, age = ${age}`); // name = John, age = 30
// Assigning to different variable names
let { name: personName, age: personAge } = person;
print(`personName = ${personName}, personAge = ${personAge}`); // personName = John, personAge = 30
// Default values
let { name: n, age: a, job = "Developer" } = person;
print(`n = ${n}, a = ${a}, job = ${job}`); // n = John, a = 30, job = Developer
// Nested destructuring
let user = {
id: 42,
profile: {
name: "Alice",
address: {
city: "San Francisco",
country: "USA"
}
}
};
let { profile: { name: userName, address: { city } } } = user;
print(`userName = ${userName}, city = ${city}`); // userName = Alice, city = San Francisco
// Rest pattern with objects
let { name: personName2, ...rest } = person;
print(`personName2 = ${personName2}, rest = ${JSON.stringify(rest)}`); // personName2 = John, rest = {"age":30}
// Function parameter destructuring
print("\n=== Function Parameter Destructuring ===\n");
// Array parameter destructuring
fn sum([a, b]) {
return a + b;
}
print(`sum([1, 2]) = ${sum([1, 2])}`); // sum([1, 2]) = 3
// Object parameter destructuring
fn greet({ name, greeting = "Hello" }) {
return `${greeting}, ${name}!`;
}
print(greet({ name: "John" })); // Hello, John!
print(greet({ name: "Alice", greeting: "Hi" })); // Hi, Alice!
// Destructuring with rest parameters
fn printPersonInfo({ name, age, ...rest }) {
print(`Name: ${name}, Age: ${age}`);
print("Additional info:", rest);
return rest;
}
let additionalInfo = printPersonInfo({
name: "John",
age: 30,
job: "Developer",
city: "New York"
});
// Output:
// Name: John, Age: 30
// Additional info: { job: "Developer", city: "New York" }
// Spread operator examples
print("\n=== Array Spread ===\n");
// Combining arrays
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let combined = [...arr1, ...arr2];
print(`combined = ${JSON.stringify(combined)}`); // [1, 2, 3, 4, 5, 6]
// Copying arrays
let original = [1, 2, 3];
let copy = [...original];
original[0] = 99;
print(`original = ${JSON.stringify(original)}`); // [99, 2, 3]
print(`copy = ${JSON.stringify(copy)}`); // [1, 2, 3]
// Using spread with push
let numbers = [1, 2];
numbers.push(...[3, 4, 5]);
print(`numbers = ${JSON.stringify(numbers)}`); // [1, 2, 3, 4, 5]
// Using spread with function calls
fn add(a, b, c) {
return a + b + c;
}
let args = [1, 2, 3];
print(`add(...args) = ${add(...args)}`); // 6
print("\n=== Object Spread ===\n");
// Combining objects
let obj1 = { a: 1, b: 2 };
let obj2 = { c: 3, d: 4 };
let combinedObj = { ...obj1, ...obj2 };
print(`combinedObj = ${JSON.stringify(combinedObj)}`); // { a: 1, b: 2, c: 3, d: 4 }
// Copying objects
let originalObj = { a: 1, b: 2 };
let copyObj = { ...originalObj };
originalObj.a = 99;
print(`originalObj = ${JSON.stringify(originalObj)}`); // { a: 99, b: 2 }
print(`copyObj = ${JSON.stringify(copyObj)}`); // { a: 1, b: 2 }
// Overriding properties
let defaults = { theme: "dark", fontSize: 12, showSidebar: true };
let userPrefs = { theme: "light" };
let settings = { ...defaults, ...userPrefs };
print(`settings = ${JSON.stringify(settings)}`); // { theme: "light", fontSize: 12, showSidebar: true }
// Adding new properties while spreading
let employee = { ...person, jobTitle: "Developer", salary: 100000 };
print(`employee = ${JSON.stringify(employee)}`); // { name: "John", age: 30, jobTitle: "Developer", salary: 100000 }
// Practical example: Immutable state updates
print("\n=== Immutable State Updates ===\n");
const initialState = {
user: {
name: "John",
preferences: {
theme: "dark",
notifications: true
}
},
posts: []
};
// Update nested property immutably
const updatedState = {
...initialState,
user: {
...initialState.user,
preferences: {
...initialState.user.preferences,
theme: "light"
}
}
};
// Add item to array immutably
const stateWithNewPost = {
...updatedState,
posts: [...updatedState.posts, { id: 1, title: "New Post" }]
};
print(`Theme: ${stateWithNewPost.user.preferences.theme}`); // "light"
print(`Posts count: ${stateWithNewPost.posts.length}`); // 1
print(`First post title: ${stateWithNewPost.posts[0].title}`); // "New Post"