README.md (5744B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2020 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Filled Array 22 23 > Create a filled array. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var filledarray = require( '@stdlib/array/filled' ); 41 ``` 42 43 #### filledarray( \[dtype] ) 44 45 Creates a filled array having a specified data type `dtype`. 46 47 ```javascript 48 var arr = filledarray(); 49 // returns <Float64Array> 50 ``` 51 52 The function recognizes the following data types: 53 54 - `float64`: double-precision floating-point numbers (IEEE 754) 55 - `float32`: single-precision floating-point numbers (IEEE 754) 56 - `int32`: 32-bit two's complement signed integers 57 - `uint32`: 32-bit unsigned integers 58 - `int16`: 16-bit two's complement signed integers 59 - `uint16`: 16-bit unsigned integers 60 - `int8`: 8-bit two's complement signed integers 61 - `uint8`: 8-bit unsigned integers 62 - `uint8c`: 8-bit unsigned integers clamped to `0-255` 63 - `generic`: generic JavaScript values 64 65 By default, the output array data type is `float64` (i.e., a [typed array][mdn-typed-array]). To specify an alternative data type, provide a `dtype` argument. 66 67 ```javascript 68 var arr = filledarray( 'int32' ); 69 // returns <Int32Array> 70 ``` 71 72 #### filledarray( value, length\[, dtype] ) 73 74 Returns a filled array having a specified `length`. 75 76 ```javascript 77 var arr1 = filledarray( 1.0, 5 ); 78 // returns <Float64Array>[ 1.0, 1.0, 1.0, 1.0, 1.0 ] 79 80 var arr2 = filledarray( 1, 5, 'uint8' ); 81 // returns <Uint8Array>[ 1, 1, 1, 1, 1 ] 82 ``` 83 84 #### filledarray( value, array\[, dtype] ) 85 86 Creates a filled array from another array (or array-like object). 87 88 ```javascript 89 var arr0 = { 90 '0': 0.5, 91 '1': 0.5, 92 '2': 0.5, 93 'length': 3 94 }; 95 96 var arr1 = filledarray( 1.0, arr0 ); 97 // returns <Float64Array>[ 1.0, 1.0, 1.0 ] 98 99 var arr2 = filledarray( 2.0, arr1 ); 100 // returns <Float64Array>[ 2.0, 2.0, 2.0 ] 101 102 var arr3 = filledarray( 3, arr1, 'int32' ); 103 // returns <Int32Array>[ 3, 3, 3 ] 104 ``` 105 106 #### filledarray( value, iterable\[, dtype] ) 107 108 Creates a filled array from an iterable. 109 110 ```javascript 111 var iterConstant = require( '@stdlib/iter/constant' ); 112 113 var it = iterConstant( 3.0, { 114 'iter': 3 115 }); 116 var arr1 = filledarray( 1.0, it ); 117 // returns <Float64Array>[ 1.0, 1.0, 1.0 ] 118 119 var arr2 = filledarray( 1.0, it, 'float32' ); 120 // returns <Float32Array>[ 1.0, 1.0, 1.0 ] 121 ``` 122 123 #### filledarray( value, buffer\[, byteOffset\[, length]]\[, dtype] ) 124 125 Returns a filled [typed array][mdn-typed-array] view of an [`ArrayBuffer`][mdn-arraybuffer]. 126 127 ```javascript 128 var ArrayBuffer = require( '@stdlib/array/buffer' ); 129 130 var buf = new ArrayBuffer( 32 ); 131 var arr = filledarray( 1.0, buf ); 132 // returns <Float64Array>[ 1.0, 1.0, 1.0, 1.0 ] 133 134 buf = new ArrayBuffer( 32 ); 135 arr = filledarray( 1.0, buf, 'float32' ); 136 // returns <Float32Array>[ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] 137 138 buf = new ArrayBuffer( 32 ); 139 arr = filledarray( 1.0, buf, 16 ); 140 // returns <Float64Array>[ 1.0, 1.0 ] 141 142 buf = new ArrayBuffer( 32 ); 143 arr = filledarray( 1.0, buf, 16, 'float32' ); 144 // returns <Float32Array>[ 1.0, 1.0, 1.0, 1.0 ] 145 146 buf = new ArrayBuffer( 32 ); 147 arr = filledarray( 1.0, buf, 16, 1 ); 148 // returns <Float64Array>[ 1.0 ] 149 150 buf = new ArrayBuffer( 32 ); 151 arr = filledarray( 1, buf, 10, 4, 'int16' ); 152 // returns <Int16Array>[ 1, 1, 1, 1 ] 153 ``` 154 155 </section> 156 157 <!-- /.usage --> 158 159 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 160 161 <section class="notes"> 162 163 ## Notes 164 165 - Creating a generic [array][mdn-array] from an [`ArrayBuffer`][mdn-arraybuffer] is **not** supported. 166 167 </section> 168 169 <!-- /.notes --> 170 171 <!-- Package usage examples. --> 172 173 <section class="examples"> 174 175 ## Examples 176 177 <!-- eslint no-undef: "error" --> 178 179 ```javascript 180 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 181 var dtypes = require( '@stdlib/array/dtypes' ); 182 var filledarray = require( '@stdlib/array/filled' ); 183 184 // Generate a random number: 185 var r = discreteUniform( 0, 100 ); 186 187 // Get the list of supported array data types: 188 var dt = dtypes(); 189 190 // Generate filled arrays... 191 var arr; 192 var i; 193 for ( i = 0; i < dt.length; i++ ) { 194 arr = filledarray( r, 10, dt[ i ] ); 195 console.log( arr ); 196 } 197 ``` 198 199 </section> 200 201 <!-- /.examples --> 202 203 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 204 205 <section class="references"> 206 207 </section> 208 209 <!-- /.references --> 210 211 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 212 213 <section class="links"> 214 215 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 216 217 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 218 219 [mdn-arraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer 220 221 </section> 222 223 <!-- /.links -->