Skip to content

Commit a958397

Browse files
committed
chore: clean-up implementation
1 parent 585e867 commit a958397

File tree

15 files changed

+945
-886
lines changed

15 files changed

+945
-886
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# cuanyByRight
22+
23+
> Cumulatively test whether at least one element in a provided array passes a test implemented by a predicate function, while iterating from right-to-left.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var cuanyByRight = require( '@stdlib/array/base/cuany-by-right' );
31+
```
32+
33+
#### cuanyByRight( x, predicate\[, thisArg] )
34+
35+
Cumulatively tests whether at least one element in a provided array passes a test implemented by a `predicate` function, while iterating from right-to-left.
36+
37+
```javascript
38+
function isPositive( value ) {
39+
return ( value > 0 );
40+
}
41+
42+
var x = [ 0, 0, 0, 1, 0 ];
43+
44+
var y = cuanyByRight( x, isPositive );
45+
// returns [ false, true, true, true, true ]
46+
```
47+
48+
The `predicate` function is provided three arguments:
49+
50+
- **value**: current array element.
51+
- **index**: current array element index.
52+
- **arr**: input array.
53+
54+
To set the `predicate` function execution context, provide a `thisArg`.
55+
56+
```javascript
57+
function isPositive( value ) {
58+
this.count += 1;
59+
return ( value > 0 );
60+
}
61+
62+
var x = [ 0, 1, 0, 0, 0 ];
63+
64+
var context = {
65+
'count': 0
66+
};
67+
68+
var out = cuanyByRight( x, isPositive, context );
69+
// returns [ false, false, false, true, true ]
70+
71+
var cnt = context.count;
72+
// returns 4
73+
```
74+
75+
#### cuanyByRight.assign( x, out, stride, offset, predicate\[, thisArg] )
76+
77+
Cumulatively tests whether at least one element in a provided array passes a test implemented by a `predicate` function, while iterating from right-to-left, and assigns results to a provided output array.
78+
79+
```javascript
80+
function isPositive( value ) {
81+
return ( value > 0 );
82+
}
83+
84+
var x = [ 0, 1, 0, 0, 0 ];
85+
var y = [ false, null, false, null, false, null, false, null, false, null ];
86+
87+
var out = cuanyByRight.assign( x, y, 2, 0, isPositive );
88+
// returns [ false, null, false, null, false, null, true, null, true, null ]
89+
90+
var bool = ( out === y );
91+
// returns true
92+
```
93+
94+
The function supports the following parameters:
95+
96+
- **x**: input array.
97+
- **out**: output array.
98+
- **stride**: output array stride.
99+
- **offset**: output array offset.
100+
- **predicate**: test function.
101+
102+
</section>
103+
104+
<!-- /.usage -->
105+
106+
<section class="notes">
107+
108+
</section>
109+
110+
<!-- /.notes -->
111+
112+
<section class="examples">
113+
114+
## Examples
115+
116+
<!-- eslint no-undef: "error" -->
117+
118+
```javascript
119+
function isPositive( value ) {
120+
return ( value > 0 );
121+
}
122+
123+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
124+
var cuanyByRight = require( '@stdlib/array/base/cuany-by-right' );
125+
126+
// Create an array of random values:
127+
var x = bernoulli( 10, 0.1 );
128+
console.log( x );
129+
130+
var out = cuanyByRight( x, isPositive );
131+
console.log( out );
132+
```
133+
134+
</section>
135+
136+
<!-- /.examples -->
137+
138+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139+
140+
<section class="related">
141+
142+
</section>
143+
144+
<!-- /.related -->
145+
146+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="links">
149+
150+
</section>
151+
152+
<!-- /.links -->

lib/node_modules/@stdlib/array/base/cuany-by-right/benchmark/benchmark.assign.length.js

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var bench = require( '@stdlib/bench' );
2424
var pow = require( '@stdlib/math/base/special/pow' );
2525
var isArray = require( '@stdlib/assert/is-array' );
26+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
2627
var filled = require( '@stdlib/array/base/filled' );
2728
var pkg = require( './../package.json' ).name;
2829
var cuanyByRight = require( './../lib' );
@@ -38,48 +39,38 @@ var cuanyByRight = require( './../lib' );
3839
* @returns {Function} benchmark function
3940
*/
4041
function createBenchmark( len ) {
41-
var x = filled( 1.5, len );
42-
return benchmark;
42+
var x = filled( 1.5, len );
43+
return benchmark;
4344

44-
/**
45-
* Benchmark function.
46-
*
47-
* @private
48-
* @param {Benchmark} b - benchmark instance
49-
*/
50-
function benchmark( b ) {
51-
var y;
52-
var v;
53-
var i;
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var y;
53+
var v;
54+
var i;
5455

55-
y = filled( false, len );
56+
y = filled( false, len );
5657

57-
b.tic();
58-
for ( i = 0; i < b.iterations; i++ ) {
59-
v = cuanyByRight.assign( x, y, 1, 0, isPositive );
60-
if ( typeof v !== 'object' ) {
61-
b.fail( 'should return an array' );
62-
}
63-
}
64-
b.toc();
65-
if ( !isArray( v ) ) {
66-
b.fail( 'should return an array' );
67-
}
68-
b.pass( 'benchmark finished' );
69-
b.end();
70-
}
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
v = cuanyByRight.assign( x, y, 1, 0, isPositiveInteger );
61+
if ( typeof v !== 'object' ) {
62+
b.fail( 'should return an array' );
63+
}
64+
}
65+
b.toc();
66+
if ( !isArray( v ) ) {
67+
b.fail( 'should return an array' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
7172
}
7273

73-
/**
74-
* Predicate function to test if a value is positive.
75-
*
76-
* @private
77-
* @param {number} value - input value
78-
* @returns {boolean} boolean indicating whether the value is positive
79-
*/
80-
function isPositive( value ) {
81-
return ( value > 0 );
82-
}
8374

8475
// MAIN //
8576

@@ -89,20 +80,20 @@ function isPositive( value ) {
8980
* @private
9081
*/
9182
function main() {
92-
var len;
93-
var min;
94-
var max;
95-
var f;
96-
var i;
83+
var len;
84+
var min;
85+
var max;
86+
var f;
87+
var i;
9788

98-
min = 1; // 10^min
99-
max = 6; // 10^max
89+
min = 1; // 10^min
90+
max = 6; // 10^max
10091

101-
for ( i = min; i <= max; i++ ) {
102-
len = pow( 10, i );
103-
f = createBenchmark( len );
104-
bench( pkg + ':assign:len=' + len, f );
105-
}
92+
for ( i = min; i <= max; i++ ) {
93+
len = pow( 10, i );
94+
f = createBenchmark( len );
95+
bench( pkg+':assign:len='+len, f );
96+
}
10697
}
10798

10899
main();

lib/node_modules/@stdlib/array/base/cuany-by-right/benchmark/benckmark.js renamed to lib/node_modules/@stdlib/array/base/cuany-by-right/benchmark/benchmark.js

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,31 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var isArray = require( '@stdlib/assert/is-array' );
25+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
2526
var pkg = require( './../package.json' ).name;
2627
var cuanyByRight = require( './../lib' );
2728

2829

2930
// MAIN //
3031

3132
bench( pkg, function benchmark( b ) {
32-
var x;
33-
var i;
34-
var v;
35-
36-
x = [ 0, 1, 0, 0, 0 ];
37-
38-
b.tic();
39-
for ( i = 0; i < b.iterations; i++ ) {
40-
v = cuanyByRight( x, isPositive );
41-
if ( typeof v !== 'object' ) {
42-
b.fail( 'should return an array' );
43-
}
44-
}
45-
b.toc();
46-
if ( !isArray( v ) ) {
47-
b.fail( 'should return an array' );
48-
}
49-
b.pass( 'benchmark finished' );
50-
b.end();
33+
var x;
34+
var i;
35+
var v;
36+
37+
x = [ 1, 1, 0, 0, 0 ];
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
v = cuanyByRight( x, isPositiveInteger );
42+
if ( typeof v !== 'object' ) {
43+
b.fail( 'should return an array' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isArray( v ) ) {
48+
b.fail( 'should return an array' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
5152
});
52-
53-
/**
54-
* Predicate function to test if a value is positive.
55-
*
56-
* @private
57-
* @param {number} value - input value
58-
* @returns {boolean} boolean indicating whether the value is positive
59-
*/
60-
function isPositive( value ) {
61-
return ( value > 0 );
62-
}

lib/node_modules/@stdlib/array/base/cuany-by-right/benchmark/benckmark.length.js renamed to lib/node_modules/@stdlib/array/base/cuany-by-right/benchmark/benchmark.length.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
var bench = require( '@stdlib/bench' );
2424
var pow = require( '@stdlib/math/base/special/pow' );
2525
var isArray = require( '@stdlib/assert/is-array' );
26+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
2627
var filled = require( '@stdlib/array/base/filled' );
2728
var pkg = require( './../package.json' ).name;
28-
// var cuanyByRight = require( './../lib' );
29-
import cuanyByRight from '@stdlib/array/base/cuany-by-right/lib/main';
29+
var cuanyByRight = require( './../lib' );
3030

3131

3232
// FUNCTIONS //
@@ -39,7 +39,7 @@ import cuanyByRight from '@stdlib/array/base/cuany-by-right/lib/main';
3939
* @returns {Function} benchmark function
4040
*/
4141
function createBenchmark( len ) {
42-
var x = filled( 1.5, len );
42+
var x = filled( 0, len );
4343
return benchmark;
4444

4545
/**
@@ -54,7 +54,7 @@ function createBenchmark( len ) {
5454

5555
b.tic();
5656
for ( i = 0; i < b.iterations; i++ ) {
57-
v = cuanyByRight( x, isPositive );
57+
v = cuanyByRight( x, isPositiveInteger );
5858
if ( typeof v !== 'object' ) {
5959
b.fail( 'should return an array' );
6060
}
@@ -68,16 +68,6 @@ function createBenchmark( len ) {
6868
}
6969
}
7070

71-
/**
72-
* Predicate function to test if a value is positive.
73-
*
74-
* @private
75-
* @param {number} value - input value
76-
* @returns {boolean} boolean indicating whether the value is positive
77-
*/
78-
function isPositive( value ) {
79-
return ( value > 0 );
80-
}
8171

8272
// MAIN //
8373

0 commit comments

Comments
 (0)