14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ciconst array1 = [1, 2, 3, 4];
174514f5e3Sopenharmony_ciconst initialValue = 0;
184514f5e3Sopenharmony_ciconst sumWithInitial = array1.reduce(
194514f5e3Sopenharmony_ci  (accumulator, currentValue) => accumulator + currentValue,
204514f5e3Sopenharmony_ci  initialValue,
214514f5e3Sopenharmony_ci);
224514f5e3Sopenharmony_ciprint(sumWithInitial);
234514f5e3Sopenharmony_ciconst objects = [{ x: 1 }, { x: 2 }, { x: 3 }];
244514f5e3Sopenharmony_ciconst sum = objects.reduce(
254514f5e3Sopenharmony_ci  (accumulator, currentValue) => accumulator + currentValue.x,
264514f5e3Sopenharmony_ci  0,
274514f5e3Sopenharmony_ci);
284514f5e3Sopenharmony_ciprint(sum); // 6
294514f5e3Sopenharmony_ciprint([1, 2, , 4].reduce((a, b) => a + b)); // 7
304514f5e3Sopenharmony_ciprint([1, 2, undefined, 4].reduce((a, b) => a + b)); // NaN
314514f5e3Sopenharmony_ciconst arrayLike = {
324514f5e3Sopenharmony_ci  length: 3,
334514f5e3Sopenharmony_ci  0: 2,
344514f5e3Sopenharmony_ci  1: 3,
354514f5e3Sopenharmony_ci  2: 4,
364514f5e3Sopenharmony_ci};
374514f5e3Sopenharmony_ciprint(Array.prototype.reduce.call(arrayLike, (x, y) => x + y));
384514f5e3Sopenharmony_ciconst myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
394514f5e3Sopenharmony_ciconst myArrayWithNoDuplicates = myArray.reduce((accumulator, currentValue) => {
404514f5e3Sopenharmony_ci  if (!accumulator.includes(currentValue)) {
414514f5e3Sopenharmony_ci    return [...accumulator, currentValue];
424514f5e3Sopenharmony_ci  }
434514f5e3Sopenharmony_ci  return accumulator;
444514f5e3Sopenharmony_ci}, []);
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_ciprint(myArrayWithNoDuplicates);
474514f5e3Sopenharmony_ciconst numbers = [-5, 6, 2, 0];
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ciconst doubledPositiveNumbers = numbers.reduce((accumulator, currentValue) => {
504514f5e3Sopenharmony_ci  if (currentValue > 0) {
514514f5e3Sopenharmony_ci    const doubled = currentValue * 2;
524514f5e3Sopenharmony_ci    return [...accumulator, doubled];
534514f5e3Sopenharmony_ci  }
544514f5e3Sopenharmony_ci  return accumulator;
554514f5e3Sopenharmony_ci}, []);
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ciprint(doubledPositiveNumbers); // [12, 4]
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_cifunction runPromiseInSequence(arr, input) {
604514f5e3Sopenharmony_ci  return arr.reduce(
614514f5e3Sopenharmony_ci    (promiseChain, currentFunction) => promiseChain.then(currentFunction),
624514f5e3Sopenharmony_ci    Promise.resolve(input),
634514f5e3Sopenharmony_ci  );
644514f5e3Sopenharmony_ci}
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_cifunction p1(a) {
674514f5e3Sopenharmony_ci  return new Promise((resolve, reject) => {
684514f5e3Sopenharmony_ci    resolve(a * 5);
694514f5e3Sopenharmony_ci  });
704514f5e3Sopenharmony_ci}
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_cifunction p2(a) {
734514f5e3Sopenharmony_ci  return new Promise((resolve, reject) => {
744514f5e3Sopenharmony_ci    resolve(a * 2);
754514f5e3Sopenharmony_ci  });
764514f5e3Sopenharmony_ci}
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_cifunction f3(a) {
794514f5e3Sopenharmony_ci  return a * 3;
804514f5e3Sopenharmony_ci}
814514f5e3Sopenharmony_ci
824514f5e3Sopenharmony_cifunction p4(a) {
834514f5e3Sopenharmony_ci  return new Promise((resolve, reject) => {
844514f5e3Sopenharmony_ci    resolve(a * 4);
854514f5e3Sopenharmony_ci  });
864514f5e3Sopenharmony_ci}
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ciconst promiseArr = [p1, p2, f3, p4];
894514f5e3Sopenharmony_cirunPromiseInSequence(promiseArr, 10).then(console.log); // 1200
90