1
- /**
1
+ /**
2
2
* lazy-linked-lists
3
3
* Lazy and infinite linked lists for JavaScript.
4
4
*
@@ -16,9 +16,10 @@ import {
16
16
EQ
17
17
} from './ord' ;
18
18
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' ;
22
23
23
24
export const emptyList = new List ( ) ;
24
25
@@ -120,7 +121,7 @@ export const cons = (x, xs) => new List(x, xs);
120
121
* head(lst); // => 1
121
122
*/
122
123
export const head = xs => {
123
- if ( isEmpty ( xs ) ) { throw errorEmptyList ( head ) ; }
124
+ if ( isEmpty ( xs ) ) { throw new EmptyListError ( head ) ; }
124
125
return xs . head ( ) ;
125
126
}
126
127
@@ -134,7 +135,7 @@ export const head = xs => {
134
135
* last(lst); // => 3
135
136
*/
136
137
export const last = xs => {
137
- if ( isEmpty ( xs ) ) { throw errorEmptyList ( last ) ; }
138
+ if ( isEmpty ( xs ) ) { throw new EmptyListError ( last ) ; }
138
139
return isEmpty ( tail ( xs ) ) ? head ( xs ) : last ( tail ( xs ) ) ;
139
140
}
140
141
@@ -148,7 +149,7 @@ export const last = xs => {
148
149
* tail(lst); // => [2:3:[]]
149
150
*/
150
151
export const tail = xs => {
151
- if ( isEmpty ( xs ) ) { throw errorEmptyList ( tail ) ; }
152
+ if ( isEmpty ( xs ) ) { throw new EmptyListError ( tail ) ; }
152
153
return xs . tail ( ) ;
153
154
}
154
155
@@ -162,7 +163,7 @@ export const tail = xs => {
162
163
* init(lst); // => [1:2:[]]
163
164
*/
164
165
export const init = xs => {
165
- if ( isEmpty ( xs ) ) { throw errorEmptyList ( init ) ; }
166
+ if ( isEmpty ( xs ) ) { throw new EmptyListError ( init ) ; }
166
167
return isEmpty ( tail ( xs ) ) ? emptyList : cons ( head ( xs ) , init ( tail ( xs ) ) ) ;
167
168
}
168
169
@@ -275,7 +276,7 @@ export const concat = xss => {
275
276
* index(lst, 3)); // => 4
276
277
*/
277
278
export const index = ( as , n ) => {
278
- if ( n < 0 || isEmpty ( as ) ) { throw errorOutOffRange ( index ) ; }
279
+ if ( n < 0 || isEmpty ( as ) ) { throw new OutOfRangeError ( head ) ; }
279
280
const x = head ( as ) ;
280
281
const xs = tail ( as ) ;
281
282
if ( n === 0 ) { return x ; }
@@ -549,7 +550,7 @@ export const replicate = (n, x) => take(n, repeat(x));
549
550
* index(c, 100); // => 2
550
551
*/
551
552
export const cycle = as => {
552
- if ( isEmpty ( as ) ) { throw errorEmptyList ( cycle ) ; }
553
+ if ( isEmpty ( as ) ) { throw new EmptyListError ( cycle ) ; }
553
554
let x = head ( as ) ;
554
555
let xs = tail ( as ) ;
555
556
const c = list ( x ) ;
0 commit comments