-
Notifications
You must be signed in to change notification settings - Fork 0
/
defineArrayMethods.ahk
205 lines (173 loc) · 5.76 KB
/
defineArrayMethods.ahk
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
defineArrayMethods(arr) {
arr.Prototype.some := some
arr.Prototype.every := every
arr.Prototype.filter := filter
arr.Prototype.map := map
arr.Prototype.reduce := reduce
arr.Prototype.with := with
arr.Prototype.concat := concat
arr.Prototype.unshift := unshift
arr.Prototype.toReversed := toReversed
arr.Prototype.unique := unique
some(arr, fn) {
for item in arr {
if (fn(item)) {
return true
}
}
return false
}
every(arr, fn) {
for item in arr {
if (!fn(item))
return false
}
return true
}
filter(arr, fn) {
newArray := []
for item in arr {
if (fn(item)) {
newArray.Push(item)
}
}
return newArray
}
map(arr, fn) {
newArray := []
if (fn.MaxParams = 1) {
for item in arr {
newArray.Push(fn(item))
}
} else if (fn.MaxParams = 2) {
for item in arr {
newArray.Push(fn(item, A_Index))
}
}
return newArray
}
reduce(arr, fn, initialValue) {
initIsSet := !(initialValue = 0)
accumulator := initIsSet ? initialValue : arr[1]
currentValue := initIsSet ? arr[1] : arr[2]
loopTimes := initIsSet ? arr.Length : arr.Length - 1
result := 0
loop loopTimes {
if (A_Index = 1) {
result := fn(accumulator, currentValue)
} else {
if (!(initialValue = 0)) {
result := fn(result, arr[A_Index])
} else {
result := fn(result, arr[A_Index + 1])
}
}
}
return result
}
with(arr, index, newValue) {
if (index > arr.Length) {
throw ValueError("Index out of range")
}
newArray := []
for item in arr {
newArray.Push(item)
}
newArray[index] := newValue
return newArray
}
concat(arr, val) {
newArray := arr
if (val is Array) {
for item in val {
newArray.Push(item)
}
} else {
newArray.Push(val)
}
return newArray
}
unshift(arr, val) {
newArray := arr
if (val is Array) {
for item in val.toReversed() {
newArray.InsertAt(1, item)
}
} else {
newArray.InsertAt(1, val)
}
return newArray
}
toReversed(arr) {
newArray := []
index := arr.Length
loop arr.Length {
newArray.Push(arr[index])
index--
}
return newArray
}
unique(arr){
newArray := arr
loop newArray.Length {
curItem := newArray[1]
newArray := newArray.filter(item => item != curItem)
newArray.Push(curItem)
}
return newArray
}
}
defineArrayMethods(Array)
; LSP syntax fragments for vscode-autohotkey2-lsp
;#defineArrayMethods.ahk
; /**
; * Returns true if, in the array, it finds an element for which the provided function returns true;
; * otherwise it returns false. It doesn't modify the array.
; */
; some(callbackFn) => Boolean
; /**
; * Tests whether all elements in the array pass the test implemented by the provided function.
; */
; every(callbackFn) => Boolean
; /**
; * Creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
; */
; filter(callbackFn) => Array
; /**
; * Executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
; * @param callbackFn
; * A function to execute for each element in the array. Its return value becomes the value of the accumulator parameter on the next invocation of callbackFn.
; * For the last invocation, the return value becomes the return value of reduce(). The function is called with arguments: accumulator, currentValue.
; * @param accumulator
; * The value resulting from the previous call to callbackFn. On the first call, its value is initialValue if the latter is specified; otherwise its value is array[1]
; * @param currentValue
; * The index position of currentValue in the array. On the first call, its value is 0 if initialValue is specified, otherwise 1.
; */
; reduce(callbackFn) => Any
; /**
; * Creates a new array populated with the results of calling a provided function on every element in the calling array.
; * @param callbackFn A function to execute for each element in the array. Its return value is added as a single element in the new array. The function is called with arguments: element [, index]
; * @param element
; * The current element being processed in the array.
; * @param index
; * The index of the current element being processed in the array.
; */
; map(callbackFn) => Array
; /**
; * Returns a new array with the element at the given index replaced with the given value.
; */
; with(index, newValue) => Array
; /**
; * Adds the specified elements to the end of an array and returns the new concatenated array.
; * @param element Can be an array or a single value.
; */
; concat(element) => Array
; /**
; * Adds the specified elements to the beginning of an array and returns the new concatenated array.
; * @param element Can be an array or a single value.
; */
; unshift(element) => Array
; /**
; * returns a new array with the elements in reversed order.
; */
; toReversed() => Array