Skip to content

Commit

Permalink
refactor(ramp): dedupe samples() impls
Browse files Browse the repository at this point in the history
- extract common impl as internal helper
- update Group.samples() & Ramp.samples()
  • Loading branch information
postspectacular committed Jun 19, 2024
1 parent f1f986b commit 85515e1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
14 changes: 2 additions & 12 deletions packages/ramp/src/group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { mix } from "@thi.ng/math/mix";
import { map } from "@thi.ng/transducers/map";
import { normRange } from "@thi.ng/transducers/norm-range";
import type {
Frame,
IRamp,
Expand All @@ -10,6 +7,7 @@ import type {
RampOpts,
} from "./api.js";
import { unconstrained } from "./domain.js";
import { __samples } from "./utils.js";

export type GroupImpl<T extends Record<string, any>> = {
[P in keyof T]: IRamp<T[P]>;
Expand Down Expand Up @@ -77,15 +75,7 @@ export class Group<T extends Record<string, any>> implements IReadonlyRamp<T> {
}

samples(n = 100, start?: number, end?: number): Iterable<Frame<T>> {
if (start == undefined || end == undefined) {
const bounds = this.timeBounds();
start = start ?? bounds[0];
end = end ?? bounds[1];
}
return map((t) => {
t = mix(start!, end!, t);
return <Frame<T>>[t, this.at(t)];
}, normRange(n));
return __samples(this, n, start, end);
}

bounds(): RampBounds<T> {
Expand Down
16 changes: 3 additions & 13 deletions packages/ramp/src/ramp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { compareNumAsc } from "@thi.ng/compare/numeric";
import { assert } from "@thi.ng/errors/assert";
import { absDiff } from "@thi.ng/math/abs";
import { clamp } from "@thi.ng/math/interval";
import { mix } from "@thi.ng/math/mix";
import { map } from "@thi.ng/transducers/map";
import { normRange } from "@thi.ng/transducers/norm-range";
import type {
Frame,
IRamp,
Expand All @@ -17,6 +14,7 @@ import type {
RampOpts,
} from "./api.js";
import { unconstrained } from "./domain.js";
import { __samples } from "./utils.js";

/**
* Syntax sugar for {@link Ramp} constructor using the given ramp interpolation
Expand Down Expand Up @@ -76,16 +74,8 @@ export class Ramp<T> implements ICopy<IRamp<T>>, IEmpty<IRamp<T>>, IRamp<T> {
return i < 0 ? first[1] : i >= n ? last[1] : impl.at(stops, i, t);
}

samples(n = 100, start?: number, end?: number) {
if (start == undefined || end == undefined) {
const bounds = this.timeBounds();
start = start ?? bounds[0];
end = end ?? bounds[1];
}
return map((t) => {
t = mix(start!, end!, t);
return <Frame<T>>[t, this.at(t)];
}, normRange(n));
samples(n = 100, start?: number, end?: number): Iterable<Frame<T>> {
return __samples(this, n, start, end);
}

bounds(): RampBounds<T> {
Expand Down
22 changes: 22 additions & 0 deletions packages/ramp/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// thing:no-export
import { mix } from "@thi.ng/math/mix";
import { map } from "@thi.ng/transducers/map";
import { normRange } from "@thi.ng/transducers/norm-range";
import type { Frame, IReadonlyRamp } from "./api.js";

export const __samples = <T>(
ramp: IReadonlyRamp<T>,
n: number,
start?: number,
end?: number
) => {
if (start === undefined || end === undefined) {
const bounds = ramp.timeBounds();
start = start ?? bounds[0];
end = end ?? bounds[1];
}
return map((t) => {
t = mix(start!, end!, t);
return <Frame<T>>[t, ramp.at(t)];
}, normRange(n));
};

0 comments on commit 85515e1

Please sign in to comment.