diff options
| author | Terry Truong <terry06890@gmail.com> | 2022-03-29 11:39:33 +1100 |
|---|---|---|
| committer | Terry Truong <terry06890@gmail.com> | 2022-03-29 13:30:50 +1100 |
| commit | a68a55205ed189250693368af7028031a70631d9 (patch) | |
| tree | 4d780d1b450a2a3a51866103e80c53a818a8cb23 /src/util.ts | |
| parent | 304274b1380f955b6e1913428f6dbbc9efcf0bcf (diff) | |
Update comments, adjust names, do minor refactors
Diffstat (limited to 'src/util.ts')
| -rw-r--r-- | src/util.ts | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/util.ts b/src/util.ts index 79c2b5c..be31102 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,17 +1,18 @@ /* - * Contains commonly-used utility functions. + * Contains utility functions. */ // Returns [0 ... len] -export function range(len: number){ +export function range(len: number): number[] { return [...Array(len).keys()]; } // Returns sum of array values -export function arraySum(array: number[]){ +export function arraySum(array: number[]): number { return array.reduce((x,y) => x+y); } -// Returns array copy with vals clipped to within [min,max], redistributing to compensate (returns null on failure) -export function limitVals(arr: number[], min: number, max: number){ +// Returns array copy with vals clipped to within [min,max], redistributing to compensate +// Returns null on failure +export function limitVals(arr: number[], min: number, max: number): number[] | null { let vals = [...arr]; let clipped = new Array(vals.length).fill(false); let owedChg = 0; // Stores total change made after clipping values @@ -48,8 +49,9 @@ export function limitVals(arr: number[], min: number, max: number){ } } // Usable to iterate through possible int arrays with ascending values in the range 0 to maxLen-1, starting with [0] - // eg: With maxLen 3, updates [0] to [0,1], then to [0,2], then [0,1,2], then null -export function updateAscSeq(seq: number[], maxLen: number){ + // eg: With maxLen 3, updates [0] to [0,1], then to [0,2], then [0,1,2] +// Returns false when there is no next array +export function updateAscSeq(seq: number[], maxLen: number): boolean { // Try increasing last element, then preceding elements, then extending the array let i = seq.length - 1; while (true){ |
