From 86e8ed2ac0bd904ff0fde6a4c376aed1949aece5 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 29 Apr 2022 16:46:17 +0100 Subject: [PATCH] Trying a different naming scheme for composers --- packages/core/pattern.mjs | 41 ++++++++++++++++++++++++----- packages/core/test/pattern.test.mjs | 13 ++++----- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index 8a96874c..d6eca137 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -253,20 +253,20 @@ export class Pattern { ); } - _op(other, func) { + _opIn(other, func) { return this.fmap(func).appLeft(reify(other)); } - _opFlip(other, func) { + _opOut(other, func) { return this.fmap(func).appRight(reify(other)); } - _opSect(other, func) { + _opMix(other, func) { return this.fmap(func).appBoth(reify(other)); } _opSqueeze(other, func) { const otherPat = reify(other); return this.fmap((a) => otherPat.fmap((b) => func(a)(b)))._squeezeJoin(); } - _opSqueezeFlip(other, func) { + _opSqueezeOut(other, func) { const thisPat = this; const otherPat = reify(other); return otherPat.fmap((a) => thisPat.fmap((b) => func(b)(a)))._squeezeJoin(); @@ -800,16 +800,43 @@ const composers = { mul: (a, b) => a * b, div: (a, b) => a / b, mod: mod, + pow: Math.pow, + lt: (a, b) => a < b, + gt: (a, b) => a > b, + lte: (a, b) => a <= b, + gte: (a, b) => a >= b, + eq: (a, b) => a == b, + eqt: (a, b) => a === b, + ne: (a, b) => a != b, + net: (a, b) => a !== b, + and: (a, b) => a && b, + or: (a, b) => a || b, + _and: (a, b) => a & b, + _or: (a, b) => a | b, + _xor: (a, b) => a ^ b, + _lshift: (a, b) => a << b, + _rshift: (a, b) => a >> b, func: (a, b) => b(a), }; for (const [name, op] of Object.entries(composers)) { - for (const opType of ['', 'Flip', 'Sect', 'Squeeze', 'SqueezeFlip', 'Reset', 'Restart']) { + for (const opType of ['In', 'Out', 'Mix', 'Squeeze', 'SqueezeOut', 'Reset', 'Restart']) { Pattern.prototype[name + opType] = function (...other) { return this['_op' + opType](sequence(other), (a) => (b) => _composeOp(a, b, op)); }; - if (name === 'set' && opType !== '') { - Pattern.prototype[opType.toLowerCase()] = Pattern.prototype[name + opType]; + if (opType === 'Squeeze') { + // support 'squeezeIn' longhand + Pattern.prototype[name + "SqueezeIn"] = Pattern.prototype[name + opType]; + } + if (opType === 'In') { + // default how to 'in', e.g. add == addIn + Pattern.prototype[name] = Pattern.prototype[name + opType]; + } + else { + // default what to 'set', e.g. squeeze = setSqueeze + if (name === 'set') { + Pattern.prototype[opType.toLowerCase()] = Pattern.prototype[name + opType]; + } } } } diff --git a/packages/core/test/pattern.test.mjs b/packages/core/test/pattern.test.mjs index 1c60766a..44a0bd5e 100644 --- a/packages/core/test/pattern.test.mjs +++ b/packages/core/test/pattern.test.mjs @@ -148,11 +148,12 @@ describe('Pattern', function () { describe('add()', function () { it('Can add things', function () { assert.equal(pure(3).add(pure(4)).query(st(0, 1))[0].value, 7); + assert.equal(pure(3).addIn(pure(4)).query(st(0, 1))[0].value, 7); }); }); - describe('addFlip()', () => { + describe('addOut()', () => { it('Can add things with structure from second pattern', () => { - sameFirst(sequence(1, 2).addFlip(4), sequence(5, 6).struct(true)); + sameFirst(sequence(1, 2).addOut(4), sequence(5, 6).struct(true)); }); }); describe('addSqueeze()', () => { @@ -169,10 +170,10 @@ describe('Pattern', function () { ); }); }); - describe('addSqueezeFlip()', () => { + describe('addSqueezeOut()', () => { it('Can add while squeezing the first pattern inside the events of the second', () => { sameFirst( - sequence(1, [2, 3]).addSqueezeFlip(10, 20, 30), + sequence(1, [2, 3]).addSqueezeOut(10, 20, 30), sequence([11, [12, 13]], [21, [22, 23]], [31, [32, 33]]), ); }); @@ -208,9 +209,9 @@ describe('Pattern', function () { it('Can set things with plain values', function () { sameFirst(sequence(1, 2, 3).set(4), sequence(4).fast(3)); }); - describe('setFlip()', () => { + describe('setOut()', () => { it('Can set things with structure from second pattern', () => { - sameFirst(sequence(1, 2).setFlip(4), pure(4).mask(true, true)); + sameFirst(sequence(1, 2).setOut(4), pure(4).mask(true, true)); }); }); describe('setSqueeze()', () => {