diff --git a/packages/transducers/src/index.ts b/packages/transducers/src/index.ts index f4d6e8f2bb..31811819b5 100644 --- a/packages/transducers/src/index.ts +++ b/packages/transducers/src/index.ts @@ -111,6 +111,7 @@ export * from "./iter/concat"; export * from "./iter/cycle"; export * from "./iter/iterate"; export * from "./iter/keys"; +export * from "./iter/norm-range"; export * from "./iter/pairs"; export * from "./iter/permutations"; export * from "./iter/range"; diff --git a/packages/transducers/src/iter/norm-range.ts b/packages/transducers/src/iter/norm-range.ts new file mode 100644 index 0000000000..c4433c0f97 --- /dev/null +++ b/packages/transducers/src/iter/norm-range.ts @@ -0,0 +1,18 @@ +/** + * Yields sequence of `n+1` monotonically increasing numbers in the + * closed interval (0.0 .. 1.0). If `n <= 0`, yields nothing. + * + * ``` + * [...normRange(4)] + * // [0, 0.25, 0.5, 0.75, 1.0] + * ``` + * + * @param n number of steps + */ +export function* normRange(n: number) { + if (n > 0) { + for (let i = 0; i <= n; i++) { + yield i / n; + } + } +}