Improve scala functional string transformation How could this scala code be made more concise and/or idiomatic? Do I need to define each functions as a val in order to pass them as values, or is there a shorthand for this? Also looking for feedback on the disenvowel function.



// See: http://nurkiewicz.blogspot.com/2012/04/secret-powers-of-foldleft-in-scala.html
def transformString(s: String, fns: List[Function1[String,String]]) :String =
  (s /: fns) { (s: String, f: Function1[String,String]) =&gt; f apply s }

val toUpper = (s: String) =&gt; s.toUpperCase
val reverse = (s: String) =&gt; s.reverse
val disenvowel = (s: String) =&gt; s filterNot { Set('a','e','i','o','u') contains _ }

// scala&gt; transformString("blah",List(disenvowel))
// res175: String = blh

// scala&gt; transformString("blah",List(disenvowel, toUpper))
// res176: String = BLH

// scala&gt; transformString("blah",List(disenvowel, toUpper, reverse))
// res177: String = HLB
If I might chime in briefly, it all looks fine, any modifications here are mostly a matter of taste and personal preference; for example, I'd make it a curried function with varargs:

def transformString(s : String)(functions : String =&gt; String*) = 
    functions.foldLeft(s) { (str, fun) =&gt; fun(str) }


And you don't need to define functions as literals, for example:

scala&gt; def toUpper(s : String) = s.toUpperCase
toUpper: (s: String)String

scala&gt; def reverse(s : String) = s.reverse
reverse: (s: String)String


And then, using the slightly modified version above:

scala&gt; transformString("blah")(toUpper, reverse)
res3: String = HALB


Of course, you can always use the literal, too:

scala&gt; val reverse2 = (s : String) =&gt; s.reverse
reverse2: String =&gt; String = &lt;function1&gt;

scala&gt; transformString("blah")(reverse2)
res4: String = halb


Or even provide the lambda in-place:

scala&gt; transformString("blah")(_ + "1", _.reverse)
res9: String = 1halb


IMO, varargs here work nicely, and you can transform any sequence/collection into varargs by :_*; there are a few differences, though:


def methods have to be partially applied in the list (e.g. : toUpper_ below)
Underscore shorthand syntax won't work


so:

scala&gt; transformString("blah ")(List(reverse2, (s : String) =&gt; s + "12", toUpper_):_*)
res21: String = " HALB12"


[EDIT for the comments]

1 &amp; 2. Oh, there is a difference. You can easily make a function partially applied if it's curried, not so much if it just takes in arguments. Also, you can make shorthand underscore syntax work after applying the function partially:

scala&gt; def blahArg = transformString("blah")_
blahArg: Seq[String =&gt; String] =&gt; String


Note the trailing underscore and the return type: a function (with lower arity). Now you can pass that function somewhere else where the second argument can be supplied. You can't do that when taking in both arguments at once. That also means that your second parameter changed from varargs to Seq, which - in turn - means you explicitly have to pass a Seq, List or something similar:

scala&gt; blahArg(List(_.reverse))
res10: String = halb


3.Removing vowels looks fine :) If that's contained inside an object, you can move the Set initialization to the object level, so it's only initialized once instead of on every function call, but that's nitpicking.