time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (2093B)


      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 {Collection} y - destination array/collection
     30 * @param {integer} strideY - `y` stride length
     31 * @param {Function} fcn - unary function to apply to callback return values
     32 * @param {Callback} clbk - callback
     33 * @param {*} [thisArg] - callback execution context
     34 * @returns {Collection} `y`
     35 *
     36 * @example
     37 * var abs = require( '@stdlib/math/base/special/abs' );
     38 *
     39 * function accessor( v ) {
     40 *     return v * 2.0;
     41 * }
     42 *
     43 * var x = [ 1.0, -2.0, 3.0, -4.0, 5.0 ];
     44 * var y = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
     45 *
     46 * mapBy( x.length, x, 1, y, 1, abs, accessor );
     47 *
     48 * console.log( y );
     49 * // => [ 2.0, 4.0, 6.0, 8.0, 10.0 ]
     50 */
     51 function mapBy( N, x, strideX, y, strideY, fcn, clbk, thisArg ) {
     52 	var ix;
     53 	var iy;
     54 	var v;
     55 	var i;
     56 	if ( N <= 0 ) {
     57 		return y;
     58 	}
     59 	if ( strideX < 0 ) {
     60 		ix = (1-N) * strideX;
     61 	} else {
     62 		ix = 0;
     63 	}
     64 	if ( strideY < 0 ) {
     65 		iy = (1-N) * strideY;
     66 	} else {
     67 		iy = 0;
     68 	}
     69 	for ( i = 0; i < N; i++ ) {
     70 		v = clbk.call( thisArg, x[ ix ], i, ix, iy, x, y );
     71 		if ( v !== void 0 ) {
     72 			y[ iy ] = fcn( v );
     73 		}
     74 		ix += strideX;
     75 		iy += strideY;
     76 	}
     77 	return y;
     78 }
     79 
     80 
     81 // EXPORTS //
     82 
     83 module.exports = mapBy;