diff --git a/base/count-falsy/README.md b/base/count-falsy/README.md new file mode 100644 index 00000000..a831e9fc --- /dev/null +++ b/base/count-falsy/README.md @@ -0,0 +1,113 @@ + + +# countFalsy + +> Count the number of falsy elements in an array. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var countFalsy = require( '@stdlib/array/base/count-falsy' ); +``` + +#### countFalsy( x ) + +Counts the number of falsy elements in an array. + +```javascript +var x = [ 0, 1, 0, 1, 2 ]; + +var out = countFalsy( x ); +// returns 2 +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var countFalsy = require( '@stdlib/array/base/count-falsy' ); + +var x = bernoulli( 100, 0.5, { + 'dtype': 'generic' +}); +console.log( x ); + +var n = countFalsy( x ); +console.log( n ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/base/count-falsy/benchmark/benchmark.length.js b/base/count-falsy/benchmark/benchmark.length.js new file mode 100644 index 00000000..ef2ef5ce --- /dev/null +++ b/base/count-falsy/benchmark/benchmark.length.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var pkg = require( './../package.json' ).name; +var countFalsy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = bernoulli( len, 0.5, { + 'dtype': 'generic' + }); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = countFalsy( x ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( out ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( len ); + bench( pkg+':dtype=generic,len='+len, f ); + } +} + +main(); diff --git a/base/count-falsy/docs/repl.txt b/base/count-falsy/docs/repl.txt new file mode 100644 index 00000000..38e161ff --- /dev/null +++ b/base/count-falsy/docs/repl.txt @@ -0,0 +1,22 @@ + +{{alias}}( x ) + Counts the number of falsy values in an array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + Returns + ------- + out: integer + Number of falsy values. + + Examples + -------- + > var out = {{alias}}( [ 0, 1, 1 ] ) + 1 + + See Also + -------- + diff --git a/base/count-falsy/docs/types/index.d.ts b/base/count-falsy/docs/types/index.d.ts new file mode 100644 index 00000000..43b7c218 --- /dev/null +++ b/base/count-falsy/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection } from '@stdlib/types/array'; + +/** +* Counts the number of falsy values in an array. +* +* @param x - input array +* @returns number of falsy values +* +* @example +* var x = [ 0, 1, 0, 1, 1 ]; +* +* var out = countFalsy( x ); +* // returns 2 +*/ +declare function countFalsy( x: Collection ): number; + + +// EXPORTS // + +export = countFalsy; diff --git a/base/count-falsy/docs/types/test.ts b/base/count-falsy/docs/types/test.ts new file mode 100644 index 00000000..99f4f7ed --- /dev/null +++ b/base/count-falsy/docs/types/test.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import countFalsy = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + countFalsy( [ 1, 2, 3 ] ); // $ExpectType number +} + +// The compiler throws an error if the function is provided an argument which is not a collection... +{ + countFalsy( 5 ); // $ExpectError + countFalsy( true ); // $ExpectError + countFalsy( false ); // $ExpectError + countFalsy( null ); // $ExpectError + countFalsy( {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + countFalsy(); // $ExpectError + countFalsy( [ 1, 2, 3 ], 2 ); // $ExpectError +} diff --git a/base/count-falsy/examples/index.js b/base/count-falsy/examples/index.js new file mode 100644 index 00000000..d99a7fbb --- /dev/null +++ b/base/count-falsy/examples/index.js @@ -0,0 +1,30 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var countFalsy = require( './../lib' ); + +var x = bernoulli( 100, 0.5, { + 'dtype': 'generic' +}); +console.log( x ); + +var n = countFalsy( x ); +console.log( n ); diff --git a/base/count-falsy/lib/index.js b/base/count-falsy/lib/index.js new file mode 100644 index 00000000..f76288aa --- /dev/null +++ b/base/count-falsy/lib/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Count the number of falsy values in an array. +* +* @module @stdlib/array/base/count-falsy +* +* @example +* var countFalsy = require( '@stdlib/array/base/count-falsy' ); +* +* var x = [ 0, 1, 0, 1, 1 ]; +* +* var n = countFalsy( x ); +* // returns 2 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/count-falsy/lib/main.js b/base/count-falsy/lib/main.js new file mode 100644 index 00000000..d8f7e7e9 --- /dev/null +++ b/base/count-falsy/lib/main.js @@ -0,0 +1,147 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isComplexTypedArray = require( './../../../base/assert/is-complex-typed-array' ); +var isAccessorArray = require( './../../../base/assert/is-accessor-array' ); +var resolveGetter = require( './../../../base/resolve-getter' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); + + +// FUNCTIONS // + +/** +* Counts the number of falsy values in an indexed array. +* +* @private +* @param {Collection} x - input array +* @returns {NonNegativeInteger} number of falsy values +* +* @example +* var x = [ 0, 1, 0, 1, 1 ]; +* +* var n = indexed( x ); +* // returns 2 +*/ +function indexed( x ) { + var n; + var i; + + n = 0; + for ( i = 0; i < x.length; i++ ) { + if ( !x[ i ] ) { + n += 1; + } + } + return n; +} + +/** +* Counts the number of falsy values in an accessor array. +* +* @private +* @param {Collection} x - input array +* @returns {NonNegativeInteger} number of falsy values +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* +* var x = toAccessorArray( [ 0, 1, 0, 1, 1 ] ); +* +* var n = accessors( x ); +* // returns 2 +*/ +function accessors( x ) { + var get; + var n; + var i; + + get = resolveGetter( x ); + + n = 0; + for ( i = 0; i < x.length; i++ ) { + if ( !get( x, i ) ) { + n += 1; + } + } + return n; +} + +/** +* Counts the number of falsy values in a complex array. +* +* @private +* @param {Collection} x - input array +* @returns {NonNegativeInteger} number of falsy values +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* +* var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0, 5.0, 6.0 ] ); +* +* var n = complex( x ); +* // returns 2 +*/ +function complex( x ) { + var view; + var n; + var i; + + view = reinterpret( x, 0 ); + + n = 0; + for ( i = 0; i < view.length; i += 2 ) { + if ( view[ i ] === 0.0 && view[ i+1 ] === 0.0 ) { + n += 1; + } + } + return n; +} + + +// MAIN // + +/** +* Counts the number of falsy values in an array. +* +* @param {Collection} x - input array +* @returns {NonNegativeInteger} number of falsy values +* +* @example +* var x = [ 0, 1, 0, 1, 1 ]; +* +* var n = countFalsy( x ); +* // returns 2 +*/ +function countFalsy( x ) { + if ( isAccessorArray( x ) ) { + if ( isComplexTypedArray( x ) ) { + return complex( x ); + } + return accessors( x ); + } + return indexed( x ); +} + + +// EXPORTS // + +module.exports = countFalsy; diff --git a/base/count-falsy/package.json b/base/count-falsy/package.json new file mode 100644 index 00000000..92641bdf --- /dev/null +++ b/base/count-falsy/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/array/base/count-falsy", + "version": "0.0.0", + "description": "Count the number of falsy elements in an array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "array", + "count", + "sum", + "summation", + "countif", + "total", + "falsy" + ] +} diff --git a/base/count-falsy/test/test.js b/base/count-falsy/test/test.js new file mode 100644 index 00000000..02aed98f --- /dev/null +++ b/base/count-falsy/test/test.js @@ -0,0 +1,114 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Complex128Array = require( './../../../complex128' ); +var Int32Array = require( './../../../int32' ); +var toAccessorArray = require( './../../../base/to-accessor-array' ); +var countFalsy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof countFalsy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function counts the number of falsy values (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 0, 1, 0, 1, 2 ]; + expected = 2; + actual = countFalsy( x ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of falsy values (accessors)', function test( t ) { + var expected; + var actual; + var x; + + x = toAccessorArray( [ 0, 1, 0, 1, 2 ] ); + expected = 2; + actual = countFalsy( x ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of falsy values (real typed array)', function test( t ) { + var expected; + var actual; + var x; + + x = new Int32Array( [ 0, 1, 0, 1, 2 ] ); + expected = 2; + actual = countFalsy( x ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of falsy values (complex typed array)', function test( t ) { + var expected; + var actual; + var x; + + x = new Complex128Array( [ 0.0, 0.0, 1.0, 0.0, 3.0, 4.0, 0.0, 5.0 ] ); + expected = 1; + actual = countFalsy( x ); + + t.strictEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns zero if provided an array of length `0`', function test( t ) { + var expected; + var actual; + var x; + + expected = 0; + + x = []; + actual = countFalsy( x ); + t.strictEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray( [] ); + actual = countFalsy( x ); + t.strictEqual( actual, expected, 'returns expected value' ); + + x = new Int32Array( [] ); + actual = countFalsy( x ); + t.strictEqual( actual, expected, 'returns expected value' ); + + x = new Complex128Array( [] ); + actual = countFalsy( x ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/base/lib/index.js b/base/lib/index.js index 7c48aa8f..eedfdca9 100644 --- a/base/lib/index.js +++ b/base/lib/index.js @@ -423,6 +423,15 @@ setReadOnly( ns, 'copy', require( './../../base/copy' ) ); */ setReadOnly( ns, 'copyIndexed', require( './../../base/copy-indexed' ) ); +/** +* @name countFalsy +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/array/base/count-falsy} +*/ +setReadOnly( ns, 'countFalsy', require( './../../base/count-falsy' ) ); + /** * @name countTruthy * @memberof ns