Skip to content

Commit 941e84b

Browse files
committed
feat(patterns): For containerHasSplit(copyArray, ...), scan from the start
Ref #3065 (comment)
1 parent c39e24c commit 941e84b

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

packages/patterns/src/patterns/patternMatchers.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,27 +1327,26 @@ const makePatternKit = () => {
13271327
* @param {Pattern} elementPatt
13281328
* @param {bigint} bound Must be >= 1n
13291329
* @param {Rejector} reject
1330-
* @param {T[]} [inResults]
1331-
* @param {T[]} [outResults]
1330+
* @param {T[] | undefined} inResults
1331+
* @param {T[] | undefined} outResults
1332+
* @param {-1 | 1} direction -1 for picking from the end (which gives
1333+
* intuitive results with descending lexicographic CopySet payloads); 1 for
1334+
* picking from the start (which gives intuitive results with other arrays)
13321335
* @returns {boolean}
13331336
*/
13341337
const confirmElementsHasSplit = (
13351338
elements,
13361339
elementPatt,
13371340
bound,
13381341
reject,
1339-
inResults = undefined,
1340-
outResults = undefined,
1342+
inResults,
1343+
outResults,
1344+
direction,
13411345
) => {
13421346
let inCount = 0n;
1343-
// Since this feature is motivated by ERTP's use on
1344-
// non-fungible (`set`, `copySet`) amounts,
1345-
// their arrays store their elements in decending lexicographic order.
1346-
// But this function has to make some choice amoung equally good minimal
1347-
// results. It is more intuitive for the choice to be the first `bound`
1348-
// matching elements in ascending lexicigraphic order, rather than
1349-
// decending. Thus we iterate `elements` in reverse order.
1350-
for (let i = elements.length - 1; i >= 0; i -= 1) {
1347+
const firstIndex = direction === -1 ? elements.length - 1 : 0;
1348+
const stopIndex = direction === -1 ? -1 : elements.length;
1349+
for (let i = firstIndex; i !== stopIndex; i += direction) {
13511350
const element = elements[i];
13521351
if (inCount >= bound) {
13531352
if (!outResults) break;
@@ -1455,6 +1454,7 @@ const makePatternKit = () => {
14551454
reject,
14561455
inResults,
14571456
outResults,
1457+
1,
14581458
) && harden([inResults, outResults])
14591459
);
14601460
}
@@ -1467,6 +1467,7 @@ const makePatternKit = () => {
14671467
reject,
14681468
inResults,
14691469
outResults,
1470+
-1,
14701471
) &&
14711472
harden([
14721473
inResults && makeCopySet(inResults),

packages/patterns/test/containerHasSplit.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ const testContainerHasSplit = test.macro((t, config) => {
8484
specimen,
8585
pattern: M.string(),
8686
bound: 1n,
87-
expectAccepted: ['bar'],
88-
expectRejected: [2, 'foo', 1],
87+
expectAccepted: ['foo'],
88+
expectRejected: [1, 2, 'bar'],
8989
});
9090

9191
test('split first two matches from copyArray', testContainerHasSplit, {
9292
specimen,
9393
pattern: M.string(),
9494
bound: 2n,
95-
expectAccepted: ['bar', 'foo'],
96-
expectRejected: [2, 1],
95+
expectAccepted: ['foo', 'bar'],
96+
expectRejected: [1, 2],
9797
});
9898

9999
test('fail against copyArray', testContainerHasSplit, {

0 commit comments

Comments
 (0)