Skip to content

Commit

Permalink
Added map parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Dec 30, 2023
1 parent 2f9d3ec commit 55f5d39
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sksamuel.tribune.core.maps

import arrow.core.sequence
import com.sksamuel.tribune.core.Parser
import com.sksamuel.tribune.core.flatMap

fun <I, K, V, R, E> Parser<I, Map<K, V>, E>.parseKeys(parser: Parser<K, R, E>): Parser<I, Map<R, V>, E> {
return this.flatMap { input ->
input.map { (key, value) -> parser.parse(key).map { Pair(it, value) } }.sequence().map { it.toMap() }
}
}

fun <I, K, V, R, E> Parser<I, Map<K, V>, E>.parseValues(parser: Parser<V, R, E>): Parser<I, Map<K, R>, E> {
return this.flatMap { input ->
input.map { (key, value) -> parser.parse(value).map { Pair(key, it) } }.sequence().map { it.toMap() }
}
}

0 comments on commit 55f5d39

Please sign in to comment.