I want to perform several ordered and successive replaceAll(...,...) on a string in a functional way in scala.
What's the most elegant solution ? Scalaz welcome ! ;)
If its just a few invocations then just chain them. Otherwise I guess I'd try this:
Seq("a" -> "b", "b" -> "a").foldLeft("abab"){case (z, (s,r)) => z.replaceAll(s, r)}
Or if you like shorter code with confusing wildcards and extra closures:
Seq("a" -> "b", "b" -> "a").foldLeft("abab"){_.replaceAll _ tupled(_)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With