ndarray.js (1946B)
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 {NonNegativeIntegerArray} offsets - array-like object containing the starting index (i.e., index offset) for the output array 30 * @param {Callback} fcn - nullary callback 31 * @returns {void} 32 * 33 * @example 34 * var Float64Array = require( '@stdlib/array/float64' ); 35 * 36 * function fill() { 37 * return 3.0; 38 * } 39 * 40 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 41 * 42 * var shape = [ x.length ]; 43 * var strides = [ 1 ]; 44 * var offsets = [ 0 ]; 45 * 46 * nullary( [ x ], shape, strides, offsets, fill ); 47 * 48 * console.log( x ); 49 * // => <Float64Array>[ 3.0, 3.0, 3.0, 3.0, 3.0 ] 50 */ 51 function nullary( arrays, shape, strides, offsets, fcn ) { 52 var sx; 53 var ix; 54 var x; 55 var N; 56 var i; 57 58 N = shape[ 0 ]; 59 if ( N <= 0 ) { 60 return; 61 } 62 ix = offsets[ 0 ]; 63 sx = strides[ 0 ]; 64 x = arrays[ 0 ]; 65 for ( i = 0; i < N; i++ ) { 66 x[ ix ] = fcn(); 67 ix += sx; 68 } 69 } 70 71 72 // EXPORTS // 73 74 module.exports = nullary;