Skip to content

compute-io/iseven

Repository files navigation

iseven

NPM version Build Status Coverage Status Dependencies

Computes element-wise whether a value is an even number.

Installation

$ npm install compute-iseven

For use in the browser, use browserify.

Usage

var isEven = require( 'compute-iseven' );

iseven( x[, options] )

Checks element-wise whether numbers in x are even. x may be either a number, an array, a typed array, or a matrix. Correspondingly, the function returns either a single number, an array with length equal to that of the input array or a matrix with equal dimensions as input x. Each output element is either 0 or 1. A value of 1 means that an element is an even number and 0 means that an element is not an even number.

var out = isEven( 9 );
// returns 0

out = isEven( [ 1, 2, 3 ] );
// returns [ 0, 1, 1 ]

When provided an input array, the function accepts two options:

  • copy: boolean indicating whether to return a new array containing 0/1's indicating whether the corresponding element is an even number. Default: true.
  • accessor: accessor function for accessing numeric values in object arrays.

To mutate the input array (e.g., when input values can be discarded or when optimizing memory usage), set the copy option to false.

var arr = [ 1, 2, 3 ];

var out = isEven( arr, {
	'copy': false
});
// returns [ 0, 1, 0 ]

console.log( arr === out );
// returns true

For object arrays, provide an accessor function for accessing array values.

var data = [
	['beep', 1],
	['boop', 2],
	['bip', 3],
	['bap', 4],
	['baz', 5]
];

function getValue( d, i ) {
	return d[ 1 ];
}

var out = isEven( data, {
	'accessor': getValue
});
// returns [ 0, 1, 0, 1, 0 ]

In the case of matrices, the function returns an indicator matrix with the same dimensions as x:

var matrix = require( 'dstructs-matrix' ),
	data,
	mat,
	out;

data = new Float64Array( 9 );
for ( i = 0; i < 9; i++ ) {
	data[ i ] = i + 1;
}
mat = matrix( data, [3,3], 'float64' );
/*
	[ 1 2 3
	  4 5 6
	  7 8 9 ]
*/

out = isEven( mat );
/*
	[ 0 1 0
	  1 0 1
	  0 1 0 ]
*/

Examples

var isEven = require( 'compute-iseven' );

console.log( isEven( 3 ) );
// returns 0

console.log( isEven( [ 1, 2, 3, 4] ) );
// returns [ 0, 1, 0, 1 ]

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2015. The Compute.io Authors.

About

Computes an element-wise is-even check.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published