ndarray.js (2159B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MAIN // 22 23 /** 24 * Applies a unary function to each element retrieved from a strided input array according to a callback function and assigns each result to an element in a strided output array. 25 * 26 * @param {NonNegativeInteger} N - number of indexed elements 27 * @param {Collection} x - input array/collection 28 * @param {integer} strideX - `x` stride length 29 * @param {NonNegativeInteger} offsetX - starting `x` index 30 * @param {Collection} y - destination array/collection 31 * @param {integer} strideY - `y` stride length 32 * @param {NonNegativeInteger} offsetY - starting `y` index 33 * @param {Function} fcn - unary function to apply to callback return values 34 * @param {Callback} clbk - callback 35 * @param {*} [thisArg] - callback execution context 36 * @returns {Collection} `y` 37 * 38 * @example 39 * var abs = require( '@stdlib/math/base/special/abs' ); 40 * 41 * function accessor( v ) { 42 * return v * 2.0; 43 * } 44 * 45 * var x = [ 1.0, -2.0, 3.0, -4.0, 5.0 ]; 46 * var y = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; 47 * 48 * mapBy( x.length, x, 1, 0, y, 1, 0, abs, accessor ); 49 * 50 * console.log( y ); 51 * // => [ 2.0, 4.0, 6.0, 8.0, 10.0 ] 52 */ 53 function mapBy( N, x, strideX, offsetX, y, strideY, offsetY, fcn, clbk, thisArg ) { // eslint-disable-line max-len 54 var ix; 55 var iy; 56 var v; 57 var i; 58 59 if ( N <= 0 ) { 60 return y; 61 } 62 ix = offsetX; 63 iy = offsetY; 64 for ( i = 0; i < N; i++ ) { 65 v = clbk.call( thisArg, x[ ix ], i, ix, iy, x, y ); 66 if ( v !== void 0 ) { 67 y[ iy ] = fcn( v ); 68 } 69 ix += strideX; 70 iy += strideY; 71 } 72 return y; 73 } 74 75 76 // EXPORTS // 77 78 module.exports = mapBy;