Skip to content

Commit e146f6e

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

File tree

14 files changed

+1557
-0
lines changed

14 files changed

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

0 commit comments

Comments
 (0)