|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# gjoin |
| 22 | + |
| 23 | +> Return a string created by joining strided array elements using a specified separator. |
| 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 gjoin = require( '@stdlib/blas/ext/base/gjoin' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### gjoin( N, separator, x, strideX ) |
| 44 | + |
| 45 | +Returns a string created by joining strided array elements using a specified separator. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var x = [ 1.0, 2.0, 3.0, 4.0 ]; |
| 49 | + |
| 50 | +var str = gjoin( x.length, ',', x, 1 ); |
| 51 | +// returns '1,2,3,4' |
| 52 | +``` |
| 53 | + |
| 54 | +The function has the following parameters: |
| 55 | + |
| 56 | +- **N**: number of indexed elements. |
| 57 | +- **separator**: separator. |
| 58 | +- **x**: input array. |
| 59 | +- **strideX**: stride length. |
| 60 | + |
| 61 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to join every other element: |
| 62 | + |
| 63 | +```javascript |
| 64 | +var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; |
| 65 | + |
| 66 | +var str = gjoin( 3, '-', x, 2 ); |
| 67 | +// returns '1-3-5' |
| 68 | +``` |
| 69 | + |
| 70 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 71 | + |
| 72 | +```javascript |
| 73 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 74 | + |
| 75 | +// Initial array: |
| 76 | +var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 77 | + |
| 78 | +// Create an offset view: |
| 79 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 80 | + |
| 81 | +// Join elements: |
| 82 | +var str = gjoin( 3, '|', x1, 2 ); |
| 83 | +// returns '2|4|6' |
| 84 | +``` |
| 85 | + |
| 86 | +#### gjoin.ndarray( N, separator, x, strideX, offsetX ) |
| 87 | + |
| 88 | +Returns a string created by joining strided array elements using a specified separator and alternative indexing semantics. |
| 89 | + |
| 90 | +```javascript |
| 91 | +var x = [ 1.0, 2.0, 3.0, 4.0 ]; |
| 92 | + |
| 93 | +var str = gjoin.ndarray( x.length, ',', x, 1, 0 ); |
| 94 | +// returns '1,2,3,4' |
| 95 | +``` |
| 96 | + |
| 97 | +The function has the following additional parameters: |
| 98 | + |
| 99 | +- **offsetX**: starting index. |
| 100 | + |
| 101 | +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: |
| 102 | + |
| 103 | +```javascript |
| 104 | +var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; |
| 105 | + |
| 106 | +var str = gjoin.ndarray( 3, '|', x, 1, x.length-3 ); |
| 107 | +// returns '4|5|6' |
| 108 | +``` |
| 109 | + |
| 110 | +</section> |
| 111 | + |
| 112 | +<!-- /.usage --> |
| 113 | + |
| 114 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 115 | + |
| 116 | +<section class="notes"> |
| 117 | + |
| 118 | +## Notes |
| 119 | + |
| 120 | +- If `N <= 0`, both functions return an empty string. |
| 121 | +- If an array element is either `null` or `undefined`, both functions will serialize the element as an empty string. |
| 122 | +- 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]). |
| 123 | + |
| 124 | +</section> |
| 125 | + |
| 126 | +<!-- /.notes --> |
| 127 | + |
| 128 | +<!-- Package usage examples. --> |
| 129 | + |
| 130 | +<section class="examples"> |
| 131 | + |
| 132 | +## Examples |
| 133 | + |
| 134 | +<!-- eslint no-undef: "error" --> |
| 135 | + |
| 136 | +```javascript |
| 137 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 138 | +var gjoin = require( '@stdlib/blas/ext/base/gjoin' ); |
| 139 | + |
| 140 | +var x = discreteUniform( 10, -100, 100, { |
| 141 | + 'dtype': 'generic' |
| 142 | +}); |
| 143 | +console.log( x ); |
| 144 | + |
| 145 | +var out = gjoin( x.length, ' | ', x, 1 ); |
| 146 | +console.log( out ); |
| 147 | +``` |
| 148 | + |
| 149 | +</section> |
| 150 | + |
| 151 | +<!-- /.examples --> |
| 152 | + |
| 153 | +<!-- 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. --> |
| 154 | + |
| 155 | +<section class="references"> |
| 156 | + |
| 157 | +</section> |
| 158 | + |
| 159 | +<!-- /.references --> |
| 160 | + |
| 161 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 162 | + |
| 163 | +<section class="related"> |
| 164 | + |
| 165 | +</section> |
| 166 | + |
| 167 | +<!-- /.related --> |
| 168 | + |
| 169 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 170 | + |
| 171 | +<section class="links"> |
| 172 | + |
| 173 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 174 | + |
| 175 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.links --> |
0 commit comments