|
| 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 | +# someBy |
| 22 | + |
| 23 | +> Test whether at least `n` elements in an ndarray pass a test implemented by a predicate function. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var someBy = require( '@stdlib/ndarray/base/some-by' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### someBy( arrays, predicate\[, thisArg ] ) |
| 40 | + |
| 41 | +Tests whether at least `n` elements in an ndarray pass a test implemented by a predicate function. |
| 42 | + |
| 43 | +<!-- eslint-disable max-len --> |
| 44 | + |
| 45 | +```javascript |
| 46 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 47 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 48 | + |
| 49 | +function predicate( value ) { |
| 50 | + return value > 0.0; |
| 51 | +} |
| 52 | + |
| 53 | +// Create a data buffer: |
| 54 | +var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 55 | + |
| 56 | +// Define the shape of the input array: |
| 57 | +var shape = [ 3, 1, 2 ]; |
| 58 | + |
| 59 | +// Define the array strides: |
| 60 | +var sx = [ 4, 4, 1 ]; |
| 61 | + |
| 62 | +// Define the index offset: |
| 63 | +var ox = 0; |
| 64 | + |
| 65 | +// Create the input ndarray-like object: |
| 66 | +var x = { |
| 67 | + 'dtype': 'float64', |
| 68 | + 'data': xbuf, |
| 69 | + 'shape': shape, |
| 70 | + 'strides': sx, |
| 71 | + 'offset': ox, |
| 72 | + 'order': 'row-major' |
| 73 | +}; |
| 74 | + |
| 75 | +// Define the success criterion: |
| 76 | +var n = scalar2ndarray( 3, { |
| 77 | + 'dtype': 'generic' |
| 78 | +}); |
| 79 | + |
| 80 | +// Test elements: |
| 81 | +var out = someBy( [ x, n ], predicate ); |
| 82 | +// returns true |
| 83 | +``` |
| 84 | + |
| 85 | +The function accepts the following arguments: |
| 86 | + |
| 87 | +- **arrays**: array-like object containing an input ndarray and a zero-dimensional ndarray specifying the minimum number of elements in the input ndarray that must satisfy the predicate function. |
| 88 | +- **predicate**: predicate function. |
| 89 | +- **thisArg**: predicate function execution context (_optional_). |
| 90 | + |
| 91 | +Each provided ndarray should be an `object` with the following properties: |
| 92 | + |
| 93 | +- **dtype**: data type. |
| 94 | +- **data**: data buffer. |
| 95 | +- **shape**: dimensions. |
| 96 | +- **strides**: stride lengths. |
| 97 | +- **offset**: index offset. |
| 98 | +- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). |
| 99 | + |
| 100 | +The predicate function is provided the following arguments: |
| 101 | + |
| 102 | +- **value**: current array element. |
| 103 | +- **indices**: current array element indices. |
| 104 | +- **arr**: the input ndarray. |
| 105 | + |
| 106 | +To set the predicate function execution context, provide a `thisArg`. |
| 107 | + |
| 108 | +<!-- eslint-disable no-invalid-this, max-len --> |
| 109 | + |
| 110 | +```javascript |
| 111 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 112 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 113 | + |
| 114 | +function predicate( value ) { |
| 115 | + this.count += 1; |
| 116 | + return value > 0.0; |
| 117 | +} |
| 118 | + |
| 119 | +// Create a data buffer: |
| 120 | +var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 121 | + |
| 122 | +// Define the shape of the input array: |
| 123 | +var shape = [ 3, 1, 2 ]; |
| 124 | + |
| 125 | +// Define the array strides: |
| 126 | +var sx = [ 4, 4, 1 ]; |
| 127 | + |
| 128 | +// Define the index offset: |
| 129 | +var ox = 0; |
| 130 | + |
| 131 | +// Create the input ndarray-like object: |
| 132 | +var x = { |
| 133 | + 'dtype': 'float64', |
| 134 | + 'data': xbuf, |
| 135 | + 'shape': shape, |
| 136 | + 'strides': sx, |
| 137 | + 'offset': ox, |
| 138 | + 'order': 'row-major' |
| 139 | +}; |
| 140 | + |
| 141 | +// Define the success criterion: |
| 142 | +var n = scalar2ndarray( 6, { |
| 143 | + 'dtype': 'generic' |
| 144 | +}); |
| 145 | + |
| 146 | +// Create a context object: |
| 147 | +var ctx = { |
| 148 | + 'count': 0 |
| 149 | +}; |
| 150 | + |
| 151 | +// Test elements: |
| 152 | +var out = someBy( [ x, n ], predicate, ctx ); |
| 153 | +// returns true |
| 154 | + |
| 155 | +var count = ctx.count; |
| 156 | +// returns 6 |
| 157 | +``` |
| 158 | + |
| 159 | +</section> |
| 160 | + |
| 161 | +<!-- /.usage --> |
| 162 | + |
| 163 | +<section class="notes"> |
| 164 | + |
| 165 | +## Notes |
| 166 | + |
| 167 | +- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing the operation in order to achieve better performance. |
| 168 | + |
| 169 | +</section> |
| 170 | + |
| 171 | +<!-- /.notes --> |
| 172 | + |
| 173 | +<section class="examples"> |
| 174 | + |
| 175 | +## Examples |
| 176 | + |
| 177 | +<!-- eslint no-undef: "error" --> |
| 178 | + |
| 179 | +```javascript |
| 180 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 181 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 182 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 183 | +var someBy = require( '@stdlib/ndarray/base/some-by' ); |
| 184 | + |
| 185 | +function predicate( value ) { |
| 186 | + return value > 0; |
| 187 | +} |
| 188 | + |
| 189 | +var x = { |
| 190 | + 'dtype': 'generic', |
| 191 | + 'data': discreteUniform( 10, -2, 10, { |
| 192 | + 'dtype': 'generic' |
| 193 | + }), |
| 194 | + 'shape': [ 5, 2 ], |
| 195 | + 'strides': [ 2, 1 ], |
| 196 | + 'offset': 0, |
| 197 | + 'order': 'row-major' |
| 198 | +}; |
| 199 | +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); |
| 200 | + |
| 201 | +var n = scalar2ndarray( 5, { |
| 202 | + 'dtype': 'generic' |
| 203 | +}); |
| 204 | + |
| 205 | +var out = someBy( [ x, n ], predicate ); |
| 206 | +console.log( out ); |
| 207 | +``` |
| 208 | + |
| 209 | +</section> |
| 210 | + |
| 211 | +<!-- /.examples --> |
| 212 | + |
| 213 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 214 | + |
| 215 | +<section class="related"> |
| 216 | + |
| 217 | +</section> |
| 218 | + |
| 219 | +<!-- /.related --> |
| 220 | + |
| 221 | +<section class="links"> |
| 222 | + |
| 223 | +</section> |
| 224 | + |
| 225 | +<!-- /.links --> |
0 commit comments