Skip to content

Commit

Permalink
feat(transducers): add line(), curve()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 13, 2020
1 parent 315cbf8 commit 31bd5b9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/transducers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -955,11 +955,13 @@ tx.transduce(tx.map((x) => x*10), tx.push(), tx.range(4))

- [choices](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/choices.ts)
- [concat](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/concat.ts)
- [curve](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/curve.ts)
- [cycle](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/cycle.ts)
- [dup](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/dup.ts)
- [extendSides](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/extend-sides.ts)
- [iterate](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/iterate.ts)
- [keys](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/keys.ts)
- [line](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/line.ts)
- [normRange](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/norm-range.ts)
- [padSides](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/pad-sides.ts)
- [pairs](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/pairs.ts)
Expand Down
2 changes: 2 additions & 0 deletions packages/transducers/README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,13 @@ tx.transduce(tx.map((x) => x*10), tx.push(), tx.range(4))

- [choices](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/choices.ts)
- [concat](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/concat.ts)
- [curve](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/curve.ts)
- [cycle](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/cycle.ts)
- [dup](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/dup.ts)
- [extendSides](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/extend-sides.ts)
- [iterate](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/iterate.ts)
- [keys](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/keys.ts)
- [line](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/line.ts)
- [normRange](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/norm-range.ts)
- [padSides](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/pad-sides.ts)
- [pairs](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/iter/pairs.ts)
Expand Down
2 changes: 2 additions & 0 deletions packages/transducers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ export * from "./func/renamer";
export * from "./iter/as-iterable";
export * from "./iter/choices";
export * from "./iter/concat";
export * from "./iter/curve";
export * from "./iter/cycle";
export * from "./iter/dup";
export * from "./iter/extend-sides";
export * from "./iter/iterate";
export * from "./iter/keys";
export * from "./iter/line";
export * from "./iter/norm-range";
export * from "./iter/pad-sides";
export * from "./iter/palindrome";
Expand Down
40 changes: 40 additions & 0 deletions packages/transducers/src/iter/curve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expFactor, fit } from "@thi.ng/math";

/**
* Iterator yielding `steps` + 1 interpolated values along an
* exponential curve in the closed `[start .. end]` interval. The
* curvature can be controlled via `falloff` (default: 1), which is used
* as a power of 10 to compute the exponential decay / growth factor.
* Use `falloff` < 0 for ease in or > 0 for ease out.
*
* @example
* ```ts
* [...curve(50, 100, 10, 2)]
* // [
* // 50,
* // 70.228,
* // 82.354,
* // 89.624,
* // 93.982,
* // 96.594,
* // 98.160,
* // 99.099,
* // 99.662,
* // 100
* // ]
* ```
*
* @param start -
* @param end -
* @param steps -
* @param falloff -
*/
export function* curve(start: number, end: number, steps = 10, falloff = 1) {
const fe = 1 / 10 ** falloff;
const f = expFactor(1, fe, steps - 1);
let t = 1;
for (; --steps >= 0; ) {
yield fit(t, 1, fe, start, end);
t *= f;
}
}
30 changes: 30 additions & 0 deletions packages/transducers/src/iter/line.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { map } from "../xform/map";
import { normRange } from "./norm-range";

/**
* Iterator yielding `steps` + 1 interpolate values on a line in the
* closed `[start .. end]` interval.
*
* @remarks
* This is similar to {@link range}, but potentially provides more
* precise values (by avoiding the accumulation of floating point errors
* during iteration).
*
* @example
* ```ts
* [...line(50, 100, 10)]
* // [
* // 50, 55, 60, 65, 70,
* // 75, 80, 85, 90, 95,
* // 100
* // ]
* ```
*
* @param start -
* @param end -
* @param steps -
*/
export const line = (start: number, end: number, steps = 10) => {
const delta = end - start;
return map((t) => start + delta * t, normRange(steps));
};

0 comments on commit 31bd5b9

Please sign in to comment.