Skip to content

Commit

Permalink
Added flatMap
Browse files Browse the repository at this point in the history
  • Loading branch information
routis committed Jan 6, 2024
1 parent c3666a0 commit 66da319
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fun <I, A, B, E> Parser<I, A?, E>.mapIfNotNull(f: (A) -> B): Parser<I, B?, E> =
*
* @return a parser which returns the modified and flattened result of this parser.
*/
@JvmName("deprecatedFlatMap")
@Deprecated(message = "Deprecated. Use transformEither()", replaceWith = ReplaceWith("transformEither(f)"))
fun <I, A, B, E> Parser<I, A, E>.flatMap(f: (A) -> EitherNel<E, B>): Parser<I, B, E> =
Parser { this@flatMap.parse(it).flatMap(f) }
Expand All @@ -48,6 +49,10 @@ fun <I, A, B, E> Parser<I, A, E>.flatMap(f: (A) -> EitherNel<E, B>): Parser<I, B
*
* @return a parser which returns the modified and flattened result of this parser.
*/
fun <I, A, B, E:E2, E2> Parser<I, A, E>.transformEither(f: (A) -> EitherNel<E2, B>): Parser<I, B, E2> =
fun <I, O, E : E2, O2, E2> Parser<I, O, E>.transformEither(f: (O) -> EitherNel<E2, O2>): Parser<I, O2, E2> =
Parser { this@transformEither.parse(it).flatMap(f) }

fun <I, O, E : E2, I2 : I, O2, E2> Parser<I, O, E>.flatMap(f: (O) -> Parser<I2, O2, E2>): Parser<I2, O2, E2> =
Parser { i ->
parse(i).flatMap { o -> f(o).parse(i) }
}

0 comments on commit 66da319

Please sign in to comment.