Skip to content

Commit

Permalink
Chop chop (#1078)
Browse files Browse the repository at this point in the history
* failing test

* support chopping chops
  • Loading branch information
yaxu committed Jun 24, 2024
1 parent 283a071 commit a4bd0ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/core/pattern.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2804,8 +2804,16 @@ export const s_tour = function (pat, ...many) {
export const chop = register('chop', function (n, pat) {
const slices = Array.from({ length: n }, (x, i) => i);
const slice_objects = slices.map((i) => ({ begin: i / n, end: (i + 1) / n }));
const merge = function (a, b) {
if ('begin' in a && 'end' in a && a.begin !== undefined && a.end !== undefined) {
const d = a.end - a.begin;
b = { begin: a.begin + b.begin * d, end: a.begin + b.end * d };
}
// return a;
return Object.assign({}, a, b);
};
const func = function (o) {
return sequence(slice_objects.map((slice_o) => Object.assign({}, o, slice_o)));
return sequence(slice_objects.map((slice_o) => merge(o, slice_o)));
};
return pat.squeezeBind(func).setTactus(__tactus ? Fraction(n).mulmaybe(pat.tactus) : undefined);
});
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/pattern.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,9 @@ describe('Pattern', () => {
.firstCycle(),
);
});
it('Can chop chops', () => {
expect(pure({ s: 'bev' }).chop(2).chop(2).firstCycle()).toStrictEqual(pure({ s: 'bev' }).chop(4).firstCycle());
});
});
describe('range', () => {
it('Can be patterned', () => {
Expand Down

0 comments on commit a4bd0ae

Please sign in to comment.