@@ -14,15 +14,19 @@ export type Type = {
14
14
count : number
15
15
}
16
16
17
- function foldleft ( a , b ) {
18
- if ( ! a . sum ) throw new TypeError ( `Unexpected lvalue type ${ JSON . stringify ( { a, b } ) } ` )
19
- if ( b . sum ) return b . sum . reduce ( ( xa , xb ) => foldleft ( xa , xb ) , a )
20
- for ( const x of a . sum ) {
21
- const xb = fold ( x , b )
22
- if ( xb . sum ) continue // not compatible
23
- return { sum : [ ...a . sum . filter ( y => y !== x ) , xb ] , count : 1 }
17
+ function foldleft ( xs : Type [ ] ) : Type {
18
+ for ( const a of xs ) {
19
+ for ( const b of xs ) {
20
+ if ( a === b ) continue
21
+ const ab = fold ( a , b )
22
+ if ( ab . sum ) continue // couldnt fold
23
+ return foldleft ( [ ab , ...xs . filter ( x => x !== a && x !== b ) ] )
24
+ }
25
+ }
26
+ return {
27
+ sum : xs ,
28
+ count : 1
24
29
}
25
- return { sum : [ ...a . sum , b ] , count : 1 }
26
30
}
27
31
28
32
export function fold ( a : Type , b : Type ) : Type {
@@ -37,7 +41,9 @@ export function fold (a: Type, b: Type): Type {
37
41
}
38
42
}
39
43
40
- if ( a . sum ) return foldleft ( a , b )
44
+ if ( a . sum && b . sum ) return foldleft ( [ ...a . sum , ...b . sum ] )
45
+ if ( a . sum ) return foldleft ( [ b , ...a . sum ] )
46
+ if ( b . sum ) return foldleft ( [ a , ...b . sum ] )
41
47
if ( a . record && b . record ) {
42
48
return {
43
49
record : {
0 commit comments