1%# Copyright (c) 2025 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% 15% require_relative 'Array_code' 16 17% TemplateData::get_lambda_data.each { |lambda_args_params| 18% lambda_params_num, lambda_args, lambda_params = lambda_args_params 19 20/** 21 * Calls a defined callback function on each element of an array, and returns an array that contains the results. 22 * 23 * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. 24 * 25 * @returns `Array` instance, constructed from `this` and given function. 26 */ 27<%= access_public %> <%= override %> map<%= map_generic %>(<%= this_arg %>callbackfn: (value: <%= el_type %><%= lambda_params %>) => <%= mapped_type %>): <%= array_of_type.(mapped_type) %> { 28 const len = <%= this_len_int %>; 29 let res : <%= make_fixed_array %> = <%= make_buffer.('len', mapped_type) %>; 30 for (let i = 0; i < len; i++) { 31 res[i] = callbackfn(<%= get_unsafe.(this, 'i') %><%= lambda_args %>); 32 } 33 return <%= from_buffer.('res', mapped_type) %>; 34} 35 36/** 37 * 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. 38 * 39 * @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. 40 * 41 * @returns a result after applying callbackfn over all elements of the Array 42 */ 43<%= access_public %> <%= override %> reduceRight<%= this_generic %>(<%= this_arg %>callbackfn: (previousValue: <%= el_type %>, currentValue: <%= el_type %><%= lambda_params %>) => <%= el_type %>): <%= el_type %> { 44 if (<%= this_len_int %> == 0) { 45 throw new TypeError("Reduce of empty array with no initial value") 46 } 47 let acc: <%= el_type %> = <%= get_unsafe.(this, "#{this_len_int} - 1") %>; 48 for (let i = <%= this_len_int %> - 2; i >= 0; i--) { 49 acc = callbackfn(acc, <%= get_unsafe.(this, 'i') %><%= lambda_args %>) 50 } 51 return acc 52} 53% } 54