-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
52 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`sample picks random value: sample([1,2,3]) 1`] = `1`; | ||
|
||
exports[`sampleSize picks specified size: sampleSize([1,2,3], 2) 1`] = ` | ||
[ | ||
1, | ||
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,23 @@ | ||
import { sample, sampleSize } from './sample'; | ||
|
||
function fakeRng() { | ||
return 0.3; | ||
} | ||
|
||
describe('sample', () => { | ||
it('picks random value', () => { | ||
expect(sample([1, 2, 3], fakeRng)).toMatchSnapshot('sample([1,2,3])'); | ||
}); | ||
it('returns undefined on empty array', () => { | ||
expect(sample([])).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('sampleSize', () => { | ||
it('picks specified size', () => { | ||
expect(sampleSize([1, 2, 3], 2, fakeRng)).toMatchSnapshot('sampleSize([1,2,3], 2)'); | ||
}); | ||
it('returns undefined on empty array', () => { | ||
expect(sampleSize([2, 3], 3, fakeRng)).toHaveLength(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,19 @@ | ||
import { defaultRng } from '../random/number'; | ||
|
||
export function sample<T>(input: readonly T[], rng = defaultRng): T { | ||
const index = Math.floor(input.length * rng()); | ||
return input[index]; | ||
} | ||
|
||
export function sampleSize<T>(from: T[], count: number, rng = defaultRng): T[] { | ||
const reservoir: T[] = from.slice(0, Math.min(count, from.length)); | ||
|
||
for (let i = count; i < from.length; i++) { | ||
const j = Math.floor(1 + Math.random() * i); | ||
if (j < count) { | ||
reservoir[j] = from[i]; | ||
} | ||
} | ||
|
||
return reservoir; | ||
} |