Skip to content

Commit 04ae1b6

Browse files
committed
improvement
1 parent 5150451 commit 04ae1b6

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/parser/parser-lib.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,13 @@ export function sequence<T extends Array<unknown>>(
201201
*
202202
* Will cause infinite recursion if the parser can parse nothing.
203203
*/
204-
export function many_<T>(parser: Parser<T>): Parser<Array<T>> {
205-
return choice(
204+
export const many = memoize(<T>(parser: Parser<T>): Parser<Array<T>> =>
205+
choice(
206206
sequence(parser, lazy(() => many(parser)))
207207
.map(([first, rest]) => [first, ...rest]),
208208
emptyArray,
209-
);
210-
}
211-
export const many = memoize(many_);
209+
)
210+
);
212211
/**
213212
* Like `many` but parses at least once.
214213
*
@@ -228,14 +227,13 @@ export function manyAtLeastOnce<T>(parser: Parser<T>): Parser<Array<T>> {
228227
*
229228
* Will cause infinite recursion if the parser can parse nothing.
230229
*/
231-
export function all_<T>(parser: Parser<T>): Parser<Array<T>> {
232-
return choiceOnlyOne(
230+
export const all = memoize(<T>(parser: Parser<T>): Parser<Array<T>> =>
231+
choiceOnlyOne(
233232
sequence(parser, lazy(() => all(parser)))
234233
.map(([first, rest]) => [first, ...rest]),
235234
emptyArray,
236-
);
237-
}
238-
export const all = memoize(all_);
235+
)
236+
);
239237
/**
240238
* Like `all` but parses at least once.
241239
*
@@ -247,8 +245,8 @@ export function allAtLeastOnce<T>(parser: Parser<T>): Parser<Array<T>> {
247245
return sequence(parser, all(parser))
248246
.map(([first, rest]) => [first, ...rest]);
249247
}
250-
export function count<T>(parser: Parser<Array<T>>): Parser<number> {
251-
return parser.map((array) => array.length);
248+
export function count(parser: Parser<{ length: number }>): Parser<number> {
249+
return parser.map(({ length }) => length);
252250
}
253251
function describeSource(src: string): string {
254252
if (src === "") {

0 commit comments

Comments
 (0)