Skip to content

Commit

Permalink
fix(transducers): update arg types for choices() & weightedRandom()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 17, 2018
1 parent bd22811 commit eb67426
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/transducers/src/func/weighted-random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { tuples } from "../iter/tuples";
* @param choices
* @param weights
*/
export function weightedRandom<T>(choices: T[], weights?: number[]) {
export function weightedRandom<T>(choices: ArrayLike<T> & Iterable<T>, weights?: ArrayLike<number> & Iterable<number>) {
const n = choices.length;
const opts = [...tuples(choices, weights || repeat(1))].sort((a, b) => b[1] - a[1]);
let total = 0, i, r, sum;
Expand Down
8 changes: 4 additions & 4 deletions packages/transducers/src/iter/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { weightedRandom } from "../func/weighted-random";
import { repeatedly } from "./repeatedly";

/**
* Returns an infinite iterator of random choices and their (optional) weights.
* If `weights` are given, it must be the same size as `choices`. If omitted,
* each choice will have same probability.
* Returns an infinite iterator of random choices and their (optional)
* weights. If `weights` is given, it must have at least the same size
* as `choices`. If omitted, each choice will have same probability.
*
* See: `weightedRandom()`
*
Expand All @@ -16,7 +16,7 @@ import { repeatedly } from "./repeatedly";
* @param choices
* @param weights
*/
export function choices<T>(choices: T[], weights?: number[]) {
export function choices<T>(choices: ArrayLike<T> & Iterable<T>, weights?: ArrayLike<number> & Iterable<number>) {
return repeatedly(
weights ?
weightedRandom(choices, weights) :
Expand Down

0 comments on commit eb67426

Please sign in to comment.