Cumulative Product
Computes the cumulative product of an array.
Installation
$ npm install compute-cprod
For use in the browser, use browserify.
Usage
var cprod = ;
cprod( arr[, options] )
Computes the cumulative product of an array
. For primitive arrays
,
var data = 1 2 3 4 ;var arr = ;// returns [ 1, 2, 6, 24 ]
The function accepts two options
:
- copy:
boolean
indicating whether to return a newarray
containing the cumulative products. Default:true
. - accessor: accessor
function
for accessing numeric values in objectarrays
.
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 data = 1 2 3 4 ;var arr =;// returns [ 1, 2, 6, 24 ]console;// returns true
For object arrays
, provide an accessor function
for accessing numeric array
values.
var data ='beep' 1'boop' 2'bap' 3'baz' 4;{return d 1 ;}var arr =;// returns [ 1, 2, 6, 24 ]
Note: the function returns an array
with a length equal to the original input array
.
Examples
var cprod = ;var data = 10 ;for var i = 0; i < datalength; i++data i = Math + 1;console;// returns [...]
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
Copyright
Copyright © 2015. Philipp Burckhardt.