main.js (1809B)
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 * Applies a nullary callback and assigns results to elements in a strided output array. 25 * 26 * @param {ArrayLikeObject<Collection>} arrays - array-like object containing one output array 27 * @param {NonNegativeIntegerArray} shape - array-like object containing a single element, the number of indexed elements 28 * @param {IntegerArray} strides - array-like object containing the stride length for the output array 29 * @param {Callback} fcn - nullary callback 30 * @returns {void} 31 * 32 * @example 33 * var Float64Array = require( '@stdlib/array/float64' ); 34 * 35 * function fill() { 36 * return 3.0; 37 * } 38 * 39 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 40 * 41 * var shape = [ x.length ]; 42 * var strides = [ 1 ]; 43 * 44 * nullary( [ x ], shape, strides, fill ); 45 * 46 * console.log( x ); 47 * // => <Float64Array>[ 3.0, 3.0, 3.0, 3.0, 3.0 ] 48 */ 49 function nullary( arrays, shape, strides, fcn ) { 50 var sx; 51 var ix; 52 var x; 53 var N; 54 var i; 55 56 N = shape[ 0 ]; 57 if ( N <= 0 ) { 58 return; 59 } 60 sx = strides[ 0 ]; 61 if ( sx < 0 ) { 62 ix = (1-N) * sx; 63 } else { 64 ix = 0; 65 } 66 x = arrays[ 0 ]; 67 for ( i = 0; i < N; i++ ) { 68 x[ ix ] = fcn(); 69 ix += sx; 70 } 71 } 72 73 74 // EXPORTS // 75 76 module.exports = nullary;