-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_transformations.tg
More file actions
132 lines (106 loc) · 3.96 KB
/
array_transformations.tg
File metadata and controls
132 lines (106 loc) · 3.96 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
// array_transformations.tg - Array Transformation Methods in TargaScript
//
// This example demonstrates how to use the built-in methods for
// transforming arrays: map, filter, and reduce.
// ======================================================================
// Map Method
// ======================================================================
// The map method applies a transformation to each element in an array
// and returns a new array with the transformed values.
// Syntax: array.map(function)
print("=== Map Method Examples ===")
// Example 1: Squaring numbers
let numbers = range(1, 5)
print("Original numbers:", numbers)
// Using anonymous function for transformation
let squared = numbers.map(fn(num) {
return num * num
})
print("Squared numbers:", squared)
// Example 2: Converting strings to uppercase
let names = ["alice", "bob", "charlie", "david"]
print("Original names:", names)
let uppercase = names.map(fn(name) {
return name.upper()
})
print("Uppercase names:", uppercase)
// Example 3: Adding a prefix to each name
let prefixed = names.map(fn(name) {
return "user_" + name
})
print("Names with prefix:", prefixed)
// ======================================================================
// Filter Method
// ======================================================================
// The filter method creates a new array with elements that pass a test
// implemented by the provided function.
// Syntax: array.filter(function)
print("=== Filter Method Examples ===")
// Example 1: Filtering even numbers
let mixed_numbers = range(1, 10)
print("Original numbers:", mixed_numbers)
let even_numbers = mixed_numbers.filter(fn(num) {
return num % 2 == 0
})
print("Even numbers only:", even_numbers)
// Example 2: Filtering by string length
print("Original names:", names)
let long_names = names.filter(fn(name) {
return name.length > 4
})
print("Names longer than 4 characters:", long_names)
// Example 3: Compound filtering
let special_numbers = mixed_numbers.filter(fn(num) {
return (num > 3) && (num < 8)
})
print("Numbers between 3 and 8:", special_numbers)
// ======================================================================
// Reduce Method
// ======================================================================
// The reduce method applies a function against an accumulator and each
// element in the array to reduce it to a single value.
// Syntax: array.reduce(function, initial_value)
print("=== Reduce Method Examples ===")
// Example 1: Sum of numbers
print("Numbers:", mixed_numbers)
let sum = mixed_numbers.reduce(fn(acc, num) {
return acc + num
}, 0)
print("Sum of all numbers:", sum)
// Example 2: Finding maximum value
let max_val = mixed_numbers.reduce(fn(acc, num) {
if (num > acc) {
return num
}
return acc
}, mixed_numbers[0])
print("Maximum value:", max_val)
// Example 3: Joining strings
print("Names:", names)
let joined = names.join(", ")
print("Joined names:", joined)
// ======================================================================
// Chaining Transformations
// ======================================================================
// You can chain multiple array methods to create complex
// data processing pipelines.
print("=== Chained Transformations ===")
// Example: Square numbers, filter those greater than 20, then sum them
let original = range(1, 10)
print("Original numbers:", original)
// One-line method chaining
let result = original
.map(fn(x) { return x * x })
.filter(fn(x) { return x > 20 })
.reduce(fn(acc, x) { return acc + x }, 0)
print("Final result from chained operations:", result)
// For clarity, we can also see each step:
// Step 1: Square the numbers
let squares = original.map(fn(x) { return x * x })
print("After map (squares):", squares)
// Step 2: Filter squares > 20
let filtered = squares.filter(fn(x) { return x > 20 })
print("After filter (> 20):", filtered)
// Step 3: Sum the remaining values
let total = filtered.reduce(fn(acc, x) { return acc + x }, 0)
print("After reduce (sum):", total)