Skip to content

Commit 5f8d03c

Browse files
committed
Replace error function calls with error objects
1 parent 73c252f commit 5f8d03c

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

source/lib.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* lazy-linked-lists
33
* Lazy and infinite linked lists for JavaScript.
44
*
@@ -16,9 +16,10 @@ import {
1616
EQ
1717
} from './ord';
1818

19-
const errorEmptyList = from => new Error(`*** Exception: ${from.name}: empty list`);
20-
21-
const errorOutOffRange = from => new Error(`*** Exception: ${from.name}: range error`);
19+
import {
20+
EmptyListError,
21+
OutOfRangeError
22+
} from './error';
2223

2324
export const emptyList = new List();
2425

@@ -120,7 +121,7 @@ export const cons = (x, xs) => new List(x, xs);
120121
* head(lst); // => 1
121122
*/
122123
export const head = xs => {
123-
if (isEmpty(xs)) { throw errorEmptyList(head); }
124+
if (isEmpty(xs)) { throw new EmptyListError(head); }
124125
return xs.head();
125126
}
126127

@@ -134,7 +135,7 @@ export const head = xs => {
134135
* last(lst); // => 3
135136
*/
136137
export const last = xs => {
137-
if (isEmpty(xs)) { throw errorEmptyList(last); }
138+
if (isEmpty(xs)) { throw new EmptyListError(last); }
138139
return isEmpty(tail(xs)) ? head(xs) : last(tail(xs));
139140
}
140141

@@ -148,7 +149,7 @@ export const last = xs => {
148149
* tail(lst); // => [2:3:[]]
149150
*/
150151
export const tail = xs => {
151-
if (isEmpty(xs)) { throw errorEmptyList(tail); }
152+
if (isEmpty(xs)) { throw new EmptyListError(tail); }
152153
return xs.tail();
153154
}
154155

@@ -162,7 +163,7 @@ export const tail = xs => {
162163
* init(lst); // => [1:2:[]]
163164
*/
164165
export const init = xs => {
165-
if (isEmpty(xs)) { throw errorEmptyList(init); }
166+
if (isEmpty(xs)) { throw new EmptyListError(init); }
166167
return isEmpty(tail(xs)) ? emptyList : cons(head(xs), init(tail(xs)));
167168
}
168169

@@ -275,7 +276,7 @@ export const concat = xss => {
275276
* index(lst, 3)); // => 4
276277
*/
277278
export const index = (as, n) => {
278-
if (n < 0 || isEmpty(as)) { throw errorOutOffRange(index); }
279+
if (n < 0 || isEmpty(as)) { throw new OutOfRangeError(head); }
279280
const x = head(as);
280281
const xs = tail(as);
281282
if (n === 0) { return x; }
@@ -549,7 +550,7 @@ export const replicate = (n, x) => take(n, repeat(x));
549550
* index(c, 100); // => 2
550551
*/
551552
export const cycle = as => {
552-
if (isEmpty(as)) { throw errorEmptyList(cycle); }
553+
if (isEmpty(as)) { throw new EmptyListError(cycle); }
553554
let x = head(as);
554555
let xs = tail(as);
555556
const c = list(x);

0 commit comments

Comments
 (0)