Skip to content

Commit 6b9fe93

Browse files
♻️ refactor: Add assertions and remove unreachable code.
1 parent 70bee5c commit 6b9fe93

File tree

12 files changed

+43
-73
lines changed

12 files changed

+43
-73
lines changed

src/0-core/_fast/_append_small_list.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import assert from 'assert';
22

33
export default function _append_small_list(tree, list) {
4-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
4+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
55
// eslint-disable-next-line default-case
66
switch (list.length) {
7-
case 0:
8-
return tree;
97
case 1:
108
return tree.push(list[0]);
119
case 2:

src/0-core/_fast/_digit.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import {One, Two, Three} from '../../1-digit/index.js';
1212
*/
1313
export function _digit(list) {
1414
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 3);
15+
// eslint-disable-next-line default-case
1516
switch (list.length) {
1617
case 1:
1718
return new One(list[0]);
1819
case 2:
1920
return new Two(list[0], list[1]);
20-
default:
21+
case 3:
2122
return new Three(list[0], list[1], list[2]);
2223
}
2324
}

src/0-core/_fast/_prepend_small_list.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import assert from 'assert';
22

33
export default function _prepend_small_list(tree, list) {
4-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
4+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
55
// eslint-disable-next-line default-case
66
switch (list.length) {
7-
case 0:
8-
return tree;
97
case 1:
108
return tree.cons(list[0]);
119
case 2:

src/1-digit/1-One.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ One.prototype._nodes_with_list = function (M, list, other) {
8181

8282
One.prototype._nodes_with_list_and_one = function (M, list, other) {
8383
assert(other instanceof One);
84-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
84+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
8585
// eslint-disable-next-line default-case
8686
switch (list.length) {
87-
case 0:
88-
return [node2(M, other.a, this.a)];
8987
case 1:
9088
return [node3(M, other.a, list[0], this.a)];
9189
case 2:
@@ -102,11 +100,9 @@ One.prototype._nodes_with_list_and_one = function (M, list, other) {
102100

103101
One.prototype._nodes_with_list_and_two = function (M, list, other) {
104102
assert(other instanceof Two);
105-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
103+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
106104
// eslint-disable-next-line default-case
107105
switch (list.length) {
108-
case 0:
109-
return [node3(M, other.a, other.b, this.a)];
110106
case 1:
111107
return [other.node(M), node2(M, list[0], this.a)];
112108
case 2:
@@ -127,11 +123,9 @@ One.prototype._nodes_with_list_and_two = function (M, list, other) {
127123

128124
One.prototype._nodes_with_list_and_three = function (M, list, other) {
129125
assert(other instanceof Three);
130-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
126+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
131127
// eslint-disable-next-line default-case
132128
switch (list.length) {
133-
case 0:
134-
return [node2(M, other.a, other.b), node2(M, other.c, this.a)];
135129
case 1:
136130
return [other.node(M), node2(M, list[0], this.a)];
137131
case 2:
@@ -153,11 +147,9 @@ One.prototype._nodes_with_list_and_three = function (M, list, other) {
153147

154148
One.prototype._nodes_with_list_and_four = function (M, list, other) {
155149
assert(other instanceof Four);
156-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
150+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
157151
// eslint-disable-next-line default-case
158152
switch (list.length) {
159-
case 0:
160-
return [node3(M, other.a, other.b, other.c), node2(M, other.d, this.a)];
161153
case 1:
162154
return [
163155
node3(M, other.a, other.b, other.c),

src/1-digit/2-Two.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,9 @@ Two.prototype._nodes_with_list = function (M, list, other) {
8787

8888
Two.prototype._nodes_with_list_and_one = function (M, list, other) {
8989
assert(other instanceof One);
90-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
90+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
9191
// eslint-disable-next-line default-case
9292
switch (list.length) {
93-
case 0:
94-
return [node3(M, other.a, this.a, this.b)];
9593
case 1:
9694
return [node2(M, other.a, list[0]), this.node(M)];
9795
case 2:
@@ -113,11 +111,9 @@ Two.prototype._nodes_with_list_and_one = function (M, list, other) {
113111

114112
Two.prototype._nodes_with_list_and_two = function (M, list, other) {
115113
assert(other instanceof Two);
116-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
114+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
117115
// eslint-disable-next-line default-case
118116
switch (list.length) {
119-
case 0:
120-
return [node2(M, other.a, other.b), node2(M, this.a, this.b)];
121117
case 1:
122118
return [node3(M, other.a, other.b, list[0]), node2(M, this.a, this.b)];
123119
case 2:
@@ -139,11 +135,9 @@ Two.prototype._nodes_with_list_and_two = function (M, list, other) {
139135

140136
Two.prototype._nodes_with_list_and_three = function (M, list, other) {
141137
assert(other instanceof Three);
142-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
138+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
143139
// eslint-disable-next-line default-case
144140
switch (list.length) {
145-
case 0:
146-
return [node3(M, other.a, other.b, other.c), node2(M, this.a, this.b)];
147141
case 1:
148142
return [
149143
node3(M, other.a, other.b, other.c),
@@ -172,14 +166,9 @@ Two.prototype._nodes_with_list_and_three = function (M, list, other) {
172166

173167
Two.prototype._nodes_with_list_and_four = function (M, list, other) {
174168
assert(other instanceof Four);
175-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
169+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
176170
// eslint-disable-next-line default-case
177171
switch (list.length) {
178-
case 0:
179-
return [
180-
node3(M, other.a, other.b, other.c),
181-
node3(M, other.d, this.a, this.b),
182-
];
183172
case 1:
184173
return [
185174
node2(M, other.a, other.b),

src/1-digit/3-Three.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,9 @@ Three.prototype._nodes_with_list = function (M, list, other) {
9191

9292
Three.prototype._nodes_with_list_and_one = function (M, list, other) {
9393
assert(other instanceof One);
94-
assert(list.length <= 4);
94+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
9595
// eslint-disable-next-line default-case
9696
switch (list.length) {
97-
case 0:
98-
return [node2(M, other.a, this.a), node2(M, this.b, this.c)];
9997
case 1:
10098
return [node2(M, other.a, list[0]), this.node(M)];
10199
case 2:
@@ -117,11 +115,9 @@ Three.prototype._nodes_with_list_and_one = function (M, list, other) {
117115

118116
Three.prototype._nodes_with_list_and_two = function (M, list, other) {
119117
assert(other instanceof Two);
120-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
118+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
121119
// eslint-disable-next-line default-case
122120
switch (list.length) {
123-
case 0:
124-
return [other.node(M), this.node(M)];
125121
case 1:
126122
return [node3(M, other.a, other.b, list[0]), this.node(M)];
127123
case 2:
@@ -139,11 +135,9 @@ Three.prototype._nodes_with_list_and_two = function (M, list, other) {
139135

140136
Three.prototype._nodes_with_list_and_three = function (M, list, other) {
141137
assert(other instanceof Three);
142-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
138+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
143139
// eslint-disable-next-line default-case
144140
switch (list.length) {
145-
case 0:
146-
return [other.node(M), this.node(M)];
147141
case 1:
148142
return [
149143
node2(M, other.a, other.b),
@@ -166,15 +160,9 @@ Three.prototype._nodes_with_list_and_three = function (M, list, other) {
166160

167161
Three.prototype._nodes_with_list_and_four = function (M, list, other) {
168162
assert(other instanceof Four);
169-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
163+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
170164
// eslint-disable-next-line default-case
171165
switch (list.length) {
172-
case 0:
173-
return [
174-
node2(M, other.a, other.b),
175-
node2(M, other.c, other.d),
176-
this.node(M),
177-
];
178166
case 1:
179167
return [
180168
node2(M, other.a, other.b),

src/1-digit/4-Four.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@ Four.prototype._nodes_with_list = function (M, list, other) {
102102

103103
Four.prototype._nodes_with_list_and_one = function (M, list, other) {
104104
assert(other instanceof One);
105-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
105+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
106106
// eslint-disable-next-line default-case
107107
switch (list.length) {
108-
case 0:
109-
return [node3(M, other.a, this.a, this.b), node2(M, this.c, this.d)];
110108
case 1:
111109
return [
112110
node3(M, other.a, list[0], this.a),
@@ -135,14 +133,9 @@ Four.prototype._nodes_with_list_and_one = function (M, list, other) {
135133

136134
Four.prototype._nodes_with_list_and_two = function (M, list, other) {
137135
assert(other instanceof Two);
138-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
136+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
139137
// eslint-disable-next-line default-case
140138
switch (list.length) {
141-
case 0:
142-
return [
143-
node3(M, other.a, other.b, this.a),
144-
node3(M, this.b, this.c, this.d),
145-
];
146139
case 1:
147140
return [
148141
other.node(M),
@@ -173,15 +166,9 @@ Four.prototype._nodes_with_list_and_two = function (M, list, other) {
173166

174167
Four.prototype._nodes_with_list_and_three = function (M, list, other) {
175168
assert(other instanceof Three);
176-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
169+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
177170
// eslint-disable-next-line default-case
178171
switch (list.length) {
179-
case 0:
180-
return [
181-
other.node(M),
182-
node2(M, this.a, this.b),
183-
node2(M, this.c, this.d),
184-
];
185172
case 1:
186173
return [
187174
other.node(M),
@@ -213,15 +200,9 @@ Four.prototype._nodes_with_list_and_three = function (M, list, other) {
213200

214201
Four.prototype._nodes_with_list_and_four = function (M, list, other) {
215202
assert(other instanceof Four);
216-
assert(Number.isInteger(list.length) && list.length >= 0 && list.length <= 4);
203+
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
217204
// eslint-disable-next-line default-case
218205
switch (list.length) {
219-
case 0:
220-
return [
221-
node3(M, other.a, other.b, other.c),
222-
node2(M, other.d, this.a),
223-
node3(M, this.b, this.c, this.d),
224-
];
225206
case 1:
226207
return [
227208
node3(M, other.a, other.b, other.c),

src/3-tree/implementations/0-Empty.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {_EMPTY} from '../../0-core/index.js';
44
import {_from_medium_list} from '../../0-core/_fast/_from_medium_list.js';
55
import _append_small_list from '../../0-core/_fast/_append_small_list.js';
66
import _from_by_filling from '../../0-core/_fast/_from_by_filling.js';
7+
import isSameMeasure from '../../_debug/isSameMeasure.js';
78
import {Single, Deep} from './index.js';
89

910
export function Empty(M) {
@@ -50,6 +51,7 @@ Empty.prototype.append = function (iterable) {
5051
};
5152

5253
Empty.prototype.concat = function (other) {
54+
assert(other instanceof Tree);
5355
return other;
5456
};
5557

@@ -72,6 +74,7 @@ Empty.prototype.split = function (_p) {
7274

7375
Empty.prototype._concat_with_deep = function (other) {
7476
assert(other instanceof Deep);
77+
assert(isSameMeasure(other.M, this.M));
7578
return other;
7679
};
7780

@@ -90,5 +93,6 @@ Empty.prototype._app3_with_single = function (list, value) {
9093

9194
Empty.prototype._app3_with_deep = function (list, other) {
9295
assert(other instanceof Deep);
96+
assert(isSameMeasure(other.M, this.M));
9397
return _append_small_list(other, list);
9498
};

src/3-tree/implementations/1-Single.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {One} from '../../1-digit/index.js';
55
import _append_small_list from '../../0-core/_fast/_append_small_list.js';
66
import {_from_medium_list} from '../../0-core/_fast/_from_medium_list.js';
77
import _fill_right from '../../0-core/_fast/_fill_right.js';
8+
import isSameMeasure from '../../_debug/isSameMeasure.js';
89
import {Empty, Deep} from './index.js';
910

1011
export function Single(M, value) {
@@ -71,6 +72,7 @@ Single.prototype.append = function (iterable) {
7172
};
7273

7374
Single.prototype.concat = function (other) {
75+
assert(other instanceof Tree);
7476
return other.cons(this.a);
7577
};
7678

@@ -95,6 +97,7 @@ Single.prototype.split = function (p) {
9597

9698
Single.prototype._concat_with_deep = function (other) {
9799
assert(other instanceof Deep);
100+
assert(isSameMeasure(other.M, this.M));
98101
return other.push(this.a);
99102
};
100103

@@ -113,5 +116,6 @@ Single.prototype._app3_with_single = function (list, value) {
113116

114117
Single.prototype._app3_with_deep = function (list, other) {
115118
assert(other instanceof Deep);
119+
assert(isSameMeasure(other.M, this.M));
116120
return _append_small_list(other, list).push(this.a);
117121
};

src/3-tree/implementations/2-Deep.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {One, Two, Four} from '../../1-digit/index.js';
1212
import {delay, Lazy} from '../../4-lazy/index.js';
1313
import _prepend_small_list from '../../0-core/_fast/_prepend_small_list.js';
1414
import _fill_right from '../../0-core/_fast/_fill_right.js';
15+
import isSameMeasure from '../../_debug/isSameMeasure.js';
1516
import _append_small_list from '../../0-core/_fast/_append_small_list.js';
1617
import {Empty} from './index.js';
1718

@@ -132,6 +133,7 @@ Deep.prototype.append = function (iterable) {
132133
};
133134

134135
Deep.prototype.concat = function (other) {
136+
assert(other instanceof Tree);
135137
return other._concat_with_deep(this);
136138
};
137139

@@ -202,6 +204,7 @@ Deep.prototype.split = function (p) {
202204

203205
Deep.prototype._concat_with_deep = function (other) {
204206
assert(other instanceof Deep);
207+
assert(isSameMeasure(other.M, this.M));
205208
return new Deep(
206209
this.M,
207210
other.left,
@@ -225,6 +228,7 @@ Deep.prototype._app3_with_single = function (list, value) {
225228

226229
Deep.prototype._app3_with_deep = function (list, other) {
227230
assert(other instanceof Deep);
231+
assert(isSameMeasure(other.M, this.M));
228232
return new Deep(
229233
this.M,
230234
other.left,

0 commit comments

Comments
 (0)