From 4b0eec633544d07c053c3880f1312adfcd13a64f Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Tue, 12 Feb 2019 15:37:42 +0000 Subject: [PATCH] refactor(transducers): remove obsolete randomID() & weightedRandom() BREAKING CHANGE: migrate randomID() & weightedRandom() to @thi.ng/random - update choices() iterator --- packages/transducers/src/func/random-id.ts | 25 ------------- .../transducers/src/func/weighted-random.ts | 36 ------------------- packages/transducers/src/iter/choices.ts | 8 ++--- 3 files changed, 4 insertions(+), 65 deletions(-) delete mode 100644 packages/transducers/src/func/random-id.ts delete mode 100644 packages/transducers/src/func/weighted-random.ts diff --git a/packages/transducers/src/func/random-id.ts b/packages/transducers/src/func/random-id.ts deleted file mode 100644 index 5979d8aed5..0000000000 --- a/packages/transducers/src/func/random-id.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { IRandom, SYSTEM } from "@thi.ng/random"; -import { choices } from "../iter/choices"; -import { take } from "../xform/take"; - -/** - * Generates and returns a random string of `len` characters (default - * 4), plus optional given `prefix` and using only provided `syms` - * characters (default lowercase a-z). - * - * ``` - * randomID() - * "qgdt" - * - * randomID(8, "id-", "0123456789ABCDEF") - * "id-94EF6E1A" - * ``` - * - * @param len - * @param prefix - * @param syms - * @param rnd - */ -export const randomID = - (len = 4, prefix = "", syms = "abcdefghijklmnopqrstuvwxyz", rnd: IRandom = SYSTEM) => - [prefix, ...take(len, choices(syms, null, rnd))].join(""); diff --git a/packages/transducers/src/func/weighted-random.ts b/packages/transducers/src/func/weighted-random.ts deleted file mode 100644 index 52c26deb4d..0000000000 --- a/packages/transducers/src/func/weighted-random.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { IRandom, SYSTEM } from "@thi.ng/random"; -import { repeat } from "../iter/repeat"; -import { zip } from "../iter/zip"; - -/** - * Returns a no-arg function which produces a random choice of given - * weighted `choices`. If `weights` are given, it must be the same size - * as `choices`. If omitted, each choice will have same probability. - * - * https://www.electricmonk.nl/log/2009/12/23/weighted-random-distribution/ - * - * @param choices - * @param weights - */ -export const weightedRandom = ( - choices: ArrayLike & Iterable, - weights?: ArrayLike & Iterable, - rnd: IRandom = SYSTEM -) => { - const n = choices.length; - const opts = [...zip(choices, weights || repeat(1))].sort((a, b) => b[1] - a[1]); - let total = 0, i, r, sum; - for (i = 0; i < n; i++) { - total += opts[i][1]; - } - return () => { - r = rnd.float(total); - sum = total; - for (i = 0; i < n; i++) { - sum -= opts[i][1]; - if (sum <= r) { - return opts[i][0]; - } - } - }; -}; diff --git a/packages/transducers/src/iter/choices.ts b/packages/transducers/src/iter/choices.ts index 73858bdc26..7b21adae5e 100644 --- a/packages/transducers/src/iter/choices.ts +++ b/packages/transducers/src/iter/choices.ts @@ -1,6 +1,6 @@ -import { weightedRandom } from "../func/weighted-random"; +import { ensureArray } from "@thi.ng/arrays"; +import { IRandom, SYSTEM, weightedRandom } from "@thi.ng/random"; import { repeatedly } from "./repeatedly"; -import { IRandom, SYSTEM } from "@thi.ng/random"; /** * Returns an infinite iterator of random choices and their (optional) @@ -19,11 +19,11 @@ import { IRandom, SYSTEM } from "@thi.ng/random"; */ export const choices = ( choices: ArrayLike & Iterable, - weights?: ArrayLike & Iterable, + weights?: ArrayLike, rnd: IRandom = SYSTEM ) => repeatedly( weights ? - weightedRandom(choices, weights, rnd) : + weightedRandom(ensureArray(choices), weights, rnd) : () => choices[(rnd.float(choices.length) | 0)] );