Skip to content

Commit

Permalink
added Util.sum()
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Oct 5, 2021
1 parent 67218de commit a1d811e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs/utility-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Util = require('total-serialism').Utility;
- subtract
- multiply
- divide
- sum
- minimum
- maximum
- normalize
Expand Down Expand Up @@ -65,6 +66,18 @@ Util.divide([1, 2, 3, 4], [1, 2, 3]);
//=> [ 1, 1, 1, 4 ]
```

## sum

Return the sum of all values in an array. Ignores all non-numeric values.

```js
Util.sum([1, 2, 3, 4]);
//=> 10

Util.sum([10, 'foo', 11, 'bar', 22]);
//=> 43
```

## minimum

Return the minimum value from an array (Also part of `.Statistic`)
Expand Down
28 changes: 28 additions & 0 deletions src/gen-stochastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,34 @@ function dice(len=1, sides=6){
}
exports.dice = dice;

// Generate random clave patterns. Outputs a binary list as rhythm,
// where 1's represent onsets and 0's represent rests.
//
// @param {Int} -> output length of rhythm
// @param {Int} -> rhythmic gaps to choose from (1, 2, 3 etc)
// @param {Float} -> density distribution between -1 and 1.
//
function clave(len=1, max=3, min=2, dist=0){
let arr = [];
len = Math.max(1, len);
// swap if lo > hi
if (min > max){ var t=min, min=max; max=t; }
min = Math.max(1, min);
max = Math.max(min, max);

console.log(len, min, max);

let sum = 0;
let rtm = [];
while (sum < len){
let r = Math.floor(rng() * (max + 1 - min)) + min;
rtm.push(r);
sum += r;
}
console.log(rtm, Util.sum(rtm));
}
exports.clave = clave;

// shuffle a list, based on the Fisher-Yates shuffle algorithm
// by Ronald Fisher and Frank Yates in 1938
// The algorithm has run time complexity of O(n)
Expand Down
15 changes: 15 additions & 0 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ exports.truncate = truncate;
exports.trunc = truncate;
exports.int = truncate;

// Return the sum of all values in the array
// Ignore all non numeric values
//
// @param {Array} -> input array
// @return {Number} -> summed array
function sum(a=[0]){
a = Array.isArray(a)? a : [a];
let s = 0;
a.forEach((v) => {
s += isNaN(v)? 0 : v;
});
return s;
}
exports.sum = sum;

// Return the biggest value from an array
//
// @param {NumberArray} -> input array
Expand Down
8 changes: 6 additions & 2 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ console.log();
// markov.clear();
// console.log(markov.table);

// console.log(Algo.pisano(40));
// fullTest();
testUtil();

fullTest();
// Rand.clave(8);

function fullTest(){
console.time('Total Time');
Expand Down Expand Up @@ -553,6 +554,9 @@ function testUtil(){
test("Util.normalize([1, 2, 3, 4])");
test("Util.normalize([5, 12, 4, 17, 3])");

test("Util.sum([1, 2, 3, 4])");
test("Util.sum([10, 'foo', 11, 'bar', 22])");

let drawing = [];
Rand.seed(628);
for (let i=0; i<10; i++){
Expand Down

0 comments on commit a1d811e

Please sign in to comment.