pMap

fun <I : Any, O, R, U : Any> pMap(parser: Parser<I, O, U>, transform: (O) -> R): Parser<I, R, U>

Returns a Parser that runs parser and applies transform to the output value if it succeeds.

pMap is the primary way to convert raw parsed tokens into meaningful domain types without altering parse position or error behaviour.

Behaviour

ConditionResult
parser failsFailure propagated unchanged
parser succeedsSuccess with transform(value); index and input unchanged

Type parameters

  • I — the token type consumed by parser.

  • O — the output type of parser and the input type of transform.

  • R — the output type of transform and of the returned parser.

  • U — the user context type threaded through unchanged.

Example

val digit = pSatisfy<Char, Unit> { it.isDigit() }
val digitInt = pMap(digit) { it.digitToInt() }

val input = ParserInput.of("3".toList(), Unit)
val result = digitInt(input) // Success(3, nextIndex=1, ...)

Return

a Parser that produces transform(value) on success.

Parameters

parser

the parser whose success value is transformed.

transform

a function applied to the parsed value on success; should be side-effect-free.

See also