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 if (searchElement instanceof String) { 552 return this.lastIndexOfString(searchElement, k) 553 } else if (searchElement instanceof Number) { 554 return this.lastIndexOfNumber(searchElement, k) 555 } else if (searchElement instanceof Float) { 556 return this.lastIndexOfFloat(searchElement, k) 557 } else if (searchElement instanceof Long) { 558 return this.lastIndexOfLong(searchElement, k) 559 } else if (searchElement instanceof Int) { 560 return this.lastIndexOfInt(searchElement, k) 561 } else if (__runtimeIsSameReference(searchElement, undefined)) { 562 return this.lastIndexOfUndefined(k) 563 } else if (searchElement == null) { 564 return this.lastIndexOfNull(k) 565 } else { 566 return this.lastIndexOfCommon(searchElement, k) 567 } 568} 569 570/** 571 * Returns the last index at which a given element can be found in the array, 572 * or -1 if it is not present. The array is searched backwards, starting at fromIndex. 573 * 574 * @param searchElement element to locate in the array. 575 * 576 * @param fromIndex zero-based index at which to start searching backwards. 577 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used. 578 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned. 579 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched. 580 * If `fromIndex` is undefined then `fromIndex = 0`. 581 * If `fromIndex` is ommited then `fromIndex = length()-1`. 582 * 583 * @returns The last index of the element in the array; -1 if not found. 584 */ 585<%= access_public %> lastIndexOf<%= this_generic %>(<%= this_arg %>searchElement: <%= el_type %>, fromIndex: number|undefined): number { 586 return <%= this_call.('lastIndexOf') %>searchElement, asIntOrDefault(fromIndex, 0)); 587} 588 589/** 590 * Returns the last index at which a given element can be found in the array, 591 * or -1 if it is not present. The array is searched backwards, starting at fromIndex. 592 * 593 * @param searchElement element to locate in the array. 594 * 595 * @param fromIndex zero-based index at which to start searching backwards. 596 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used. 597 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned. 598 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched. 599 * If `fromIndex` is undefined then `fromIndex = 0`. 600 * If `fromIndex` is ommited then `fromIndex = length()-1`. 601 * 602 * @returns The last index of the element in the array; -1 if not found. 603 */ 604<%= access_public %> lastIndexOf<%= this_generic %>(<%= this_arg %>searchElement: <%= el_type %>): number { 605 return <%= this_call.('lastIndexOf') %>searchElement, <%= this_len_int %> - 1); 606} 607<% end %> 608 609/** 610 * Creates and returns a new string by concatenating all of the elements in an `Array`, 611 * separated by a specified separator string. 612 * If the array has only one item, then that item will be returned without using the separator. 613 * 614 * @param sep specifies a separator 615 * 616 * @returns A string with all array elements joined. If `length()` is 0, the empty string is returned. 617 */ 618<%= access_public %> <%= override %> join<%= this_generic %>(<%= this_arg %>sep?: String): string { 619 if (<%= this_len_int %> == 0) { 620 return "" 621 } 622 const sepReal = __runtimeIsSameReference(sep, undefined) ? "," : sep! 623 <% if el_type == "T" %>// NOTE(aleksander-sotov) We can't call String constructor here due to internal issue 18583 624 const first_el = <%= get_unsafe.(this, '0') %> 625 let first_str = "undefined" 626 if (!__runtimeIsSameReference(first_el, undefined)) { 627 first_str = new String(first_el) 628 } 629 let sb = new StringBuilder(first_str)<% else %>let sb = new StringBuilder(new String(<%= get_unsafe.(this, '0') %>))<% end %> 630 for (let i: int = 1; i < <%= this_len_int %>; i++) { 631 const tmp = <%= get_unsafe.(this, 'i') %> 632 sb.append(sepReal); 633 <% if el_type == "T" %>// NOTE(aleksander-sotov) We can't call String constructor here due to internal issue 18583 634 if (!__runtimeIsSameReference(tmp, undefined)) { 635 sb.append(new String(tmp)) 636 } else { 637 sb.append("undefined") 638 }<% else %>sb.append(tmp)<% end %> 639 } 640 641 return sb.toString(); 642} 643 644/** 645 * Returns a string representing the specified array and its elements. 646 * 647 * @returns string representation 648 */ 649<%= access_public %> <%= override %> toString<%= this_generic %>(<%= this_arg %>): string { 650 return <%= this_call.('join') %>","); 651} 652 653/** 654 * Returns a locale string representing the specified array and its elements. 655 * 656 * @param locales 657 * 658 * @param options 659 * 660 * @returns string representation 661 */ 662<%= access_public %> toLocaleString<%= this_generic %>(<%= this_arg %>locales: Object, options: Object): string { 663 throw new Error("Array.toLocaleString: not implemented") 664} 665 666/** 667 * Returns a locale string representing the specified array and its elements. 668 * 669 * @param options 670 * 671 * @returns string representation 672 */ 673<%= access_public %> toLocaleString<%= this_generic %>(<%= this_arg %>locales: Object): string { 674 return <%= this_call.('toLocaleString') %>new Object(), new Object()) 675} 676 677/** 678 * Returns a locale string representing the specified array and its elements. 679 * 680 * @returns string representation 681 */ 682<%= access_public %> <%= override %> toLocaleString<%= this_generic %>(<%= this_arg %>): string { 683 const sb = new StringBuilder() 684 const len = <%= this_len_int %>; 685 for (let i = 0; i < len; i++) { 686 if (i != 0) { 687 sb.append(",") 688 } 689 let x = <%= get_unsafe.(this, 'i') %> as NullishType; 690 if (!__runtimeIsSameReference(null, x) && !__runtimeIsSameReference(undefined, x)) { 691 sb.append(x!.toLocaleString()) 692 } 693 } 694 return sb.toString() 695} 696 697/** 698 * Copying version of the splice() method. 699 * 700 * @param start index 701 * 702 * @param delete number of items after start index 703 * 704 * @returns a new Array with some elements removed and/or replaced at a given index. 705 */ 706<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start?: Number, delete?: Number): <%= this_type %> { 707 const len = <%= this_len_int %>; 708 return <%= this_call.('toSpliced') %>asIntOrDefault(start, len), asIntOrDefault(delete, len)) 709} 710 711/** 712 * Copying version of the splice() method. 713 * 714 * @param start index 715 * 716 * @param delete number of items after start index 717 * 718 * @returns a new Array with some elements removed and/or replaced at a given index. 719 */ 720<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start: number, delete: number, ...items: <%= el_type %>[]): <%= this_type %> { 721 const len = <%= this_len_int %>; 722 return <%= this_call.('toSpliced') %>start as int, delete as int, ...items) 723} 724 725/** 726 * Copying version of the splice() method. 727 * 728 * @param start index 729 * 730 * @param delete number of items after start index 731 * 732 * @returns a new Array with some elements removed and/or replaced at a given index. 733 */ 734<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start: int, delete: int, ...items: <%= el_type %>[]): <%= this_type %> { 735 const len = <%= this_len_int %>; 736 start = normalizeIndex(start, len); 737 if (delete < 0) { 738 delete = 0; 739 } else if (delete > len) { 740 delete = len; 741 } 742 if (start > len - delete) { 743 delete = len - start 744 } 745 const res = <%= make_buffer.('len - delete + items.length') %>; 746 for (let i = 0; i < start; i++) { 747 res[i] = <%= get_unsafe.(this, 'i') %> 748 } 749 for (let i = 0; i < items.length; i++) { 750 res[start + i] = items[i] 751 } 752 for (let i = start + delete; i < len; i++) { 753 res[i - delete + items.length] = <%= get_unsafe.(this, 'i') %> 754 } 755 return <%= from_buffer.('res') %>; 756} 757 758/** 759 * Copying version of the splice() method. 760 * 761 * @param start index 762 * 763 * @returns a new Array with some elements removed and/or replaced at a given index. 764 */ 765<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start: int): <%= this_type %> { 766 return <%= this_call.('toSpliced') %>start, <%= this_len_int %>) 767} 768 769/** 770 * Checks whether an Array includes a certain value among its entries, 771 * returning true or false as appropriate. 772 * 773 * @param val value to search 774 * 775 * @param fromIndex start index 776 * 777 * @returns true if val is in Array 778 */ 779<%= access_public %> <%= override %> includes<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex?: Number): boolean { 780 const len = <%= this_len_int %>; 781 const fi = normalizeIndex(asIntOrDefault(fromIndex, 0), len); 782% if ['float', 'double', 'number'].include?(el_type) 783 if (isNaN(val)) { 784 for (let i = fi; i < len; i++) { 785 if (isNaN(<%= get_unsafe.(this, 'i') %>)) { 786 return true; 787 } 788 } 789 return false; 790 } 791% end 792% if ['boolean', 'byte', 'short', 'int', 'long', 'char', 'float', 'double', 'number'].include?(el_type) 793 for (let i = fi; i < len; i++) { 794 if (val == <%= get_unsafe.(this, 'i') %>) { 795 return true; 796 } 797 } 798 return false; 799% elsif el_type == 'T' 800 if (val instanceof String) { 801 return this.searchString(val, fi, len) 802 } else if (val instanceof Number) { 803 return this.searchNumber(val, fi, len) 804 } else if (val instanceof Float) { 805 return this.searchFloat(val, fi, len) 806 } else if (val instanceof Long) { 807 return this.searchLong(val, fi, len) 808 } else if (val instanceof Int) { 809 return this.searchInt(val, fi, len) 810 } else if (__runtimeIsSameReference(val, undefined)) { 811 return this.searchUndefined(fi, len) 812 } else if (val == null) { 813 return this.searchNull(fi, len) 814 } else { 815 return this.searchCommon(val, fi, len) 816 } 817% else 818 for (let i = fi; i < len; i++) { 819 if (__runtimeSameValueZero(val, <%= get_unsafe.(this, 'i') %>)) { 820 return true; 821 } 822 } 823 return false; 824% end 825} 826 827/** 828 * Returns the first index at which a given element 829 * can be found in the array, or -1 if it is not present. 830 * 831 * @param val value to search 832 * 833 * @param fromIndex index to search from 834 * 835 * @returns index of val, -1 otherwise 836 */ 837<%= access_public %> indexOf<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex: int): int { 838 fromIndex = normalizeIndex(fromIndex, <%= this_len_int %>) 839% if el_type != 'T' 840 for (let i = fromIndex; i < <%= this_len_int %>; i++) { 841 if (__runtimeEquals(val, <%= get_unsafe.(this, 'i') %>)) { 842 return i 843 } 844 } 845 return -1 846% else 847 const len = <%= this_len_int %>; 848 if (val instanceof String) { 849 return this.indexOfString(val, fromIndex, len) 850 } else if (val instanceof Number) { 851 return this.indexOfNumber(val, fromIndex, len) 852 } else if (val instanceof Float) { 853 return this.indexOfFloat(val, fromIndex, len) 854 } else if (val instanceof Long) { 855 return this.indexOfLong(val, fromIndex, len) 856 } else if (val instanceof Int) { 857 return this.indexOfInt(val, fromIndex, len) 858 } else if (__runtimeIsSameReference(val, undefined)) { 859 return this.indexOfUndefined(fromIndex, len) 860 } else if (val == null) { 861 return this.indexOfNull(fromIndex, len) 862 } else { 863 return this.indexOfCommon(val, fromIndex, len) 864 } 865% end 866} 867 868/** 869 * Returns the first index at which a given element 870 * can be found in the array, or -1 if it is not present. 871 * 872 * @param val value to search 873 * 874 * @param fromIndex index to search from 875 * 876 * @returns index of val, -1 otherwise 877 */ 878<%= access_public %> <%= override %> indexOf<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex?: Number): number { 879 return <%= this_call.('indexOf') %>val, asIntOrDefault(fromIndex, 0)) 880} 881 882/** 883 * Copying version of the sort() method. 884 * It returns a new array with the elements sorted in ascending order. 885 * 886 * @returns sorted copy of hte current instance using default comparator 887 */ 888<%= access_public %> toSorted<%= this_generic %>(<%= this_arg %>): <%= this_type %> { 889 let arr = <%= clone_this %>; 890 <%= arr_method_call.('arr', 'sort') %>) 891 return arr 892} 893 894/** 895 * Copying version of the sort() method. 896 * It returns a new array with the elements sorted in ascending order. 897 * 898 * @param comparator function to compare to elements of the Array 899 * 900 * @returns sorted copy of the current instance comparator 901 */ 902<%= access_public %> toSorted<%= this_generic %>(<%= this_arg %>comparator: (a: <%= el_type %>, b: <%= el_type %>) => number): <%= this_type %> { 903 let arr = <%= clone_this %>; 904 <%= arr_method_call.('arr', 'sort') %>comparator) 905 return arr 906} 907 908/** 909 * Modifies `this` instance of `Array` class and populates 910 * it with same elements ordered towards the direction opposite to that previously stated. 911 * 912 * @note Mutating method 913 */ 914<%= access_public %> reverse<%= this_generic %>(<%= this_arg %>): <%= this_return_type %> { 915 for (let i = 0; i < <%= this_len_int %> / 2; i++) { 916 const tmp = <%= get_unsafe.(this, 'i') %>; 917 const idx_r = <%= this_len_int %> - 1 - i; 918 const val_r = <%= get_unsafe.(this, 'idx_r') %>; 919 <%= set_unsafe.(this, 'i', 'val_r') %>; 920 <%= set_unsafe.(this, 'idx_r', 'tmp') %>; 921 } 922 return <%= this %>; 923} 924 925/** 926 * Copying version of the reverse() method. 927 * It returns a new array with the elements in reversed order. 928 * 929 * @returns reversed copy of the current Array 930 */ 931<%= access_public %> toReversed<%= this_generic %>(<%= this_arg %>): <%= this_type %> { 932 let arr = <%= make_buffer.(this_len_int) %> 933 for (let i = 0; i < <%= this_len_int %>; i++) { 934 arr[<%= this_len_int %> - 1 - i] = <%= get_unsafe.(this, 'i') %> 935 } 936 return <%= from_buffer.('arr') %> 937} 938 939/** 940 * Copying version of using the bracket notation to change the value of a given index. 941 * It returns a new Array with the element at the given index replaced with the given value. 942 * 943 * @param index to replace 944 * 945 * @param value new value 946 * 947 * @returns a new Array with the element at the given index replaced with the given value 948 */ 949<%= access_public %> with<%= this_generic %>(<%= this_arg %>index: number, value: <%= el_type %>): <%= this_type %> { 950 return <%= this_call.('with') %>index as int, value) 951} 952 953/** 954 * Copying version of using the bracket notation to change the value of a given index. 955 * It returns a new Array with the element at the given index replaced with the given value. 956 * 957 * @param index to replace 958 * 959 * @param value new value 960 * 961 * @returns a new Array with the element at the given index replaced with the given value 962 */ 963<%= access_public %> with<%= this_generic %>(<%= this_arg %>index: int, value: <%= el_type %>): <%= this_type %> { 964 if (index < 0) { 965 index += <%= this_len_int %>; 966 } 967 if (index >= <%= this_len_int %>) { 968 throw new RangeError("Invalid index") 969 } 970 let arr = <%= clone_this %>; 971 <%= set_unsafe.('arr', 'index', 'value') %>; 972 return arr 973} 974 975/** 976 * Returns an iterator over all values 977 */ 978<%= access_public %> <%= override %> values<%= this_generic %>(<%= this_arg %>): IterableIterator<<%= el_type %>> { 979 return new ArrayValuesIterator_<%= el_type %><%= this_iterator_generic || this_generic %>(<%= this %>); 980} 981 982/** 983 * Returns an iterable of key, value pairs for every entry in the array 984 */ 985<%= access_public %> <%= override %> entries<%= this_generic %>(<%= this_arg %>): IterableIterator<[number, <%= el_type %>]> { 986 return new ArrayEntriesIterator_<%= el_type %><%= this_iterator_generic || this_generic %>(<%= this %>); 987} 988 989/** 990 * Determines whether all the members of an array satisfy the specified test. 991 * 992 * @param predicate A function that accepts up to three arguments. The every method calls 993 * the predicate function for each element in the array until the predicate returns a value 994 * which is coercible to the Boolean value false, or until the end of the array. 995 * 996 * @returns `true` if `predicate` returns a `true` value for every array element. Otherwise, `false`. 997 */ 998<%= access_public %> <%= override %> every<%= this_generic %>(<%= this_arg %>predicate: () => boolean): boolean { 999 for (let i = 0; i < <%= this_len_int %>; i++) { 1000 if (!predicate()) { 1001 return false 1002 } 1003 } 1004 return true 1005} 1006 1007/** 1008 * Returns the elements of an array that meet the condition specified in a callback function. 1009 * 1010 * @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. 1011 * 1012 * @returns New `Array` instance constructed from `this` with elements filtered using test function `predicate`. 1013 */ 1014<%= access_public %> <%= override %> filter<%= this_generic %>(<%= this_arg %>predicate: () => boolean): <%= this_type %> { 1015 return <%= this_call.('filter') %>(value: <%= el_type %>, index: number): boolean => predicate()); 1016} 1017 1018/** 1019 * Returns the value of the first element in the array where predicate is true, and undefined 1020 * otherwise. 1021 * 1022 * @param predicate find calls predicate once for each element of the array, in ascending 1023 * order, until it finds one where predicate returns true. If such an element is found, find 1024 * immediately returns that element value. Otherwise, find returns undefined. 1025 * 1026 * @returns the value of the first element in the array or undefined 1027 */ 1028<%= access_public %> <%= override %> find<%= this_generic %>(<%= this_arg %>predicate: () => boolean): <%= el_type_boxed %> | undefined { 1029 const res = <%= this_call.('findIndex') %>predicate) 1030 if (res == -1) { 1031 return undefined 1032 } 1033 return <%= get_unsafe.(this, 'res as int') %>; 1034} 1035 1036/** 1037 * Returns the index of the first element in the array where predicate is true, and -1 1038 * otherwise. 1039 * 1040 * @param predicate find calls predicate once for each element of the array, in ascending 1041 * order, until it finds one where predicate returns true. If such an element is found, 1042 * findIndex immediately returns that element index. Otherwise, findIndex returns -1. 1043 * 1044 * @returns found element index or -1 otherwise 1045 */ 1046<%= access_public %> <%= override %> findIndex<%= this_generic %>(<%= this_arg %>predicate: () => boolean): number { 1047 for (let i = 0; i < <%= this_len_int %>; i++) { 1048 if (predicate()) { 1049 return i; 1050 } 1051 } 1052 return -1; 1053} 1054 1055/** 1056 * Performs the specified action for each element in an array. 1057 * 1058 * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array. 1059 */ 1060<%= access_public %> <%= override %> forEach<%= this_generic %>(<%= this_arg %>callbackfn: () => void): void { 1061 const len0 = <%= this_len_int %>; 1062 for (let i = 0; i < len0; i++) { 1063 callbackfn() 1064 } 1065} 1066 1067/** 1068 * 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. 1069 * 1070 * @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. 1071 * 1072 * @returns a result after applying callbackfn over all elements of the Array 1073 */ 1074<%= access_public %> <%= override %> reduce<%= this_generic %>(<%= this_arg %>callbackfn: (previousValue: <%= el_type %>) => <%= el_type %>): <%= el_type %> { 1075 if (<%= this_len_int %> == 0) { 1076 throw new TypeError("Reduce of empty array with no initial value") 1077 } 1078 let acc: <%= el_type %> = <%= get_unsafe.(this, '0') %>; 1079 for (let i = 1; i < <%= this_len_int %>; i++) { 1080 acc = callbackfn(acc) 1081 } 1082 return acc 1083} 1084 1085/** 1086 * 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. 1087 * 1088 * @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. 1089 * 1090 * @returns a result after applying callbackfn over all elements of the Array 1091 */ 1092<%= access_public %> <%= override %> reduce<%= this_generic %>(<%= this_arg %>callbackfn: () => <%= el_type %>): <%= el_type %> { 1093 if (<%= this_len_int %> == 0) { 1094 throw new TypeError("Reduce of empty array with no initial value") 1095 } 1096 let acc: <%= el_type %> = <%= get_unsafe.(this, '0') %>; 1097 for (let i = 1; i < <%= this_len_int %>; i++) { 1098 acc = callbackfn() 1099 } 1100 return acc 1101} 1102 1103/** 1104 * 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. 1105 * 1106 * @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. 1107 * 1108 * @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. 1109 * 1110 * @returns a result after applying callbackfn over all elements of the Array 1111 */ 1112<%= access_public %> <%= override %> reduce<<%= this_generic_one %>U = <%= el_type %>>(<%= this_arg %>callbackfn: (previousValue: U) => U, initialValue: U): U { 1113 let acc = initialValue 1114 for (let i = 0; i < <%= this_len_int %>; i++) { 1115 acc = callbackfn(acc) 1116 } 1117 return acc 1118} 1119 1120/** 1121 * 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. 1122 * 1123 * @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. 1124 * 1125 * @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. 1126 * 1127 * @returns a result after applying callbackfn over all elements of the Array 1128 */ 1129<%= access_public %> <%= override %> reduce<<%= this_generic_one %>U = <%= el_type %>>(<%= this_arg %>callbackfn: () => U, initialValue: U): U { 1130 let acc = initialValue 1131 for (let i = 0; i < <%= this_len_int %>; i++) { 1132 acc = callbackfn() 1133 } 1134 return acc 1135} 1136 1137/** 1138 * 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. 1139 * 1140 * @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. 1141 * 1142 * @returns a result after applying callbackfn over all elements of the Array 1143 */ 1144<%= access_public %> <%= override %> reduceRight<%= this_generic %>(<%= this_arg %>callbackfn: (previousValue: <%= el_type %>) => <%= el_type %>): <%= el_type %> { 1145 if (<%= this_len_int %> == 0) { 1146 throw new TypeError("Reduce of empty array with no initial value") 1147 } 1148 let acc: <%= el_type %> = <%= get_unsafe.(this, "#{this_len_int} - 1") %>; 1149 for (let i = <%= this_len_int %> - 2; i >= 0; i--) { 1150 acc = callbackfn(acc) 1151 } 1152 return acc 1153} 1154 1155/** 1156 * 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. 1157 * 1158 * @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. 1159 * 1160 * @returns a result after applying callbackfn over all elements of the Array 1161 */ 1162<%= access_public %> <%= override %> reduceRight<%= this_generic %>(<%= this_arg %>callbackfn: () => <%= el_type %>): <%= el_type %> { 1163 if (<%= this_len_int %> == 0) { 1164 throw new TypeError("Reduce of empty array with no initial value") 1165 } 1166 let acc: <%= el_type %> = <%= get_unsafe.(this, "#{this_len_int} - 1") %>; 1167 for (let i = <%= this_len_int %> - 2; i >= 0; i--) { 1168 acc = callbackfn() 1169 } 1170 return acc 1171} 1172 1173/** 1174 * 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. 1175 * 1176 * @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. 1177 * 1178 * @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. 1179 * 1180 * @returns a result after applying callbackfn over all elements of the Array 1181 */ 1182<%= access_public %> <%= override %> reduceRight<<%= this_generic_one %>U>(<%= this_arg %>callbackfn: (previousValue: U) => U, initialValue: U): U { 1183 let acc = initialValue 1184 for (let i = <%= this_len_int %> - 1; i >= 0; i--) { 1185 acc = callbackfn(acc) 1186 } 1187 return acc 1188} 1189 1190/** 1191 * 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. 1192 * 1193 * @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. 1194 * 1195 * @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. 1196 * 1197 * @returns a result after applying callbackfn over all elements of the Array 1198 */ 1199<%= access_public %> <%= override %> reduceRight<<%= this_generic_one %>U>(<%= this_arg %>callbackfn: () => U, initialValue: U): U { 1200 let acc = initialValue 1201 for (let i = <%= this_len_int %> - 1; i >= 0; i--) { 1202 acc = callbackfn() 1203 } 1204 return acc 1205} 1206 1207/** 1208 * Determines whether the specified callback function returns true for any element of an array. 1209 * 1210 * @param predicate A function that accepts up to three arguments. The some method calls 1211 * the predicate function for each element in the array until the predicate returns a value 1212 * which is coercible to the Boolean value true, or until the end of the array. 1213 * 1214 * @returns `true` if `predicate` returns a `true` value for at least one array element. Otherwise, `false`. 1215 */ 1216<%= access_public %> <%= override %> some<%= this_generic %>(<%= this_arg %>predicate: () => boolean): boolean { 1217 for (let i = 0; i < <%= this_len_int %>; i++) { 1218 if (predicate()) { 1219 return true 1220 } 1221 } 1222 return false 1223} 1224 1225/** 1226 * Iterates the array in reverse order and returns the value of the first element 1227 * that satisfies the provided testing function 1228 * 1229 * @param predicate testing function 1230 * 1231 * @returns found element or undefined otherwise 1232 */ 1233<%= access_public %> <%= override %> findLast<%= this_generic %>(<%= this_arg %>predicate: () => boolean): <%= el_type_boxed %> | undefined { 1234 for (let i = <%= this_len_int %> - 1; i >= 0; i--) { 1235 const val = <%= get_unsafe.(this, 'i') %>; 1236 if (predicate()) { 1237 return val; 1238 } 1239 } 1240 return undefined; 1241} 1242 1243/** 1244 * Iterates the array in reverse order and returns the index of 1245 * the first element that satisfies the provided testing function. 1246 * If no elements satisfy the testing function, -1 is returned. 1247 * 1248 * @param predicate testing function 1249 * 1250 * @returns index of first element satisfying to predicate, -1 if no such element 1251 */ 1252<%= access_public %> <%= override %> findLastIndex<%= this_generic %>(<%= this_arg %>predicate: () => boolean): number { 1253 for (let i = <%= this_len_int %> - 1; i >= 0; i--) { 1254 if (predicate()) { 1255 return i 1256 } 1257 } 1258 return -1 1259} 1260