ndarray.js (1520B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 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 * Fills a strided array according to a provided callback function. 25 * 26 * @param {PositiveInteger} N - number of indexed elements 27 * @param {Collection} x - input array/collection 28 * @param {integer} stride - index increment 29 * @param {NonNegativeInteger} offset - starting index 30 * @param {Callback} clbk - callback 31 * @param {*} [thisArg] - execution context 32 * @returns {Collection} input array/collection 33 * 34 * @example 35 * var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; 36 * 37 * function fill() { 38 * return 5.0; 39 * } 40 * 41 * gfillBy( 3, 5.0, x, 1, x.length-3 ); 42 * // x => [ 1.0, -2.0, 3.0, 5.0, 5.0, 5.0 ] 43 */ 44 function gfillBy( N, x, stride, offset, clbk, thisArg ) { 45 var ix; 46 var i; 47 48 if ( N <= 0 ) { 49 return x; 50 } 51 ix = offset; 52 for ( i = 0; i < N; i++ ) { 53 x[ ix ] = clbk.call( thisArg, x[ ix ], i, ix, x ); 54 ix += stride; 55 } 56 return x; 57 } 58 59 60 // EXPORTS // 61 62 module.exports = gfillBy;