Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply several string transformations in scala

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 ! ;)

like image 872
M'λ' Avatar asked Jul 05 '12 17:07

M'λ'


1 Answers

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(_)}
like image 90
Kaito Avatar answered Oct 22 '22 13:10

Kaito