Repository navigation
Expand file tree
/
Copy pathcollections.script
More file actions
439 lines (368 loc) · 11.7 KB
/
Copy pathcollections.script
File metadata and controls
439 lines (368 loc) · 11.7 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* Collections and Data Structures in Script
*
* This example demonstrates:
* - Working with arrays and vectors
* - HashMap usage for key-value storage
* - Collection operations and iteration
* - Practical data structure examples
* - Option and Result types for safe operations
*/
fn main() {
print("=== Script Collections Tutorial ===\n")
vector_examples()
hashmap_examples()
data_processing_examples()
real_world_example()
}
/**
* Demonstrates vector operations - dynamic arrays in Script
*/
fn vector_examples() {
print("--- Vector (Dynamic Array) Examples ---")
// Create a new vector
let numbers = Vec::new()
print("Created empty vector")
// Add elements to the vector
vec_push(numbers, 10)
vec_push(numbers, 20)
vec_push(numbers, 30)
vec_push(numbers, 40)
vec_push(numbers, 50)
print("Added numbers: 10, 20, 30, 40, 50")
print("Vector length: " + vec_len(numbers))
// Access elements safely
let first_element = vec_get(numbers, 0)
match first_element {
Some(value) => print("First element: " + value),
None => print("No first element (empty vector)")
}
let middle_element = vec_get(numbers, 2)
match middle_element {
Some(value) => print("Middle element (index 2): " + value),
None => print("Index 2 is out of bounds")
}
// Try to access an invalid index
let invalid_element = vec_get(numbers, 10)
match invalid_element {
Some(value) => print("Element at index 10: " + value),
None => print("Index 10 is out of bounds (as expected)")
}
// Remove elements
let popped = vec_pop(numbers)
match popped {
Some(value) => print("Popped element: " + value),
None => print("Vector was empty, nothing to pop")
}
print("Vector length after pop: " + vec_len(numbers))
// Iterate through all elements
print("All remaining elements:")
let length = vec_len(numbers)
for i in 0..length {
match vec_get(numbers, i) {
Some(value) => print(" [" + i + "] = " + value),
None => continue
}
}
print("")
}
/**
* Demonstrates HashMap operations - key-value storage
*/
fn hashmap_examples() {
print("--- HashMap (Key-Value Storage) Examples ---")
// Create a new HashMap for storing student grades
let grades = HashMap::new()
print("Created empty HashMap for student grades")
// Insert key-value pairs
hashmap_insert(grades, "Alice", 95)
hashmap_insert(grades, "Bob", 87)
hashmap_insert(grades, "Charlie", 92)
hashmap_insert(grades, "Diana", 89)
print("Added grades for Alice (95), Bob (87), Charlie (92), Diana (89)")
// Retrieve values
let alice_grade = hashmap_get(grades, "Alice")
match alice_grade {
Some(grade) => print("Alice's grade: " + grade),
None => print("Alice's grade not found")
}
let eve_grade = hashmap_get(grades, "Eve")
match eve_grade {
Some(grade) => print("Eve's grade: " + grade),
None => print("Eve's grade not found (as expected)")
}
// Check if keys exist
if hashmap_contains_key(grades, "Bob") {
print("Bob's grade is recorded")
} else {
print("Bob's grade is not recorded")
}
if hashmap_contains_key(grades, "Frank") {
print("Frank's grade is recorded")
} else {
print("Frank's grade is not recorded (as expected)")
}
// Update existing values
hashmap_insert(grades, "Alice", 98) // Alice improved her grade!
let updated_grade = hashmap_get(grades, "Alice")
match updated_grade {
Some(grade) => print("Alice's updated grade: " + grade),
None => print("Failed to update Alice's grade")
}
print("")
}
/**
* Shows practical data processing with collections
*/
fn data_processing_examples() {
print("--- Data Processing Examples ---")
// Create a list of test scores
let scores = Vec::new()
vec_push(scores, 95)
vec_push(scores, 87)
vec_push(scores, 92)
vec_push(scores, 76)
vec_push(scores, 89)
vec_push(scores, 94)
vec_push(scores, 82)
print("Test scores: 95, 87, 92, 76, 89, 94, 82")
// Calculate statistics
let total = calculate_sum(scores)
let average = calculate_average(scores)
let highest = find_maximum(scores)
let lowest = find_minimum(scores)
let passing_count = count_passing_scores(scores, 80)
print("Statistics:")
print(" Total: " + total)
print(" Average: " + average)
print(" Highest: " + highest)
print(" Lowest: " + lowest)
print(" Passing (≥80): " + passing_count + " out of " + vec_len(scores))
// Filter scores
let high_scores = filter_high_scores(scores, 90)
print("High scores (≥90):")
let high_count = vec_len(high_scores)
for i in 0..high_count {
match vec_get(high_scores, i) {
Some(score) => print(" " + score),
None => continue
}
}
print("")
}
/**
* Real-world example: A simple contact management system
*/
fn real_world_example() {
print("--- Real-World Example: Contact Management ---")
// Create contact database using HashMap
let contacts = HashMap::new()
// Add contacts
add_contact(contacts, "Alice Johnson", "alice@email.com")
add_contact(contacts, "Bob Smith", "bob@email.com")
add_contact(contacts, "Charlie Brown", "charlie@email.com")
print("Added 3 contacts to the database")
// Look up contacts
lookup_contact(contacts, "Alice Johnson")
lookup_contact(contacts, "David Wilson") // Doesn't exist
// Create a list of favorite contacts
let favorites = Vec::new()
vec_push(favorites, "Alice Johnson")
vec_push(favorites, "Charlie Brown")
print("\nFavorite contacts:")
display_favorite_contacts(contacts, favorites)
// Simulate contact search
let search_results = search_contacts_by_domain(contacts, "email.com")
print("\nContacts with 'email.com' domain: " + vec_len(search_results) + " found")
print("")
}
// Helper functions for data processing
/**
* Calculate the sum of all numbers in a vector
*/
fn calculate_sum(numbers: Vec<i32>) -> i32 {
let total = 0
let length = vec_len(numbers)
for i in 0..length {
match vec_get(numbers, i) {
Some(num) => total = total + num,
None => continue
}
}
total
}
/**
* Calculate the average of all numbers in a vector
*/
fn calculate_average(numbers: Vec<i32>) -> f32 {
let length = vec_len(numbers)
if length == 0 {
return 0.0
}
let total = calculate_sum(numbers)
total as f32 / length as f32
}
/**
* Find the maximum value in a vector
*/
fn find_maximum(numbers: Vec<i32>) -> i32 {
let length = vec_len(numbers)
if length == 0 {
return 0 // or handle error appropriately
}
let max = vec_get(numbers, 0).unwrap_or(0)
for i in 1..length {
match vec_get(numbers, i) {
Some(num) => {
if num > max {
max = num
}
},
None => continue
}
}
max
}
/**
* Find the minimum value in a vector
*/
fn find_minimum(numbers: Vec<i32>) -> i32 {
let length = vec_len(numbers)
if length == 0 {
return 0 // or handle error appropriately
}
let min = vec_get(numbers, 0).unwrap_or(0)
for i in 1..length {
match vec_get(numbers, i) {
Some(num) => {
if num < min {
min = num
}
},
None => continue
}
}
min
}
/**
* Count how many scores are above or equal to the passing threshold
*/
fn count_passing_scores(scores: Vec<i32>, threshold: i32) -> i32 {
let count = 0
let length = vec_len(scores)
for i in 0..length {
match vec_get(scores, i) {
Some(score) => {
if score >= threshold {
count = count + 1
}
},
None => continue
}
}
count
}
/**
* Filter scores that are above or equal to the threshold
*/
fn filter_high_scores(scores: Vec<i32>, threshold: i32) -> Vec<i32> {
let high_scores = Vec::new()
let length = vec_len(scores)
for i in 0..length {
match vec_get(scores, i) {
Some(score) => {
if score >= threshold {
vec_push(high_scores, score)
}
},
None => continue
}
}
high_scores
}
// Helper functions for contact management
/**
* Add a contact to the database
*/
fn add_contact(contacts: HashMap<string, string>, name: string, email: string) {
hashmap_insert(contacts, name, email)
print("Added contact: " + name + " (" + email + ")")
}
/**
* Look up a contact by name
*/
fn lookup_contact(contacts: HashMap<string, string>, name: string) {
let email = hashmap_get(contacts, name)
match email {
Some(email_addr) => print("Found " + name + ": " + email_addr),
None => print("Contact '" + name + "' not found")
}
}
/**
* Display favorite contacts with their email addresses
*/
fn display_favorite_contacts(contacts: HashMap<string, string>, favorites: Vec<string>) {
let fav_count = vec_len(favorites)
for i in 0..fav_count {
match vec_get(favorites, i) {
Some(name) => {
match hashmap_get(contacts, name) {
Some(email) => print(" ⭐ " + name + " (" + email + ")"),
None => print(" ⭐ " + name + " (email not found)")
}
},
None => continue
}
}
}
/**
* Search for contacts by email domain
*/
fn search_contacts_by_domain(contacts: HashMap<string, string>, domain: string) -> Vec<string> {
let results = Vec::new()
// Note: In a real implementation, you'd iterate through the HashMap
// This is a simplified version showing the concept
// For demonstration, we'll manually check known contacts
let test_names = Vec::new()
vec_push(test_names, "Alice Johnson")
vec_push(test_names, "Bob Smith")
vec_push(test_names, "Charlie Brown")
let name_count = vec_len(test_names)
for i in 0..name_count {
match vec_get(test_names, i) {
Some(name) => {
match hashmap_get(contacts, name) {
Some(email) => {
if contains(email, domain) {
vec_push(results, name)
}
},
None => continue
}
},
None => continue
}
}
results
}
/**
* Key Concepts Demonstrated:
*
* 1. VECTOR OPERATIONS: Creating, adding, removing, and accessing elements
* 2. SAFE ACCESS: Using Option types to handle potential failures
* 3. HASHMAP USAGE: Key-value storage for efficient lookups
* 4. DATA PROCESSING: Calculating statistics and filtering data
* 5. ITERATION PATTERNS: Safe iteration through collections
* 6. ERROR HANDLING: Graceful handling of edge cases
* 7. REAL-WORLD APPLICATIONS: Practical examples like contact management
* 8. HELPER FUNCTIONS: Breaking down complex operations into smaller functions
*
* Try This:
* - Create a shopping cart system using HashMap and Vec
* - Build a simple inventory management system
* - Implement a basic grading system for a school
* - Create a word frequency counter using HashMap
* - Build a simple music playlist manager
*/
// Run the examples
main()