dedup many

This commit is contained in:
Rowan 2025-04-16 22:27:45 -05:00
parent 74bac48730
commit 628d0ef80b
3 changed files with 23 additions and 16 deletions

View file

@ -32,3 +32,24 @@ export const anyOf = curry(
any(...mapStr(char, str))(state) any(...mapStr(char, str))(state)
)) ))
export const map = curry(
/**
* @param {(...args: any[]) => any} fn
* @param {Parser} parser
* @param {ParserState} state
*/
(fn, parser, state) => {
return parser(state).chain(result => {
try {
/** @type {Result<ParserState, ParseError>} */
const parsed = fn(diff(state[0], result[0]))
const backtrack = Tuple(state[0], result[1])
return succeed(parsed, backtrack)
} catch (e) {
return fail('failed to map', state, e)
}
})
})

View file

@ -44,19 +44,6 @@ export const until = parser => state => {
return result return result
} }
export const many = curry((parser, state) => { export const skip = curry((parser, state) => {
let result = ok(state)
while (true) {
const [original, clone] = fork(result)
const res = result.chain(x => parser(clone))
if (res.isOk()) {
result = res
} else {
break
}
}
return result
}) })

View file

@ -4,8 +4,7 @@ import { mapStr, join } from '../../src/fn.js'
export const Tests = [ export const Tests = [
it('whatever', () => { it('whatever', () => {
const a = 'abcdef!!!!'.split('')
parse(str('abc'), 'abc').map(console.log)
}) })
] ]