Skip to content

Commit ed3693f

Browse files
committed
feat: add some-by
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent d03bb0e commit ed3693f

File tree

20 files changed

+2626
-0
lines changed

20 files changed

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

0 commit comments

Comments
 (0)