Apply multiple string conversions in scala

I want to execute several ordered and sequential replaceAll (..., ...) in a string in a functional way in scala.

What is the most elegant solution? Scalaaz is welcome !;)

+7
source share
4 answers

First, let’s get the function from the replaceAll method:

 scala> val replace = (from: String, to: String) => (_:String).replaceAll(from, to) replace: (String, String) => String => java.lang.String = <function2> 

Now you can use the Functor instance for the function in scalaz. This way you can create functions using map (or, to make it better, using unicode aliases).

It will look like this:

 scala> replace("from", "to") ∘ replace("to", "from") ∘ replace("some", "none") res0: String => java.lang.String = <function1> 

If you prefer the haskell-way layout (from right to left), use contramap :

 scala> replace("some", "none") βˆ™ replace("to", "from") βˆ™ replace ("from", "to") res2: String => java.lang.String = <function1> 

You can also have some fun with the Category instance :

 scala> replace("from", "to") β‹™ replace("to", "from") β‹™ replace("some", "none") res5: String => java.lang.String = <function1> scala> replace("some", "none") β‹˜ replace("to", "from") β‹˜ replace ("from", "to") res7: String => java.lang.String = <function1> 

And applying it:

 scala> "somestringfromto" |> res0 res3: java.lang.String = nonestringfromfrom scala> res2("somestringfromto") res4: java.lang.String = nonestringfromfrom scala> "somestringfromto" |> res5 res6: java.lang.String = nonestringfromfrom scala> res7("somestringfromto") res8: java.lang.String = nonestringfromfrom 
+13
source

If these are just a few invitations, just put them together. Otherwise, I think I would try this:

 Seq("a" -> "b", "b" -> "a").foldLeft("abab"){case (z, (s,r)) => z.replaceAll(s, r)} 

Or if you like a shorter code with confusing wildcards and extra closures:

 Seq("a" -> "b", "b" -> "a").foldLeft("abab"){_.replaceAll _ tupled(_)} 
+15
source

Another Scalaz-based solution for this problem is to use the Endo monoid. This monoid captures the identity function (as an element of the monoid identifier) ​​and the composition of the function (as the operation of adding a monoid). This solution would be especially useful if you have an arbitrary size (even possibly empty) list of applicable functions.

 val replace = (from: String, to: String) => (_:String).replaceAll(from, to) val f: Endo[String] = List( replace("some", "none"), replace("to", "from"), replace("from", "to") ).foldMap(_.endo) 

eg. (using one example)

 scala> f.run("somestringfromto") res0: String = nonestringfromfrom 
+4
source

Define a replacement function with anonymous parameters, and then you can sequentially group the replacement functions.

 scala> val s = "hello world" res0: java.lang.String = hello world scala> def replace = s.replaceAll(_, _) replace: (java.lang.String, java.lang.String) => java.lang.String scala> replace("h", "H") replace("w", "W") res1: java.lang.String = Hello World 
+3
source

Source: https://habr.com/ru/post/919786/


All Articles