You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: to-fancy/README.md
+114-18
Original file line number
Diff line number
Diff line change
@@ -113,8 +113,19 @@ v = y[ ':' ];
113
113
114
114
The function supports the following options:
115
115
116
+
-**cache**: cache for resolving array index objects. Must have a `get` method which accepts a single argument: a string identifier associated with an array index.
117
+
118
+
If an array index associated with a provided identifier exists, the `get` method should return an object having the following properties:
119
+
120
+
-**data**: the underlying index array.
121
+
-**type**: the index type. Must be either `'mask'`, `'bool'`, or `'int'`.
122
+
-**dtype**: the [data type][@stdlib/array/dtypes] of the underlying array.
123
+
124
+
If an array index is not associated with a provided identifier, the `get` method should return `null`.
-**cache**: cache for resolving array index objects. Default: [`ArrayIndex`][@stdlib/array/index].
118
129
119
130
By default, the function returns a fancy array which does **not** enforce strict bounds checking. For example,
120
131
@@ -156,8 +167,19 @@ var v = y[ ':' ];
156
167
157
168
The function supports the following options:
158
169
170
+
-**cache**: default cache for resolving array index objects. Must have a `get` method which accepts a single argument: a string identifier associated with an array index.
171
+
172
+
If an array index associated with a provided identifier exists, the `get` method should return an object having the following properties:
173
+
174
+
-**data**: the underlying index array.
175
+
-**type**: the index type. Must be either `'mask'`, `'bool'`, or `'int'`.
176
+
-**dtype**: the [data type][@stdlib/array/dtypes] of the underlying array.
177
+
178
+
If an array index is not associated with a provided identifier, the `get` method should return `null`.
179
+
180
+
Default: [`ArrayIndex`][@stdlib/array/index].
181
+
159
182
-**strict**: boolean indicating whether to enforce strict bounds checking by default. Default: `false`.
160
-
-**cache**: cache for resolving array index objects. Default: [`ArrayIndex`][@stdlib/array/index].
161
183
162
184
By default, the function returns a function which, by default, does **not** enforce strict bounds checking. For example,
163
185
@@ -205,6 +227,72 @@ The returned function supports the same options as above. When the returned func
205
227
- Indexing expressions provide a convenient and powerful means for creating and operating on array views; however, their use does entail a performance cost. Indexing expressions are best suited for interactive use (e.g., in the [REPL][@stdlib/repl]) and scripting. For performance critical applications, prefer equivalent functional APIs supporting array-like objects.
206
228
- In older JavaScript environments which do **not** support [`Proxy`][@stdlib/proxy/ctor] objects, the use of indexing expressions is **not** supported.
207
229
230
+
### Bounds Checking
231
+
232
+
By default, fancy arrays do **not** enforce strict bounds checking across index expressions. The motivation for the default fancy array behavior stems from a desire to maintain parity with plain arrays; namely, the returning of `undefined` when accessing a single non-existent property.
233
+
234
+
Accordingly, when `strict` is `false`, one may observe the following behaviors:
235
+
236
+
<!-- run throws: true -->
237
+
238
+
```javascript
239
+
var idx =require( '@stdlib/array/index' );
240
+
241
+
var x =array2fancy( [ 1, 2, 3, 4 ], {
242
+
'strict':false
243
+
});
244
+
245
+
// Access a non-existent property:
246
+
var v = x[ 'foo' ];
247
+
// returns undefined
248
+
249
+
// Access an out-of-bounds index:
250
+
v = x[ 10 ];
251
+
// returns undefined
252
+
253
+
v = x[ -10 ];
254
+
// returns undefined
255
+
256
+
// Access an out-of-bounds slice:
257
+
v = x[ '10:' ];
258
+
// returns []
259
+
260
+
// Access one or more out-of-bounds indices:
261
+
v = x[ idx( [ 10, 20 ] ) ];
262
+
// throws <RangeError>
263
+
```
264
+
265
+
When `strict` is `true`, fancy arrays normalize index behavior and consistently enforce strict bounds checking.
266
+
267
+
<!-- run throws: true -->
268
+
269
+
```javascript
270
+
var idx =require( '@stdlib/array/index' );
271
+
272
+
var x =array2fancy( [ 1, 2, 3, 4 ], {
273
+
'strict':true
274
+
});
275
+
276
+
// Access a non-existent property:
277
+
var v = x[ 'foo' ];
278
+
// returns undefined
279
+
280
+
// Access an out-of-bounds index:
281
+
v = x[ 10 ];
282
+
// throws <RangeError>
283
+
284
+
v = x[ -10 ];
285
+
// throws <RangeError>
286
+
287
+
// Access an out-of-bounds slice:
288
+
v = x[ '10:' ];
289
+
// throws <RangeError>
290
+
291
+
// Access one or more out-of-bounds indices:
292
+
v = x[ idx( [ 10, 20 ] ) ];
293
+
// throws <RangeError>
294
+
```
295
+
208
296
### Broadcasting
209
297
210
298
Fancy arrays support **broadcasting** in which assigned scalars and single-element arrays are repeated (without additional memory allocation) to match the length of a target array instance.
@@ -340,22 +428,6 @@ im = imag( v );
340
428
// returns 0.0
341
429
```
342
430
343
-
Note, however, that attempting to assign a real-valued array to a complex number array slice is **not** supported due to the ambiguity of whether the real-valued array is a collection of real components (with implied imaginary components equal to zero) or an array of interleaved real and imaginary components.
var Complex128Array =require( '@stdlib/array/complex128' );
350
-
351
-
var x =newComplex128Array( [ 1.0, 2.0, 3.0, 4.0 ] );
352
-
var y =array2fancy( x );
353
-
354
-
// Attempt to assign a real-valued array:
355
-
y[ ':' ] =newFloat64Array( [ 5.0, 6.0 ] ); // is this a single complex number which should be broadcast or a list of real components with implied imaginary components?
356
-
// throws <Error>
357
-
```
358
-
359
431
</section>
360
432
361
433
<!-- /.notes -->
@@ -371,12 +443,16 @@ y[ ':' ] = new Float64Array( [ 5.0, 6.0 ] ); // is this a single complex number
371
443
<!-- eslint no-undef: "error" -->
372
444
373
445
```javascript
446
+
varUint8Array=require( '@stdlib/array/uint8' );
447
+
varInt32Array=require( '@stdlib/array/int32' );
448
+
var idx =require( '@stdlib/array/index' );
374
449
var array2fancy =require( '@stdlib/array/to-fancy' );
0 commit comments