Skip to content

Commit aca1246

Browse files
committed
Add pkg to return a list of safe and same "kind" casts
1 parent 8ae22e6 commit aca1246

File tree

11 files changed

+885
-0
lines changed

11 files changed

+885
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# Same Kind Casts
22+
23+
> Return a list of ndarray [data types][@stdlib/ndarray/dtypes] to which a provided ndarray [data type][@stdlib/ndarray/dtypes] can be safely cast or cast within the same "kind".
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 sameKindCasts = require( '@stdlib/ndarray/same-kind-casts' );
41+
```
42+
43+
#### sameKindCasts( dtype )
44+
45+
Returns a list of ndarray [data types][@stdlib/ndarray/dtypes] to which a provided ndarray [data type][@stdlib/ndarray/dtypes] can be safely cast or cast within the same "kind" (e.g., between signed integers or between floating-point numbers).
46+
47+
```javascript
48+
var out = sameKindCasts( 'float64' );
49+
// e.g., returns [ float64', 'float32', 'generic' ]
50+
```
51+
52+
If provided an unrecognized or unsupported `dtype`, the function returns `null`.
53+
54+
```javascript
55+
var out = sameKindCasts( 'foo' );
56+
// returns null
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
</section>
68+
69+
<!-- /.notes -->
70+
71+
<!-- Package usage examples. -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var dtypes = require( '@stdlib/ndarray/dtypes' );
81+
var sameKindCasts = require( '@stdlib/ndarray/same-kind-casts' );
82+
83+
var DTYPES;
84+
var list;
85+
var i;
86+
87+
// Get the list of supported ndarray data types:
88+
DTYPES = dtypes();
89+
90+
// Print the list of ndarray data types to which a data type can be cast...
91+
for ( i = 0; i < DTYPES.length; i++ ) {
92+
list = sameKindCasts( DTYPES[ i ] );
93+
console.log( '%s: %s', DTYPES[ i ], list.join( ', ' ) );
94+
}
95+
```
96+
97+
</section>
98+
99+
<!-- /.examples -->
100+
101+
<!-- 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. -->
102+
103+
<section class="references">
104+
105+
</section>
106+
107+
<!-- /.references -->
108+
109+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
110+
111+
<section class="links">
112+
113+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
114+
115+
</section>
116+
117+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
25+
var dtypes = require( '@stdlib/ndarray/dtypes' );
26+
var pkg = require( './../package.json' ).name;
27+
var sameKindCasts = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var out;
34+
var dt;
35+
var i;
36+
37+
dt = dtypes();
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
out = sameKindCasts( dt[ i%dt.length ] );
42+
if ( out.length === 0 ) {
43+
b.fail( 'should not be empty' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isStringArray( out ) ) {
48+
b.fail( 'should return an array of strings' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( dtype )
3+
Returns a list of ndarray data types to which a provided ndarray data type
4+
can be safely cast or cast within the same "kind".
5+
6+
If provided an unrecognized ndarray data type, the function returns `null`.
7+
8+
Parameters
9+
----------
10+
dtype: string
11+
ndarray data type.
12+
13+
Returns
14+
-------
15+
out: Array<string>|null
16+
List of ndarray data types to which a provided ndarray data type can be
17+
safely cast or cast within the same "kind".
18+
19+
Examples
20+
--------
21+
> var out = {{alias}}( 'float32' )
22+
<Array>
23+
24+
See Also
25+
--------
26+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
var dtypes = require( '@stdlib/ndarray/dtypes' );
22+
var sameKindCasts = require( './../lib' );
23+
24+
var DTYPES;
25+
var list;
26+
var i;
27+
28+
// Get the list of supported ndarray data types:
29+
DTYPES = dtypes();
30+
31+
// Print the list of ndarray data types to which a data type can be cast...
32+
for ( i = 0; i < DTYPES.length; i++ ) {
33+
list = sameKindCasts( DTYPES[ i ] );
34+
console.log( '%s: %s', DTYPES[ i ], list.join( ', ' ) );
35+
}

0 commit comments

Comments
 (0)