Skip to content

Instantly share code, notes, and snippets.

@Locke
Last active February 13, 2016 15:17
Show Gist options
  • Save Locke/0761150d6e933e6ec651 to your computer and use it in GitHub Desktop.
Save Locke/0761150d6e933e6ec651 to your computer and use it in GitHub Desktop.
package locke.util.matching
import scala.util.matching.Regex
import scala.concurrent.Future
object implicits {
implicit class ConcurrentRegex(private val regex: Regex) extends AnyVal {
def replaceAllInConcurrent(target: CharSequence, replacer: Regex.Match => Future[String])(implicit ctx: scala.concurrent.ExecutionContext): Future[String] = {
val f: Iterator[Future[String]] = regex.findAllMatchIn(target).map(replacer)
Future.sequence(f).map( it2 => {
regex.replaceAllIn(target, y => it2.next)
})
}
def replaceSomeInConcurrent(target: CharSequence, replacer: Regex.Match => Future[Option[String]])(implicit ctx: scala.concurrent.ExecutionContext): Future[String] = {
val f: Iterator[Future[Option[String]]] = regex.findAllMatchIn(target).map(replacer)
Future.sequence(f).map( it2 => {
regex.replaceSomeIn(target, y => it2.next)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment