|
| 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 --> |
0 commit comments