Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
naming improvements; starting to break util functions out into pure m…
…odule; testing improvements
- Loading branch information
1 parent
7dd738f
commit 7a9ec47
Showing
5 changed files
with
124 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
import {test, describe} from 'ava-spec'; | ||
|
||
const jssm = require('../../build/jssm.es5.js'); | ||
|
||
|
||
|
||
|
||
|
||
describe('seq/1', async it => { | ||
|
||
it('(0) generates []', t => t.deepEqual([], jssm.seq(0) )); | ||
it('(1) generates [0]', t => t.deepEqual([0], jssm.seq(1) )); | ||
it('(2) generates [0,1]', t => t.deepEqual([0,1], jssm.seq(2) )); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
const rand_select = (options : Array<any>, probability_property : string = 'probability') => { | ||
|
||
if (!Array.isArray(options)) { throw new TypeError('options must be a non-empty array of objects'); } | ||
if (!(typeof options[0] === 'object')) { throw new TypeError('options must be a non-empty array of objects'); } | ||
|
||
const frand = cap => Math.random() * cap, | ||
prob_sum = options.reduce( (acc, val:any) => acc + val[probability_property], 0 ), | ||
rnd = frand(prob_sum); | ||
|
||
var cursor = 0, | ||
cursor_sum = 0; | ||
|
||
while ((cursor_sum += (options:any)[cursor++][probability_property]) <= rnd) { } | ||
return options[cursor-1]; | ||
|
||
}; | ||
|
||
|
||
|
||
|
||
|
||
const seq = (n : number) => | ||
|
||
(new Array(n)).fill(true).map( (_,i) => i ); | ||
|
||
|
||
|
||
|
||
|
||
const histograph = (a : Array<any>) => | ||
|
||
a.sort().reduce( (m,v) => ( m.set(v, (m.has(v)? m.get(v)+1 : 1)) , m), new Map() ); | ||
|
||
|
||
|
||
|
||
|
||
const sample_select = (n : number, options : Array<mixed>, probability_property : string) => | ||
|
||
seq(n).map(i => rand_select(options, probability_property)); | ||
|
||
|
||
|
||
|
||
|
||
const histo_key = (n : number, options : Array<mixed>, probability_property : string, extract : string) => | ||
|
||
histograph(sample_select(n, options, probability_property).map( (s:any) => s[extract])); | ||
|
||
|
||
|
||
|
||
|
||
export { seq, histograph, histo_key, rand_select, sample_select }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters