Skip to content

Commit a13b269

Browse files
headlessNodekgrytestdlib-bot
authored
feat: add blas/ext/base/ndarray/gjoin-between
PR-URL: #11163 Closes: stdlib-js/metr-issue-tracker#227 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent f53f0cf commit a13b269

File tree

10 files changed

+902
-0
lines changed

10 files changed

+902
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# gjoinBetween
22+
23+
> Return a string by joining one-dimensional ndarray elements using a specified separator for each pair of consecutive elements.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gjoinBetween = require( '@stdlib/blas/ext/base/ndarray/gjoin-between' );
37+
```
38+
39+
#### gjoinBetween( arrays )
40+
41+
Returns a string by joining one-dimensional ndarray elements using a specified separator for each pair of consecutive elements.
42+
43+
```javascript
44+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = [ 1, 2, 3, 4 ];
48+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
49+
50+
var prefix = scalar2ndarray( 'op: ', {
51+
'dtype': 'generic'
52+
});
53+
54+
var suffix = scalar2ndarray( '', {
55+
'dtype': 'generic'
56+
});
57+
58+
var sbuf = [ ' + ', ' - ', ' != ' ];
59+
var separators = new ndarray( 'generic', sbuf, [ 3 ], [ 1 ], 0, 'row-major' );
60+
61+
var out = gjoinBetween( [ x, prefix, suffix, separators ] );
62+
// returns 'op: 1 + 2 - 3 != 4'
63+
```
64+
65+
The function has the following parameters:
66+
67+
- **arrays**: array-like object containing the following ndarrays:
68+
69+
- a one-dimensional input ndarray.
70+
- a zero-dimensional ndarray containing a prefix string.
71+
- a zero-dimensional ndarray containing a suffix string.
72+
- a one-dimensional ndarray containing separator strings.
73+
74+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<section class="notes">
79+
80+
## Notes
81+
82+
- If the input ndarray is empty, the function returns the prefix and suffix joined together.
83+
- The `separators` ndarray is assumed to have at least `N-1` elements (i.e., equal to the number of "gaps" between consecutive elements), where `N` is the number of elements in the input ndarray.
84+
- If an array element is either `null` or `undefined`, the function serializes the element as an empty string.
85+
86+
</section>
87+
88+
<!-- /.notes -->
89+
90+
<section class="examples">
91+
92+
## Examples
93+
94+
<!-- eslint no-undef: "error" -->
95+
96+
```javascript
97+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
98+
var filled = require( '@stdlib/array/base/filled' );
99+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
100+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
101+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
102+
var gjoinBetween = require( '@stdlib/blas/ext/base/ndarray/gjoin-between' );
103+
104+
var xbuf = discreteUniform( 10, -100, 100, {
105+
'dtype': 'generic'
106+
});
107+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
108+
console.log( ndarray2array( x ) );
109+
110+
var prefix = scalar2ndarray( '[ ', {
111+
'dtype': 'generic'
112+
});
113+
114+
var suffix = scalar2ndarray( ' ]', {
115+
'dtype': 'generic'
116+
});
117+
118+
var sbuf = filled( ' | ', xbuf.length - 1 );
119+
var separators = new ndarray( 'generic', sbuf, [ sbuf.length ], [ 1 ], 0, 'row-major' );
120+
121+
var out = gjoinBetween( [ x, prefix, suffix, separators ] );
122+
console.log( out );
123+
```
124+
125+
</section>
126+
127+
<!-- /.examples -->
128+
129+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
130+
131+
<section class="related">
132+
133+
</section>
134+
135+
<!-- /.related -->
136+
137+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
138+
139+
<section class="links">
140+
141+
</section>
142+
143+
<!-- /.links -->
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var filled = require( '@stdlib/array/base/filled' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
30+
var format = require( '@stdlib/string/format' );
31+
var pkg = require( './../package.json' ).name;
32+
var gjoinBetween = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var options = {
38+
'dtype': 'generic'
39+
};
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {PositiveInteger} len - array length
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( len ) {
52+
var separators;
53+
var prefix;
54+
var suffix;
55+
var sbuf;
56+
var xbuf;
57+
var x;
58+
59+
xbuf = uniform( len, 0.0, 100.0, options );
60+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
61+
62+
prefix = scalar2ndarray( '[ ', {
63+
'dtype': 'generic'
64+
});
65+
66+
suffix = scalar2ndarray( ' ]', {
67+
'dtype': 'generic'
68+
});
69+
70+
sbuf = filled( ',', len - 1 );
71+
separators = new ndarray( 'generic', sbuf, [ sbuf.length ], [ 1 ], 0, 'row-major' );
72+
73+
return benchmark;
74+
75+
/**
76+
* Benchmark function.
77+
*
78+
* @private
79+
* @param {Benchmark} b - benchmark instance
80+
*/
81+
function benchmark( b ) {
82+
var out;
83+
var i;
84+
85+
b.tic();
86+
for ( i = 0; i < b.iterations; i++ ) {
87+
out = gjoinBetween( [ x, prefix, suffix, separators ] );
88+
if ( !isString( out ) ) {
89+
b.fail( 'should return a string' );
90+
}
91+
}
92+
b.toc();
93+
if ( !isString( out ) ) {
94+
b.fail( 'should return a string' );
95+
}
96+
b.pass( 'benchmark finished' );
97+
b.end();
98+
}
99+
}
100+
101+
102+
// MAIN //
103+
104+
/**
105+
* Main execution sequence.
106+
*
107+
* @private
108+
*/
109+
function main() {
110+
var len;
111+
var min;
112+
var max;
113+
var f;
114+
var i;
115+
116+
min = 1; // 10^min
117+
max = 6; // 10^max
118+
119+
for ( i = min; i <= max; i++ ) {
120+
len = pow( 10, i );
121+
f = createBenchmark( len );
122+
bench( format( '%s:len=%d', pkg, len ), f );
123+
}
124+
}
125+
126+
main();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
{{alias}}( arrays )
3+
Returns a string by joining one-dimensional ndarray elements using a
4+
specified separator for each pair of consecutive elements.
5+
6+
If the input ndarray is empty, the function returns the prefix and suffix
7+
joined together.
8+
9+
If an array element is either `null` or `undefined`, the function serializes
10+
the element as an empty string.
11+
12+
Parameters
13+
----------
14+
arrays: ArrayLikeObject<ndarray>
15+
Array-like object containing the following ndarrays:
16+
17+
- a one-dimensional input ndarray.
18+
- a zero-dimensional ndarray containing a prefix string.
19+
- a zero-dimensional ndarray containing a suffix string.
20+
- a one-dimensional ndarray containing separator strings.
21+
22+
Returns
23+
-------
24+
out: string
25+
Joined string.
26+
27+
Examples
28+
--------
29+
> var xbuf = [ 1, 2, 3, 4 ];
30+
> var dt = 'generic';
31+
> var sh = [ xbuf.length ];
32+
> var sx = [ 1 ];
33+
> var ox = 0;
34+
> var ord = 'row-major';
35+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
36+
> var p = {{alias:@stdlib/ndarray/from-scalar}}( '', { 'dtype': dt } );
37+
> var s = {{alias:@stdlib/ndarray/from-scalar}}( '', { 'dtype': dt } );
38+
> var sbuf = [ ',', '-', '|' ];
39+
> var sh2 = [ sbuf.length ];
40+
> var sep = new {{alias:@stdlib/ndarray/ctor}}( dt, sbuf, sh2, sx, ox, ord );
41+
> {{alias}}( [ x, p, s, sep ] )
42+
'1,2-3|4'
43+
44+
See Also
45+
--------
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns a string by joining one-dimensional ndarray elements using a specified separator for each pair of consecutive elements.
27+
*
28+
* @param arrays - array-like object containing a one-dimensional input ndarray, a zero-dimensional ndarray containing a prefix string, a zero-dimensional ndarray containing a suffix string, and a one-dimensional ndarray containing separator strings
29+
* @returns joined string
30+
*
31+
* @example
32+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
33+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
34+
*
35+
* var xbuf = [ 1, 2, 3, 4 ];
36+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var prefix = scalar2ndarray( 'op: ', {
39+
* 'dtype': 'generic'
40+
* });
41+
*
42+
* var suffix = scalar2ndarray( '', {
43+
* 'dtype': 'generic'
44+
* });
45+
*
46+
* var sbuf = [ ' + ', ' - ', ' != ' ];
47+
* var separators = new ndarray( 'generic', sbuf, [ 3 ], [ 1 ], 0, 'row-major' );
48+
*
49+
* var v = gjoinBetween( [ x, prefix, suffix, separators ] );
50+
* // returns 'op: 1 + 2 - 3 != 4'
51+
*/
52+
declare function gjoinBetween<T = unknown>( arrays: [ typedndarray<T>, typedndarray<string>, typedndarray<string>, typedndarray<string> ] ): string;
53+
54+
55+
// EXPORTS //
56+
57+
export = gjoinBetween;

0 commit comments

Comments
 (0)