• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1%# Copyright (c) 2021-2024 Huawei Device Co., Ltd.
2%# Licensed under the Apache License, Version 2.0 (the "License");
3%# you may not use this file except in compliance with the License.
4%# You may obtain a copy of the License at
5%#
6%# http://www.apache.org/licenses/LICENSE-2.0
7%#
8%# Unless required by applicable law or agreed to in writing, software
9%# distributed under the License is distributed on an "AS IS" BASIS,
10%# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11%# See the License for the specific language governing permissions and
12%# limitations under the License.
13%
14% require_relative 'Array_code'
15
16/**
17 * Takes an integer value and returns the item at that index,
18 * allowing for positive and negative integers. Negative integers count back
19 * from the last item in the array.
20 *
21 * @param index Zero-based index of the array element to be returned.
22 * Negative index counts back from the end of the array — if `index` < 0, index + `array.length()` is accessed.
23 *
24 * @returns The element in the array matching the given index.
25 * Returns undefined if `index` < `-length()` or `index` >= `length()`.
26 */
27<%= access_public %> <%= override %> at<%= this_generic %>(<%= this_arg %>index: number): <%= el_type_boxed %> | undefined {
28    return <%= this_call.('at') %>index as int)
29}
30
31/**
32 * Creates a new `Array` from this `Array` instance and given `Array` instance.
33 *
34 * @param other to concatenate into a new array.
35 *
36 * @returns New `Array` instance, constructed from `this` and given `other` instances of `Array` class.
37 */
38// <%= access_public %> <%= override %> concat<%= this_generic %>(<%= this_arg %>...items: (<%= el_type %> | Concat<%= this_type %>)[]): <%= this_type %> {
39//     throw new Error("not implemented")
40// }
41
42<%= access_public %> concat<%= this_generic %>(<%= this_arg %>...items: ConcatArray<<%= el_type %>>[]): <%= this_type %> {
43    let totalAdd = <%= this_len_int %>;
44    for (let i = 0; i < items.length; i++) {
45        totalAdd += items[i].length as int
46    }
47
48    const buf = <%= make_buffer.('totalAdd') %>;
49
50    for (let i = 0; i < <%= this_len_int %>; i++) {
51        buf[i] = <%= get_unsafe.(this, 'i') %>;
52    }
53
54    let insertTo = <%= this_len_int %>;
55    for (let i = 0; i < items.length; i++) {
56        const arr = items[i]
57        const len = arr.length as int
58        for (let j = 0; j < len; j++) {
59            buf[insertTo++] = arr.$_get(j)
60        }
61    }
62
63    return <%= from_buffer.('buf') %>;
64}
65
66/**
67 * Takes an integer value and returns the item at that index,
68 * allowing for positive and negative integers. Negative integers count back
69 * from the last item in the array.
70 *
71 * @param index Zero-based index of the array element to be returned.
72 * Negative index counts back from the end of the array — if `index` < 0, index + `array.length()` is accessed.
73 *
74 * @returns The element in the array matching the given index.
75 * Returns undefined if `index` < `-length()` or `index` >= `length()`.
76 */
77<%= access_public %> at<%= this_generic %>(<%= this_arg %>index: int): <%= el_type_boxed %> | undefined {
78    let len = <%= this_len_int %>;
79    let k: int;
80    if (index >= 0) {
81        k = index;
82    } else {
83        k = len + index;
84    }
85
86    if (k < 0 || k >= len) {
87        return undefined;
88    }
89
90    return <%= get_unsafe.(this, 'k') %>;
91}
92
93/**
94 * Makes a shallow copy of the Array part to another location in the same Array and returns it without modifying its length.
95 *
96 * @param target index at which to copy the sequence
97 *
98 * @param start index at which to start copying elements from
99 *
100 * @param end index at which to end copying elements from
101 *
102 * @returns this array after transformation
103 */
104<%= access_public %> copyWithin<%= this_generic %>(<%= this_arg %>target: number, start: number, end?: Number): <%= this_return_type %> {
105    <%= this_call.('copyWithin') %>target as int, start as int, asIntOrDefault(end, <%= this_len_int %>));
106    return <%= this %>;
107}
108
109/**
110 * Makes a shallow copy of the Array part to another location in the same Array and returns it without modifying its length.
111 *
112 * @param target index at which to copy the sequence
113 *
114 * @param start index at which to start copying elements from
115 *
116 * @param end index at which to end copying elements from
117 *
118 * @returns this array after transformation
119 */
120<%= access_public %> copyWithin<%= this_generic %>(<%= this_arg %>target: int, start: int, end: int): <%= this_return_type %> {
121    target = normalizeIndex(target, <%= this_len_int %>)
122    start = normalizeIndex(start, <%= this_len_int %>)
123    end = normalizeIndex(end, <%= this_len_int %>)
124
125    if (end <= start) {
126        return <%= this %>;
127    }
128
129    if (target <= start) {
130        while (start < end) {
131            const read = <%= get_unsafe.(this, 'start++') %>;
132            <%= set_unsafe.(this, 'target++', 'read') %>;
133        }
134    } else {
135        let len = end - start;
136        if (target + len > <%= this_len_int %>) {
137            len = <%= this_len_int %> - target
138        }
139        for (let i = 0; i < len; i++) {
140            const read = <%= get_unsafe.(this, 'start + len - 1 - i') %>;
141            <%= set_unsafe.(this, 'target + len - 1 - i', 'read') %>;
142        }
143    }
144
145    return <%= this %>;
146}
147
148/**
149 * Makes a shallow copy of the Array part to another location in the same Array and returns it without modifying its length.
150 *
151 * @param target index at which to copy the sequence
152 *
153 * @param start index at which to start copying elements from
154 *
155 * @returns this array after transformation
156 */
157<%= access_public %> copyWithin<%= this_generic %>(<%= this_arg %>target: int, start: int): <%= this_return_type %> {
158    <%= this_call.('copyWithin') %>target, start, <%= this_len_int %>);
159    return <%= this %>;
160}
161
162/**
163 * Makes a shallow copy of the Array part to another location in the same Array and returns it without modifying its length.
164 *
165 * @param target index at which to copy the sequence
166 *
167 * @returns this array after transformation
168 */
169<%= access_public %> copyWithin<%= this_generic %>(<%= this_arg %>target: int): <%= this_return_type %> {
170    <%= this_call.('copyWithin') %>target, 0, <%= this_len_int %>);
171    return <%= this %>;
172}
173
174/**
175 * Changes all elements in the Array to a static value, from a start index to an end index
176 *
177 * @param value to fill the array with
178 *
179 * @param start index at which to start filling
180 *
181 * @param end index at which to end filling, but not including
182 *
183 * @returns this array after transformation
184 */
185<%= access_public %> fill<%= this_generic %>(<%= this_arg %>value: <%= el_type %>, start?: Number, end?: Number): <%= this_return_type %> {
186    <%= this_call.('fill') %>value, asIntOrDefault(start, 0), asIntOrDefault(end, <%= this_len_int %>));
187    return <%= this %>;
188}
189
190/**
191 * Changes all elements in the Array to a static value, from a start index to an end index
192 *
193 * @param value to fill the array with
194 *
195 * @param start index at which to start filling
196 *
197 * @param end index at which to end filling, but not including
198 *
199 * @returns this array after transformation
200 */
201<%= access_public %> fill<%= this_generic %>(<%= this_arg %>value: <%= el_type %>, start: int, end: int): <%= this_return_type %> {
202    start = normalizeIndex(start, <%= this_len_int %>);
203    end = normalizeIndex(end, <%= this_len_int %>)
204
205    for (let i = start; i < end; i++) {
206        <%= set_unsafe.(this, 'i', 'value') %>;
207    }
208
209    return <%= this %>;
210}
211
212% TemplateData::get_lambda_data.each { |lambda_args_params|
213%   lambda_params_num, lambda_args, lambda_params, lambda_args2 = lambda_args_params
214/**
215 * Returns the value of the first element in the array where predicate is true, and undefined
216 * otherwise.
217 *
218 * @param predicate find calls predicate once for each element of the array, in ascending
219 * order, until it finds one where predicate returns true. If such an element is found, find
220 * immediately returns that element value. Otherwise, find returns undefined.
221 *
222 * @returns the value of the first element in the array or undefined
223 */
224<%= access_public %> <%= override %> find<%= this_generic %>(<%= this_arg %>predicate: (value: <%= el_type %><%= lambda_params %>) => boolean): <%= el_type_boxed %> | undefined {
225    const res = <%= this_call.('findIndex') %>predicate)
226    if (res == -1) {
227        return undefined
228    }
229    return <%= get_unsafe.(this, 'res as int') %>;
230}
231
232/**
233 * Returns the index of the first element in the array where predicate is true, and -1
234 * otherwise.
235 *
236 * @param predicate find calls predicate once for each element of the array, in ascending
237 * order, until it finds one where predicate returns true. If such an element is found,
238 * findIndex immediately returns that element index. Otherwise, findIndex returns -1.
239 *
240 * @returns found element index or -1 otherwise
241 */
242<%= access_public %> <%= override %> findIndex<%= this_generic %>(<%= this_arg %>predicate: (value: <%= el_type %><%= lambda_params %>) => boolean): number {
243    for (let i = 0; i < <%= this_len_int %>; i++) {
244        if (predicate(<%= get_unsafe.(this, 'i') %><%= lambda_args %>)) {
245            return i;
246        }
247    }
248    return -1;
249}
250
251/**
252 * Iterates the array in reverse order and returns the value of the first element
253 * that satisfies the provided testing function
254 *
255 * @param predicate testing function
256 *
257 * @returns found element or undefined otherwise
258 */
259<%= access_public %> <%= override %> findLast<%= this_generic %>(<%= this_arg %>predicate: (elem: <%= el_type %><%= lambda_params %>) => boolean): <%= el_type_boxed %> | undefined {
260    for (let i = <%= this_len_int %> - 1; i >= 0; i--) {
261        const val = <%= get_unsafe.(this, 'i') %>;
262        if (predicate(val<%= lambda_args %>)) {
263            return val;
264        }
265    }
266    return undefined;
267}
268
269/**
270 * Determines whether all the members of an array satisfy the specified test.
271 *
272 * @param predicate A function that accepts up to three arguments. The every method calls
273 * the predicate function for each element in the array until the predicate returns a value
274 * which is coercible to the Boolean value false, or until the end of the array.
275 *
276 * @returns `true` if `predicate` returns a `true` value for every array element. Otherwise, `false`.
277 */
278<%= access_public %> <%= override %> every<%= this_generic %>(<%= this_arg %>predicate: (value: <%= el_type %><%= lambda_params %>) => boolean): boolean {
279    for (let i = 0; i < <%= this_len_int %>; i++) {
280        if (!predicate(<%= get_unsafe.(this, 'i') %><%= lambda_args %>)) {
281            return false
282        }
283    }
284    return true;
285}
286
287/**
288 * Determines whether the specified callback function returns true for any element of an array.
289 *
290 * @param predicate A function that accepts up to three arguments. The some method calls
291 * the predicate function for each element in the array until the predicate returns a value
292 * which is coercible to the Boolean value true, or until the end of the array.
293 *
294 * @returns `true` if `predicate` returns a `true` value for at least one array element. Otherwise, `false`.
295 */
296<%= access_public %> <%= override %> some<%= this_generic %>(<%= this_arg %>predicate: (value: <%= el_type %><%= lambda_params %>) => boolean): boolean {
297    for (let i = 0; i < <%= this_len_int %>; i++) {
298        if (predicate(<%= get_unsafe.(this, 'i') %><%= lambda_args %>)) {
299            return true
300        }
301    }
302    return false
303}
304
305%   if lambda_params_num != 1
306/**
307 * Returns the elements of an array that meet the condition specified in a callback function.
308 *
309 * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
310 *
311 * @returns New `Array` instance constructed from `this` with elements filtered using test function `predicate`.
312 */
313<%= access_public %> <%= override %> filter<%= this_generic %>(<%= this_arg %>predicate: (value: <%= el_type %><%= lambda_params %>) => boolean): <%= this_type %> {
314    return <%= this_call.('filter') %>(value: <%= el_type %>, index: number): boolean => predicate(value<%= lambda_args2 %>));
315}
316%   end
317
318/**
319 * Iterates the array in reverse order and returns the index of
320 * the first element that satisfies the provided testing function.
321 * If no elements satisfy the testing function, -1 is returned.
322 *
323 * @param predicate testing function
324 *
325 * @returns index of first element satisfying to predicate, -1 if no such element
326 */
327<%= access_public %> <%= override %> findLastIndex<%= this_generic %>(<%= this_arg %>predicate: (element: <%= el_type %><%= lambda_params %>) => boolean): number {
328    for (let i = <%= this_len_int %> - 1; i >= 0; i--) {
329        if (predicate(<%= get_unsafe.(this, 'i') %><%= lambda_args %>)) {
330            return i
331        }
332    }
333    return -1
334}
335
336/**
337 * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
338 *
339 * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
340 *
341 * @returns a result after applying callbackfn over all elements of the Array
342 */
343<%= access_public %> <%= override %> reduce<%= this_generic %>(<%= this_arg %>callbackfn: (previousValue: <%= el_type %>, currentValue: <%= el_type %><%= lambda_params %>) => <%= el_type %>): <%= el_type %> {
344    if (<%= this_len_int %> == 0) {
345        throw new TypeError("Reduce of empty array with no initial value")
346    }
347    let acc: <%= el_type %> = <%= get_unsafe.(this, '0') %>;
348    for (let i = 1; i < <%= this_len_int %>; i++) {
349        acc = callbackfn(acc, <%= get_unsafe.(this, 'i') %><%= lambda_args %>)
350    }
351    return acc
352}
353
354/**
355 * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
356 *
357 * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
358 *
359 * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
360 *
361 * @returns a result after applying callbackfn over all elements of the Array
362 */
363<%= access_public %> <%= override %> reduce<<%= this_generic_one %>U = <%= el_type %>>(<%= this_arg %>callbackfn: (previousValue: U, currentValue: <%= el_type %><%= lambda_params %>) => U, initialValue: U): U {
364    let acc = initialValue
365    for (let i = 0; i < <%= this_len_int %>; i++) {
366        acc = callbackfn(acc, <%= get_unsafe.(this, 'i') %><%= lambda_args %>)
367    }
368    return acc
369}
370
371/**
372 * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
373 *
374 * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
375 *
376 * @returns a result after applying callbackfn over all elements of the Array
377 */
378<%= access_public %> <%= override %> reduceRight<%= this_generic %>(<%= this_arg %>callbackfn: (previousValue: <%= el_type %>, currentValue: <%= el_type %><%= lambda_params %>) => <%= el_type %>): <%= el_type %> {
379    if (<%= this_len_int %> == 0) {
380        throw new TypeError("Reduce of empty array with no initial value")
381    }
382    let acc: <%= el_type %> = <%= get_unsafe.(this, "#{this_len_int} - 1") %>;
383    for (let i = <%= this_len_int %> - 2; i >= 0; i--) {
384        acc = callbackfn(acc, <%= get_unsafe.(this, 'i') %><%= lambda_args %>)
385    }
386    return acc
387}
388
389/**
390 * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
391 *
392 * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
393 *
394 * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
395 *
396 * @returns a result after applying callbackfn over all elements of the Array
397 */
398<%= access_public %> <%= override %> reduceRight<<%= this_generic_one %>U>(<%= this_arg %>callbackfn: (previousValue: U, currentValue: <%= el_type %><%= lambda_params %>) => U, initialValue: U): U {
399    let acc = initialValue
400    for (let i = <%= this_len_int %> - 1; i >= 0; i--) {
401        acc = callbackfn(acc, <%= get_unsafe.(this, 'i') %><%= lambda_args %>)
402    }
403    return acc
404}
405
406/**
407 * Performs the specified action for each element in an array.
408 *
409 * @param callbackfn  A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
410 */
411<%= access_public %> <%= override %> forEach<%= this_generic %>(<%= this_arg %>callbackfn: (value: <%= el_type %><%= lambda_params %>) => void): void {
412    const len0 = <%= this_len_int %>;
413    for (let i = 0; i < len0; i++) {
414        callbackfn(<%= get_unsafe.(this, 'i') %><%= lambda_args %>)
415    }
416}
417
418% }
419
420/**
421 * Creates a new `Array` object and populates it with elements of `this` instance of `Array` class
422 * selected from `start` to `end` (`end` not included) where `start` and `end` represent the index of items in that array.
423 *
424 * @param start zero-based index at which to start extraction
425 *
426 * @param end zero-based index at which to end extraction. `slice()` extracts up to but not including end.
427 *
428 * @returns `Array` instance, constructed from extracted elements of `this` instance.
429 */
430<%= access_public %> <%= override %> slice<%= this_generic %>(<%= this_arg %>start?: Number, end?: Number): <%= this_type %> {
431    const len: int = <%= this_len_int %>;
432    return <%= this_call.('slice') %>asIntOrDefault(start, 0), asIntOrDefault(end, len))
433}
434
435/**
436 * Creates a new `Array` object and populates it with elements of `this` instance of `Array` class
437 * selected from `start` to `end` (`end` not included) where `start` and `end` represent the index of items in that array.
438 *
439 * @param start zero-based index at which to start extraction
440 *
441 * @param end zero-based index at which to end extraction. `slice()` extracts up to but not including end.
442 *
443 * @returns `Array` instance, constructed from extracted elements of `this` instance.
444 */
445<%= access_public %> slice<%= this_generic %>(<%= this_arg %>start: int, end: int): <%= this_type %> {
446    const len: int = <%= this_len_int %>;
447    const relStart = normalizeIndex(start, len)
448    const relEnd = normalizeIndex(end, len)
449
450    let count = relEnd - relStart;
451    if (count < 0) {
452        count = 0;
453    }
454    let res = <%= make_buffer.('count') %>
455    for (let i = 0; i < count; i++) {
456        res[i] = <%= get_unsafe.(this, 'relStart + i') %>;
457    }
458
459    return <%= from_buffer.('res') %>
460}
461
462/**
463 * Creates a new `Array` object and populates it with elements of `this` instance of `Array` class
464 * selected from `start` to `Int.MAX_VALUE`, which means 'to the end of an array'.
465 *
466 * @param start zero-based index at which to start extraction
467 *
468 * @returns `Array` instance, constructed from extracted elements of `this` instance.
469 */
470<%= access_public %> slice<%= this_generic %>(<%= this_arg %>start: int): <%= this_type %> {
471    return <%= this_call.('slice') %>start, Int.MAX_VALUE as int);
472}
473
474<% if access_public == "export function" %><% #generate code for BuiltinArray.sts %>
475/**
476 * Returns the last index at which a given element can be found in the array,
477 * or -1 if it is not present. The array is searched backwards, starting at fromIndex.
478 *
479 * @param element element to locate in the array.
480 * @param fromIndex zero-based index at which to start searching backwards.
481 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used.
482 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned.
483 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched.
484 *
485 * @returns The last index of the element in the array; -1 if not found.
486 */
487<%= access_public %> lastIndexOf<%= this_generic %>(<%= this_arg %>element: <%= el_type %>, fromIndex: int): int {
488    if (<%= this_len_int %> == 0) {
489        return -1;
490    }
491    let n = fromIndex;
492    let k: int;
493    if (n >= 0) {
494        k = min(<%= this_len_int %> - 1, n);
495    } else {
496        k = <%= this_len_int %> + n;
497    }
498
499    while (k >= 0) {
500        if (__runtimeEquals(<%= get_unsafe.(this, 'k') %>, element)) {
501            return k;
502        }
503        k--;
504    }
505    return -1;
506}
507
508/**
509 * Returns the last index at which a given element can be found in the array,
510 * or -1 if it is not present. The array is searched backwards, starting at fromIndex.
511 *
512 * @param element element to locate in the array.
513 * @param fromIndex zero-based index at which to start searching backwards.
514 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used.
515 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned.
516 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched.
517 *
518 * @returns The last index of the element in the array; -1 if not found.
519 */
520<%= access_public %> <%= override %> lastIndexOf<%= this_generic %>(<%= this_arg %>element: <%= el_type %>, fromIndex?: Number): number {
521    return <%= this_call.('lastIndexOf') %>element, asIntOrDefault(fromIndex, <%= this_len_int %> - 1));
522}
523<% else %><% #generate code for Array.sts %>
524/**
525 * Returns the last index at which a given element can be found in the array,
526 * or -1 if it is not present. The array is searched backwards, starting at fromIndex.
527 *
528 * @param searchElement element to locate in the array.
529 *
530 * @param fromIndex zero-based index at which to start searching backwards.
531 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used.
532 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned.
533 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched.
534 * If `fromIndex` is undefined then `fromIndex = 0`.
535 * If `fromIndex` is ommited then `fromIndex = length()-1`.
536 *
537 * @returns The last index of the element in the array; -1 if not found.
538 */
539 <%= access_public %> lastIndexOf<%= this_generic %>(<%= this_arg %>searchElement: T, fromIndex: int): int {
540    if (<%= this_len_int %> == 0) {
541        return -1;
542    }
543    let n = fromIndex;
544    let k: int;
545    if (n >= 0) {
546        k = min(<%= this_len_int %> - 1, n);
547    } else {
548        k = <%= this_len_int %> + n;
549    }
550
551    while (k >= 0) {
552        if (__runtimeEquals(<%= get_unsafe.(this, 'k') %>, searchElement)) {
553            return k;
554        }
555        k--;
556    }
557    return -1;
558}
559
560/**
561 * Returns the last index at which a given element can be found in the array,
562 * or -1 if it is not present. The array is searched backwards, starting at fromIndex.
563 *
564 * @param searchElement element to locate in the array.
565 *
566 * @param fromIndex zero-based index at which to start searching backwards.
567 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used.
568 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned.
569 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched.
570 * If `fromIndex` is undefined then `fromIndex = 0`.
571 * If `fromIndex` is ommited then `fromIndex = length()-1`.
572 *
573 * @returns The last index of the element in the array; -1 if not found.
574 */
575<%= access_public %> lastIndexOf<%= this_generic %>(<%= this_arg %>searchElement: <%= el_type %>, fromIndex: number|undefined): number {
576    return <%= this_call.('lastIndexOf') %>searchElement, asIntOrDefault(fromIndex, 0));
577}
578
579/**
580 * Returns the last index at which a given element can be found in the array,
581 * or -1 if it is not present. The array is searched backwards, starting at fromIndex.
582 *
583 * @param searchElement element to locate in the array.
584 *
585 * @param fromIndex zero-based index at which to start searching backwards.
586 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used.
587 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned.
588 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched.
589 * If `fromIndex` is undefined then `fromIndex = 0`.
590 * If `fromIndex` is ommited then `fromIndex = length()-1`.
591 *
592 * @returns The last index of the element in the array; -1 if not found.
593 */
594<%= access_public %> lastIndexOf<%= this_generic %>(<%= this_arg %>searchElement: <%= el_type %>): number {
595    return <%= this_call.('lastIndexOf') %>searchElement, <%= this_len_int %> - 1);
596}
597<% end %>
598
599/**
600 * Creates and returns a new string by concatenating all of the elements in an `Array`,
601 * separated by a specified separator string.
602 * If the array has only one item, then that item will be returned without using the separator.
603 *
604 * @param sep specifies a separator
605 *
606 * @returns A string with all array elements joined. If `length()` is 0, the empty string is returned.
607 */
608<%= access_public %> <%= override %> join<%= this_generic %>(<%= this_arg %>sep?: String): string {
609    const sepReal = __runtimeIsSameReference(sep, undefined) ? "," : sep!
610    let sb = new StringBuilder()
611    for (let i: int = 0; i < <%= this_len_int %>; i++) {
612        const tmp = <%= get_unsafe.(this, 'i') %>
613        // NOTE(aleksander-sotov) We can't move if from loop due to 18709
614        if (i != 0) {
615            sb.append(sepReal);
616        }
617        <% if el_type == "T" %>// NOTE(aleksander-sotov) We can't call String constructor here due to internal issue 18583
618        if (!__runtimeIsSameReference(tmp, undefined)) {
619            sb.append(new String(tmp))
620        } else {
621            sb.append("undefined")
622        }<% else %>sb.append(tmp)<% end %>
623    }
624
625    return sb.toString();
626}
627
628/**
629 * Returns a string representing the specified array and its elements.
630 *
631 * @returns string representation
632 */
633<%= access_public %> <%= override %> toString<%= this_generic %>(<%= this_arg %>): string {
634    return <%= this_call.('join') %>",");
635}
636
637/**
638 * Returns a locale string representing the specified array and its elements.
639 *
640 * @param locales
641 *
642 * @param options
643 *
644 * @returns string representation
645 */
646<%= access_public %> toLocaleString<%= this_generic %>(<%= this_arg %>locales: Object, options: Object): string {
647    throw new Error("Array.toLocaleString: not implemented")
648}
649
650/**
651 * Returns a locale string representing the specified array and its elements.
652 *
653 * @param options
654 *
655 * @returns string representation
656 */
657<%= access_public %> toLocaleString<%= this_generic %>(<%= this_arg %>locales: Object): string {
658    return <%= this_call.('toLocaleString') %>new Object(), new Object())
659}
660
661/**
662 * Returns a locale string representing the specified array and its elements.
663 *
664 * @returns string representation
665 */
666<%= access_public %> <%= override %> toLocaleString<%= this_generic %>(<%= this_arg %>): string {
667    const sb = new StringBuilder()
668    const len = <%= this_len_int %>;
669    for (let i = 0; i < len; i++) {
670        if (i != 0) {
671            sb.append(",")
672        }
673        let x = <%= get_unsafe.(this, 'i') %> as NullishType;
674        if (!__runtimeIsSameReference(null, x) && !__runtimeIsSameReference(undefined, x)) {
675            sb.append(x!.toLocaleString())
676        }
677    }
678    return sb.toString()
679}
680
681/**
682 * Copying version of the splice() method.
683 *
684 * @param start index
685 *
686 * @param delete number of items after start index
687 *
688 * @returns a new Array with some elements removed and/or replaced at a given index.
689 */
690<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start?: Number, delete?: Number): <%= this_type %> {
691    const len = <%= this_len_int %>;
692    return <%= this_call.('toSpliced') %>asIntOrDefault(start, len), asIntOrDefault(delete, len))
693}
694
695/**
696 * Copying version of the splice() method.
697 *
698 * @param start index
699 *
700 * @param delete number of items after start index
701 *
702 * @returns a new Array with some elements removed and/or replaced at a given index.
703 */
704<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start: number, delete: number, ...items: <%= el_type %>[]): <%= this_type %> {
705    const len = <%= this_len_int %>;
706    return <%= this_call.('toSpliced') %>start as int, delete as int, ...items)
707}
708
709/**
710 * Copying version of the splice() method.
711 *
712 * @param start index
713 *
714 * @param delete number of items after start index
715 *
716 * @returns a new Array with some elements removed and/or replaced at a given index.
717 */
718<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start: int, delete: int, ...items: <%= el_type %>[]): <%= this_type %> {
719    const len = <%= this_len_int %>;
720    start = normalizeIndex(start, len);
721    if (delete < 0) {
722        delete = 0;
723    } else if (delete > len) {
724        delete = len;
725    }
726    if (start > len - delete) {
727        delete = len - start
728    }
729    const res = <%= make_buffer.('len - delete + items.length') %>;
730    for (let i = 0; i < start; i++) {
731        res[i] = <%= get_unsafe.(this, 'i') %>
732    }
733    for (let i = 0; i < items.length; i++) {
734        res[start + i] = items[i]
735    }
736    for (let i = start + delete; i < len; i++) {
737        res[i - delete + items.length] = <%= get_unsafe.(this, 'i') %>
738    }
739    return <%= from_buffer.('res') %>;
740}
741
742/**
743 * Copying version of the splice() method.
744 *
745 * @param start index
746 *
747 * @returns a new Array with some elements removed and/or replaced at a given index.
748 */
749<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start: int): <%= this_type %> {
750    return <%= this_call.('toSpliced') %>start, <%= this_len_int %>)
751}
752
753/**
754 * Checks whether an Array includes a certain value among its entries,
755 * returning true or false as appropriate.
756 *
757 * @param val value to search
758 *
759 * @param fromIndex start index
760 *
761 * @returns true if val is in Array
762 */
763<%= access_public %> <%= override %> includes<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex?: Number): boolean {
764    const len = <%= this_len_int %>;
765    const fi = normalizeIndex(asIntOrDefault(fromIndex, 0), len);
766% if ['float', 'double', 'number'].include?(el_type)
767    if (isNaN(val)) {
768        for (let i = fi; i < len; i++) {
769            if (isNaN(<%= get_unsafe.(this, 'i') %>)) {
770                return true;
771            }
772        }
773        return false;
774    }
775% end
776% if ['boolean', 'byte', 'short', 'int', 'long', 'char', 'float', 'double', 'number'].include?(el_type)
777    for (let i = fi; i < len; i++) {
778        if (val == <%= get_unsafe.(this, 'i') %>) {
779            return true;
780        }
781    }
782% else
783    for (let i = fi; i < len; i++) {
784        if (__runtimeSameValueZero(val, <%= get_unsafe.(this, 'i') %>)) {
785            return true;
786        }
787    }
788% end
789    return false;
790}
791
792/**
793 * Returns the first index at which a given element
794 * can be found in the array, or -1 if it is not present.
795 *
796 * @param val value to search
797 *
798 * @param fromIndex index to search from
799 *
800 * @returns index of val, -1 otherwise
801 */
802<%= access_public %> indexOf<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex: int): int {
803    fromIndex = normalizeIndex(fromIndex, <%= this_len_int %>)
804    for (let i = fromIndex; i < <%= this_len_int %>; i++) {
805        if (__runtimeEquals(val, <%= get_unsafe.(this, 'i') %>)) {
806            return i
807        }
808    }
809    return -1
810}
811
812/**
813 * Returns the first index at which a given element
814 * can be found in the array, or -1 if it is not present.
815 *
816 * @param val value to search
817 *
818 * @param fromIndex index to search from
819 *
820 * @returns index of val, -1 otherwise
821 */
822<%= access_public %> <%= override %> indexOf<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex?: Number): number {
823    return <%= this_call.('indexOf') %>val, asIntOrDefault(fromIndex, 0))
824}
825
826/**
827 * Copying version of the sort() method.
828 * It returns a new array with the elements sorted in ascending order.
829 *
830 * @returns sorted copy of hte current instance using default comparator
831 */
832<%= access_public %> toSorted<%= this_generic %>(<%= this_arg %>): <%= this_type %> {
833    let arr = <%= clone_this %>;
834    <%= arr_method_call.('arr', 'sort') %>)
835    return arr
836}
837
838/**
839 * Copying version of the sort() method.
840 * It returns a new array with the elements sorted in ascending order.
841 *
842 * @param comparator function to compare to elements of the Array
843 *
844 * @returns sorted copy of the current instance comparator
845 */
846<%= access_public %> toSorted<%= this_generic %>(<%= this_arg %>comparator: (a: <%= el_type %>, b: <%= el_type %>) => number): <%= this_type %> {
847    let arr = <%= clone_this %>;
848    <%= arr_method_call.('arr', 'sort') %>comparator)
849    return arr
850}
851
852/**
853 * Modifies `this` instance of `Array` class and populates
854 * it with same elements ordered towards the direction opposite to that previously stated.
855 *
856 * @note Mutating method
857 */
858<%= access_public %> reverse<%= this_generic %>(<%= this_arg %>): <%= this_return_type %> {
859    for (let i = 0; i < <%= this_len_int %> / 2; i++) {
860        const tmp = <%= get_unsafe.(this, 'i') %>;
861        const idx_r = <%= this_len_int %> - 1 - i;
862        const val_r = <%= get_unsafe.(this, 'idx_r') %>;
863        <%= set_unsafe.(this, 'i', 'val_r') %>;
864        <%= set_unsafe.(this, 'idx_r', 'tmp') %>;
865    }
866    return <%= this %>;
867}
868
869/**
870 * Copying version of the reverse() method.
871 * It returns a new array with the elements in reversed order.
872 *
873 * @returns reversed copy of the current Array
874 */
875<%= access_public %> toReversed<%= this_generic %>(<%= this_arg %>): <%= this_type %> {
876    let arr = <%= make_buffer.(this_len_int) %>
877    for (let i = 0; i < <%= this_len_int %>; i++) {
878        arr[<%= this_len_int %> - 1 - i] = <%= get_unsafe.(this, 'i') %>
879    }
880    return <%= from_buffer.('arr') %>
881}
882
883/**
884 * Copying version of using the bracket notation to change the value of a given index.
885 * It returns a new Array with the element at the given index replaced with the given value.
886 *
887 * @param index to replace
888 *
889 * @param value new value
890 *
891 * @returns a new Array with the element at the given index replaced with the given value
892 */
893<%= access_public %> with<%= this_generic %>(<%= this_arg %>index: number, value: <%= el_type %>): <%= this_type %> {
894    return <%= this_call.('with') %>index as int, value)
895}
896
897/**
898 * Copying version of using the bracket notation to change the value of a given index.
899 * It returns a new Array with the element at the given index replaced with the given value.
900 *
901 * @param index to replace
902 *
903 * @param value new value
904 *
905 * @returns a new Array with the element at the given index replaced with the given value
906 */
907<%= access_public %> with<%= this_generic %>(<%= this_arg %>index: int, value: <%= el_type %>): <%= this_type %> {
908    if (index < 0) {
909        index += <%= this_len_int %>;
910    }
911    if (index >= <%= this_len_int %>) {
912        throw new RangeError("Invalid index")
913    }
914    let arr = <%= clone_this %>;
915    <%= set_unsafe.('arr', 'index', 'value') %>;
916    return arr
917}
918
919/**
920 * Returns an iterator over all values
921 */
922<%= access_public %> <%= override %> values<%= this_generic %>(<%= this_arg %>): IterableIterator<<%= el_type %>> {
923    return new ArrayValuesIterator_<%= el_type %><%= this_iterator_generic || this_generic %>(<%= this %>);
924}
925
926/**
927 * Returns an iterable of key, value pairs for every entry in the array
928 */
929<%= access_public %> <%= override %> entries<%= this_generic %>(<%= this_arg %>): IterableIterator<[number, <%= el_type %>]> {
930    return new ArrayEntriesIterator_<%= el_type %><%= this_iterator_generic || this_generic %>(<%= this %>);
931}
932
933/**
934 * Determines whether all the members of an array satisfy the specified test.
935 *
936 * @param predicate A function that accepts up to three arguments. The every method calls
937 * the predicate function for each element in the array until the predicate returns a value
938 * which is coercible to the Boolean value false, or until the end of the array.
939 *
940 * @returns `true` if `predicate` returns a `true` value for every array element. Otherwise, `false`.
941 */
942<%= access_public %> <%= override %> every<%= this_generic %>(<%= this_arg %>predicate: () => boolean): boolean {
943    for (let i = 0; i < <%= this_len_int %>; i++) {
944        if (!predicate()) {
945            return false
946        }
947    }
948    return true
949}
950
951/**
952 * Returns the elements of an array that meet the condition specified in a callback function.
953 *
954 * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
955 *
956 * @returns New `Array` instance constructed from `this` with elements filtered using test function `predicate`.
957 */
958<%= access_public %> <%= override %> filter<%= this_generic %>(<%= this_arg %>predicate: () => boolean): <%= this_type %> {
959    return <%= this_call.('filter') %>(value: <%= el_type %>, index: number): boolean => predicate());
960}
961
962/**
963 * Returns the value of the first element in the array where predicate is true, and undefined
964 * otherwise.
965 *
966 * @param predicate find calls predicate once for each element of the array, in ascending
967 * order, until it finds one where predicate returns true. If such an element is found, find
968 * immediately returns that element value. Otherwise, find returns undefined.
969 *
970 * @returns the value of the first element in the array or undefined
971 */
972<%= access_public %> <%= override %> find<%= this_generic %>(<%= this_arg %>predicate: () => boolean): <%= el_type_boxed %> | undefined {
973    const res = <%= this_call.('findIndex') %>predicate)
974    if (res == -1) {
975        return undefined
976    }
977    return <%= get_unsafe.(this, 'res as int') %>;
978}
979
980/**
981 * Returns the index of the first element in the array where predicate is true, and -1
982 * otherwise.
983 *
984 * @param predicate find calls predicate once for each element of the array, in ascending
985 * order, until it finds one where predicate returns true. If such an element is found,
986 * findIndex immediately returns that element index. Otherwise, findIndex returns -1.
987 *
988 * @returns found element index or -1 otherwise
989 */
990<%= access_public %> <%= override %> findIndex<%= this_generic %>(<%= this_arg %>predicate: () => boolean): number {
991    for (let i = 0; i < <%= this_len_int %>; i++) {
992        if (predicate()) {
993            return i;
994        }
995    }
996    return -1;
997}
998
999/**
1000 * Performs the specified action for each element in an array.
1001 *
1002 * @param callbackfn  A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
1003 */
1004<%= access_public %> <%= override %> forEach<%= this_generic %>(<%= this_arg %>callbackfn: () => void): void {
1005    const len0 = <%= this_len_int %>;
1006    for (let i = 0; i < len0; i++) {
1007        callbackfn()
1008    }
1009}
1010
1011/**
1012 * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
1013 *
1014 * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
1015 *
1016 * @returns a result after applying callbackfn over all elements of the Array
1017 */
1018<%= access_public %> <%= override %> reduce<%= this_generic %>(<%= this_arg %>callbackfn: (previousValue: <%= el_type %>) => <%= el_type %>): <%= el_type %> {
1019    if (<%= this_len_int %> == 0) {
1020        throw new TypeError("Reduce of empty array with no initial value")
1021    }
1022    let acc: <%= el_type %> = <%= get_unsafe.(this, '0') %>;
1023    for (let i = 1; i < <%= this_len_int %>; i++) {
1024        acc = callbackfn(acc)
1025    }
1026    return acc
1027}
1028
1029/**
1030 * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
1031 *
1032 * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
1033 *
1034 * @returns a result after applying callbackfn over all elements of the Array
1035 */
1036<%= access_public %> <%= override %> reduce<%= this_generic %>(<%= this_arg %>callbackfn: () => <%= el_type %>): <%= el_type %> {
1037    if (<%= this_len_int %> == 0) {
1038        throw new TypeError("Reduce of empty array with no initial value")
1039    }
1040    let acc: <%= el_type %> = <%= get_unsafe.(this, '0') %>;
1041    for (let i = 1; i < <%= this_len_int %>; i++) {
1042        acc = callbackfn()
1043    }
1044    return acc
1045}
1046
1047/**
1048 * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
1049 *
1050 * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
1051 *
1052 * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
1053 *
1054 * @returns a result after applying callbackfn over all elements of the Array
1055 */
1056<%= access_public %> <%= override %> reduce<<%= this_generic_one %>U = <%= el_type %>>(<%= this_arg %>callbackfn: (previousValue: U) => U, initialValue: U): U {
1057    let acc = initialValue
1058    for (let i = 0; i < <%= this_len_int %>; i++) {
1059        acc = callbackfn(acc)
1060    }
1061    return acc
1062}
1063
1064/**
1065 * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
1066 *
1067 * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
1068 *
1069 * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
1070 *
1071 * @returns a result after applying callbackfn over all elements of the Array
1072 */
1073<%= access_public %> <%= override %> reduce<<%= this_generic_one %>U = <%= el_type %>>(<%= this_arg %>callbackfn: () => U, initialValue: U): U {
1074    let acc = initialValue
1075    for (let i = 0; i < <%= this_len_int %>; i++) {
1076        acc = callbackfn()
1077    }
1078    return acc
1079}
1080
1081/**
1082 * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
1083 *
1084 * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
1085 *
1086 * @returns a result after applying callbackfn over all elements of the Array
1087 */
1088<%= access_public %> <%= override %> reduceRight<%= this_generic %>(<%= this_arg %>callbackfn: (previousValue: <%= el_type %>) => <%= el_type %>): <%= el_type %> {
1089    if (<%= this_len_int %> == 0) {
1090        throw new TypeError("Reduce of empty array with no initial value")
1091    }
1092    let acc: <%= el_type %> = <%= get_unsafe.(this, "#{this_len_int} - 1") %>;
1093    for (let i = <%= this_len_int %> - 2; i >= 0; i--) {
1094        acc = callbackfn(acc)
1095    }
1096    return acc
1097}
1098
1099/**
1100 * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
1101 *
1102 * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
1103 *
1104 * @returns a result after applying callbackfn over all elements of the Array
1105 */
1106<%= access_public %> <%= override %> reduceRight<%= this_generic %>(<%= this_arg %>callbackfn: () => <%= el_type %>): <%= el_type %> {
1107    if (<%= this_len_int %> == 0) {
1108        throw new TypeError("Reduce of empty array with no initial value")
1109    }
1110    let acc: <%= el_type %> = <%= get_unsafe.(this, "#{this_len_int} - 1") %>;
1111    for (let i = <%= this_len_int %> - 2; i >= 0; i--) {
1112        acc = callbackfn()
1113    }
1114    return acc
1115}
1116
1117/**
1118 * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
1119 *
1120 * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
1121 *
1122 * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
1123 *
1124 * @returns a result after applying callbackfn over all elements of the Array
1125 */
1126<%= access_public %> <%= override %> reduceRight<<%= this_generic_one %>U>(<%= this_arg %>callbackfn: (previousValue: U) => U, initialValue: U): U {
1127    let acc = initialValue
1128    for (let i = <%= this_len_int %> - 1; i >= 0; i--) {
1129        acc = callbackfn(acc)
1130    }
1131    return acc
1132}
1133
1134/**
1135 * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
1136 *
1137 * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
1138 *
1139 * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
1140 *
1141 * @returns a result after applying callbackfn over all elements of the Array
1142 */
1143<%= access_public %> <%= override %> reduceRight<<%= this_generic_one %>U>(<%= this_arg %>callbackfn: () => U, initialValue: U): U {
1144    let acc = initialValue
1145    for (let i = <%= this_len_int %> - 1; i >= 0; i--) {
1146        acc = callbackfn()
1147    }
1148    return acc
1149}
1150
1151/**
1152 * Determines whether the specified callback function returns true for any element of an array.
1153 *
1154 * @param predicate A function that accepts up to three arguments. The some method calls
1155 * the predicate function for each element in the array until the predicate returns a value
1156 * which is coercible to the Boolean value true, or until the end of the array.
1157 *
1158 * @returns `true` if `predicate` returns a `true` value for at least one array element. Otherwise, `false`.
1159 */
1160<%= access_public %> <%= override %> some<%= this_generic %>(<%= this_arg %>predicate: () => boolean): boolean {
1161    for (let i = 0; i < <%= this_len_int %>; i++) {
1162        if (predicate()) {
1163            return true
1164        }
1165    }
1166    return false
1167}
1168
1169/**
1170 * Iterates the array in reverse order and returns the value of the first element
1171 * that satisfies the provided testing function
1172 *
1173 * @param predicate testing function
1174 *
1175 * @returns found element or undefined otherwise
1176 */
1177<%= access_public %> <%= override %> findLast<%= this_generic %>(<%= this_arg %>predicate: () => boolean): <%= el_type_boxed %> | undefined {
1178    for (let i = <%= this_len_int %> - 1; i >= 0; i--) {
1179        const val = <%= get_unsafe.(this, 'i') %>;
1180        if (predicate()) {
1181            return val;
1182        }
1183    }
1184    return undefined;
1185}
1186
1187/**
1188 * Iterates the array in reverse order and returns the index of
1189 * the first element that satisfies the provided testing function.
1190 * If no elements satisfy the testing function, -1 is returned.
1191 *
1192 * @param predicate testing function
1193 *
1194 * @returns index of first element satisfying to predicate, -1 if no such element
1195 */
1196<%= access_public %> <%= override %> findLastIndex<%= this_generic %>(<%= this_arg %>predicate: () => boolean): number {
1197    for (let i = <%= this_len_int %> - 1; i >= 0; i--) {
1198        if (predicate()) {
1199            return i
1200        }
1201    }
1202    return -1
1203}
1204