Skip to content

Commit 7f68def

Browse files
committed
Add utility to test if all elements in a collection are falsy
1 parent d383f50 commit 7f68def

File tree

10 files changed

+614
-0
lines changed

10 files changed

+614
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# none
2+
3+
> Test whether all elements in a collection are falsy.
4+
5+
6+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
7+
8+
<section class="intro">
9+
10+
</section>
11+
12+
<!-- /.intro -->
13+
14+
<!-- Package usage documentation. -->
15+
16+
<section class="usage">
17+
18+
## Usage
19+
20+
``` javascript
21+
var none = require( '@stdlib/utils/none' );
22+
```
23+
24+
#### none( collection )
25+
26+
Tests whether all elements in a `collection` are falsy.
27+
28+
``` javascript
29+
var arr = [ 0, 0, 0, 0, 0 ];
30+
31+
var bool = none( arr );
32+
// returns true
33+
```
34+
35+
If provided an empty `collection`, the function returns `true`.
36+
37+
``` javascript
38+
var bool = none( [] );
39+
// returns true
40+
```
41+
42+
</section>
43+
44+
<!-- /.usage -->
45+
46+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
47+
48+
<section class="notes">
49+
50+
## Notes
51+
52+
* A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`).
53+
* The function does __not__ skip `undefined` elements and is thus not optimized for sparse collections.
54+
55+
</section>
56+
57+
<!-- /.notes -->
58+
59+
<!-- Package usage examples. -->
60+
61+
<section class="examples">
62+
63+
## Examples
64+
65+
``` javascript
66+
var randu = require( '@stdlib/math/base/random/randu' );
67+
var none = require( '@stdlib/utils/none' );
68+
69+
var bool;
70+
var arr;
71+
var i;
72+
73+
arr = new Array( 100 );
74+
for ( i = 0; i < arr.length; i++ ) {
75+
arr[ i ] = ( randu() > 0.95 );
76+
}
77+
78+
bool = none( arr );
79+
// returns <boolean>
80+
```
81+
82+
</section>
83+
84+
<!-- /.examples -->
85+
86+
<!-- 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. -->
87+
88+
<section class="references">
89+
90+
</section>
91+
92+
<!-- /.references -->
93+
94+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
95+
96+
<section class="links">
97+
98+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
99+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
100+
[mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
101+
102+
</section>
103+
104+
<!-- /.links -->
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
7+
var pkg = require( './../package.json' ).name;
8+
var none = require( './../lib' );
9+
10+
11+
// MAIN //
12+
13+
bench( pkg, function benchmark( b ) {
14+
var bool;
15+
var arr;
16+
var i;
17+
18+
b.tic();
19+
for ( i = 0; i < b.iterations; i++ ) {
20+
arr = [ 0, 0, 0, 0, 0, i+1 ];
21+
bool = none( arr );
22+
if ( !isBoolean( bool ) ) {
23+
b.fail( 'should return a boolean' );
24+
}
25+
}
26+
b.toc();
27+
if ( !isBoolean( bool ) ) {
28+
b.fail( 'should return a boolean' );
29+
}
30+
b.pass( 'benchmark finished' );
31+
b.end();
32+
});
33+
34+
bench( pkg+'::built-in', function benchmark( b ) {
35+
var bool;
36+
var arr;
37+
var i;
38+
39+
function predicate( v ) {
40+
return ( v === 0 );
41+
}
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
arr = [ 0, 0, 0, 0, 0, i+1 ];
46+
bool = arr.every( predicate );
47+
if ( !isBoolean( bool ) ) {
48+
b.fail( 'should return a boolean' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isBoolean( bool ) ) {
53+
b.fail( 'should return a boolean' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( pkg+'::loop', function benchmark( b ) {
60+
var bool;
61+
var arr;
62+
var i;
63+
var j;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
arr = [ 0, 0, 0, 0, 0, i+1 ];
68+
bool = true;
69+
for ( j = 0; j < arr.length; j++ ) {
70+
if ( arr[ j ] !== 0 ) {
71+
bool = false;
72+
break;
73+
}
74+
}
75+
if ( !isBoolean( bool ) ) {
76+
b.fail( 'should be a boolean' );
77+
}
78+
}
79+
b.toc();
80+
if ( !isBoolean( bool ) ) {
81+
b.fail( 'should be a boolean' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
julia 0.5
2+
BenchmarkTools 0.0.8
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/usr/bin/env julia
2+
3+
import BenchmarkTools
4+
5+
# Benchmark variables:
6+
name = "none";
7+
repeats = 3;
8+
9+
"""
10+
print_version()
11+
12+
Prints the TAP version.
13+
14+
# Examples
15+
16+
``` julia
17+
julia> print_version()
18+
```
19+
"""
20+
function print_version()
21+
@printf( "TAP version 13\n" );
22+
end
23+
24+
"""
25+
print_summary( total, passing )
26+
27+
Print the benchmark summary.
28+
29+
# Arguments
30+
31+
* `total`: total number of tests
32+
* `passing`: number of passing tests
33+
34+
# Examples
35+
36+
``` julia
37+
julia> print_summary( 3, 3 )
38+
```
39+
"""
40+
function print_summary( total, passing )
41+
@printf( "#\n" );
42+
@printf( "1..%d\n", total ); # TAP plan
43+
@printf( "# total %d\n", total );
44+
@printf( "# pass %d\n", passing );
45+
@printf( "#\n" );
46+
@printf( "# ok\n" );
47+
end
48+
49+
"""
50+
print_results( iterations, elapsed )
51+
52+
Print benchmark results.
53+
54+
# Arguments
55+
56+
* `iterations`: number of iterations
57+
* `elapsed`: elapsed time (in seconds)
58+
59+
# Examples
60+
61+
``` julia
62+
julia> print_results( 1000000, 0.131009101868 )
63+
```
64+
"""
65+
function print_results( iterations, elapsed )
66+
rate = iterations / elapsed
67+
68+
@printf( " ---\n" );
69+
@printf( " iterations: %d\n", iterations );
70+
@printf( " elapsed: %0.9f\n", elapsed );
71+
@printf( " rate: %0.9f\n", rate );
72+
@printf( " ...\n" );
73+
end
74+
75+
"""
76+
benchmark()
77+
78+
Run a benchmark.
79+
80+
# Notes
81+
82+
* Benchmark results are returned as a two-element array: [ iterations, elapsed ].
83+
* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation.
84+
* The elapsed time is in seconds.
85+
86+
# Examples
87+
88+
``` julia
89+
julia> benchmark();
90+
```
91+
"""
92+
function benchmark()
93+
# We benchmark `all` as a proxy for `none`:
94+
t = BenchmarkTools.@benchmark all( [ true, true, true, true, true, true ] ) samples=1e6
95+
96+
# Compute the total "elapsed" time and convert from nanoseconds to seconds:
97+
s = sum( t.times ) / 1.0e9;
98+
99+
# Determine the number of "iterations":
100+
iter = length( t.times );
101+
102+
# Return the results:
103+
[ iter, s ];
104+
end
105+
106+
"""
107+
main()
108+
109+
Run benchmarks.
110+
111+
# Examples
112+
113+
``` julia
114+
julia> main();
115+
```
116+
"""
117+
function main()
118+
print_version();
119+
for i in 1:3
120+
@printf( "# julia::%s\n", name );
121+
results = benchmark();
122+
print_results( results[ 1 ], results[ 2 ] );
123+
@printf( "ok %d benchmark finished\n", i );
124+
end
125+
print_summary( repeats, repeats );
126+
end
127+
128+
main();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( collection )
3+
Tests whether all elements in a collection are falsy.
4+
5+
The function immediately returns upon encountering a truthy value.
6+
7+
If provided an empty collection, the function returns `true`.
8+
9+
Parameters
10+
----------
11+
collection: Array|TypedArray|Object
12+
Input collection over which to iterate. If provided an object, the
13+
object must be array-like (excluding strings and functions).
14+
15+
Returns
16+
-------
17+
bool: boolean
18+
The function returns `true` if all elements are falsy; otherwise, the
19+
function returns `false`.
20+
21+
Examples
22+
--------
23+
> var arr = [ 0, 0, 0, 0, 0 ];
24+
> var bool = {{alias}}( arr )
25+
true
26+
27+
See Also
28+
--------
29+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var randu = require( '@stdlib/math/base/random/randu' );
4+
var none = require( './../lib' );
5+
6+
var bool;
7+
var arr;
8+
var i;
9+
10+
arr = new Array( 100 );
11+
for ( i = 0; i < arr.length; i++ ) {
12+
arr[ i ] = ( randu() > 0.95 );
13+
}
14+
15+
bool = none( arr );
16+
console.log( bool );
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
/**
4+
* Test whether all elements in a collection are falsy.
5+
*
6+
* @module @stdlib/utils/none
7+
*
8+
* @example
9+
* var none = require( '@stdlib/utils/none' );
10+
*
11+
* var arr = [ 0, 0, 0, 0, 0 ];
12+
*
13+
* var bool = none( arr );
14+
* // returns true
15+
*/
16+
17+
// MODULES //
18+
19+
var none = require( './none.js' );
20+
21+
22+
// EXPORTS //
23+
24+
module.exports = none;

0 commit comments

Comments
 (0)