Skip to content

Commit 34c1828

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/gjoin-between
PR-URL: #11140 Closes: stdlib-js/metr-issue-tracker#208 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent d36550d commit 34c1828

File tree

15 files changed

+1665
-0
lines changed

15 files changed

+1665
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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 strided array elements using a specified separator for each pair of consecutive elements.
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 gjoinBetween = require( '@stdlib/blas/ext/base/gjoin-between' );
41+
```
42+
43+
#### gjoinBetween( N, prefix, suffix, x, strideX, separators, strideS )
44+
45+
Returns a string by joining strided array elements using a specified separator for each pair of consecutive elements.
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4 ];
49+
var sep = [ ' + ', ' - ', ' != ' ];
50+
51+
var str = gjoinBetween( x.length, 'op: ', '', x, 1, sep, 1 );
52+
// returns 'op: 1 + 2 - 3 != 4'
53+
```
54+
55+
The function has the following parameters:
56+
57+
- **N**: number of indexed elements.
58+
- **prefix**: string to prepend to the output string.
59+
- **suffix**: string to append to the output string.
60+
- **x**: input array.
61+
- **strideX**: stride length for `x`.
62+
- **separators**: separators array.
63+
- **strideS**: stride length for `separators`.
64+
65+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to join every other element:
66+
67+
```javascript
68+
var x = [ 1, 2, 3, 4, 5, 6 ];
69+
var sep = [ ',', '-' ];
70+
71+
var str = gjoinBetween( 3, '', '', x, 2, sep, 1 );
72+
// returns '1,3-5'
73+
```
74+
75+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
76+
77+
```javascript
78+
var Float64Array = require( '@stdlib/array/float64' );
79+
80+
// Initial array:
81+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
82+
83+
// Create an offset view:
84+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
85+
86+
// Join elements:
87+
var sep = [ ' | ', ' | ' ];
88+
var str = gjoinBetween( 3, '[', ']', x1, 2, sep, 1 );
89+
// returns '[2 | 4 | 6]'
90+
```
91+
92+
<!-- lint disable maximum-heading-length -->
93+
94+
#### gjoinBetween.ndarray( N, prefix, suffix, x, strideX, offsetX, separators, strideS, offsetS )
95+
96+
<!-- lint enable maximum-heading-length -->
97+
98+
Returns a string by joining strided array elements using a specified separator for each pair of consecutive elements and alternative indexing semantics.
99+
100+
```javascript
101+
var x = [ 1, 2, 3, 4 ];
102+
var sep = [ ' + ', ' - ', ' != ' ];
103+
104+
var str = gjoinBetween.ndarray( x.length, 'op: ', '', x, 1, 0, sep, 1, 0 );
105+
// returns 'op: 1 + 2 - 3 != 4'
106+
```
107+
108+
The function has the following additional parameters:
109+
110+
- **offsetX**: starting index for `x`.
111+
- **offsetS**: starting index for `separators`.
112+
113+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to access only the last three elements of the input strided array:
114+
115+
```javascript
116+
var x = [ 1, 2, 3, 4, 5, 6 ];
117+
var sep = [ ' | ', ' | ' ];
118+
119+
var str = gjoinBetween.ndarray( 3, '[ ', ' ]', x, 1, x.length-3, sep, 1, 0 );
120+
// returns '[ 4 | 5 | 6 ]'
121+
```
122+
123+
</section>
124+
125+
<!-- /.usage -->
126+
127+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
128+
129+
<section class="notes">
130+
131+
## Notes
132+
133+
- If `N <= 0`, both functions return the prefix and suffix joined together.
134+
- The `separators` array is assumed to have at least `N-1` indexed elements (i.e., equal to the number of "gaps" between consecutive elements).
135+
- If an array element is either `null` or `undefined`, both functions serialize the element as an empty string.
136+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
137+
138+
</section>
139+
140+
<!-- /.notes -->
141+
142+
<!-- Package usage examples. -->
143+
144+
<section class="examples">
145+
146+
## Examples
147+
148+
<!-- eslint no-undef: "error" -->
149+
150+
```javascript
151+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
152+
var filled = require( '@stdlib/array/base/filled' );
153+
var gjoinBetween = require( '@stdlib/blas/ext/base/gjoin-between' );
154+
155+
var x = discreteUniform( 10, -100, 100, {
156+
'dtype': 'generic'
157+
});
158+
console.log( x );
159+
160+
var sep = filled( ' | ', x.length - 1 );
161+
var out = gjoinBetween( x.length, '[ ', ' ]', x, 1, sep, 1 );
162+
console.log( out );
163+
```
164+
165+
</section>
166+
167+
<!-- /.examples -->
168+
169+
<!-- 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. -->
170+
171+
<section class="references">
172+
173+
</section>
174+
175+
<!-- /.references -->
176+
177+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
178+
179+
<section class="related">
180+
181+
</section>
182+
183+
<!-- /.related -->
184+
185+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
186+
187+
<section class="links">
188+
189+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
190+
191+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
192+
193+
</section>
194+
195+
<!-- /.links -->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
26+
var oneTo = require( '@stdlib/array/one-to' );
27+
var filled = require( '@stdlib/array/base/filled' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gjoinBetween = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var sep = filled( ',', len - 1 );
44+
var x = oneTo( len, 'float64' );
45+
return benchmark;
46+
47+
/**
48+
* Benchmark function.
49+
*
50+
* @private
51+
* @param {Benchmark} b - benchmark instance
52+
*/
53+
function benchmark( b ) {
54+
var out;
55+
var i;
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
out = gjoinBetween( x.length, '', '', x, 1, sep, 1 );
60+
if ( !isString( out ) ) {
61+
b.fail( 'should return a string' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isString( out ) ) {
66+
b.fail( 'should return a string' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var len;
83+
var min;
84+
var max;
85+
var f;
86+
var i;
87+
88+
min = 1; // 10^min
89+
max = 6; // 10^max
90+
91+
for ( i = min; i <= max; i++ ) {
92+
len = pow( 10, i );
93+
94+
f = createBenchmark( len );
95+
bench( format( '%s:len=%d', pkg, len ), f );
96+
}
97+
}
98+
99+
main();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
26+
var oneTo = require( '@stdlib/array/one-to' );
27+
var filled = require( '@stdlib/array/base/filled' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gjoinBetween = require( './../lib/ndarray.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var sep = filled( ',', len - 1 );
44+
var x = oneTo( len, 'float64' );
45+
return benchmark;
46+
47+
/**
48+
* Benchmark function.
49+
*
50+
* @private
51+
* @param {Benchmark} b - benchmark instance
52+
*/
53+
function benchmark( b ) {
54+
var out;
55+
var i;
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
out = gjoinBetween( x.length, '', '', x, 1, 0, sep, 1, 0 );
60+
if ( !isString( out ) ) {
61+
b.fail( 'should return a string' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isString( out ) ) {
66+
b.fail( 'should return a string' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var len;
83+
var min;
84+
var max;
85+
var f;
86+
var i;
87+
88+
min = 1; // 10^min
89+
max = 6; // 10^max
90+
91+
for ( i = min; i <= max; i++ ) {
92+
len = pow( 10, i );
93+
94+
f = createBenchmark( len );
95+
bench( format( '%s:ndarray:len=%d', pkg, len ), f );
96+
}
97+
}
98+
99+
main();

0 commit comments

Comments
 (0)