From 14a4a4492bb139025f6b209fdaeb13c664fd5a11 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 23 Feb 2022 21:13:29 +0000 Subject: [PATCH] add apply and layer, and missing div/mul methods --- strudel.mjs | 18 +++++++++++++++++- test/pattern.test.mjs | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/strudel.mjs b/strudel.mjs index fc219679..1df97ef8 100644 --- a/strudel.mjs +++ b/strudel.mjs @@ -404,6 +404,14 @@ class Pattern { return this._opleft(other, a => b => a - b) } + mul(other) { + return this._opleft(other, a => b => a * b) + } + + div(other) { + return this._opleft(other, a => b => a / b) + } + union(other) { return this._opleft(other, a => b => Object.assign({}, a, b)) } @@ -460,6 +468,14 @@ class Pattern { return this.outerBind(id) } + _apply(func) { + return func(this) + } + + layer(...funcs) { + return stack(...funcs.map(func => func(this))) + } + _patternify(func) { const pat = this const patterned = function (...args) { @@ -648,7 +664,7 @@ class Pattern { } // methods of Pattern that get callable factories -Pattern.prototype.patternified = ['fast', 'slow', 'early', 'late']; +Pattern.prototype.patternified = ['apply', 'fast', 'slow', 'early', 'late']; // methods that create patterns, which are added to patternified Pattern methods Pattern.prototype.factories = { pure, stack, slowcat, fastcat, cat, timeCat, sequence, polymeter, pm, polyrhythm, pr}; // the magic happens in Pattern constructor. Keeping this in prototype enables adding methods from the outside (e.g. see tonal.ts) diff --git a/test/pattern.test.mjs b/test/pattern.test.mjs index f6180a80..70e49974 100644 --- a/test/pattern.test.mjs +++ b/test/pattern.test.mjs @@ -329,4 +329,30 @@ describe('Pattern', function() { ) }) }) + describe("apply", () => { + it('Can apply a function', () => { + assert.deepStrictEqual( + sequence("a", "b")._apply(fast(2)).firstCycle, + sequence("a", "b").fast(2).firstCycle + ) + }), + it('Can apply a pattern of functions', () => { + assert.deepStrictEqual( + sequence("a", "b").apply(fast(2)).firstCycle, + sequence("a", "b").fast(2).firstCycle + ) + assert.deepStrictEqual( + sequence("a", "b").apply(fast(2),fast(3)).firstCycle, + sequence("a", "b").fast(2,3).firstCycle + ) + }) + }) + describe("layer", () => { + it('Can layer up multiple functions', () => { + assert.deepStrictEqual( + sequence(1,2,3).layer(fast(2), pat => pat.add(3,4)).firstCycle, + stack(sequence(1,2,3).fast(2), sequence(1,2,3).add(3,4)).firstCycle + ) + }) + }) })