Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I curry the String.format function to use it with map over an array?

Tags:

scala

I have an array of strings and I need to turn them into quoted strings. Obviously I could just iterate over the array and use something like "\"%s\"".format(elem) to replace each element, but this seems grungy considering that my next step would be arr.mkString("(", "OR", ")")

I tried to curry String.format as follows:

val curried = "\"%s\"".format(_)
arr.map(curried)

But that does not work and complains:

 found   : (Any*) => String
 required: (java.lang.String) => ?

How do I map a function like String.format over an array of strings? Is there another way to curry it or should I perhaps be specifying types?

like image 882
Michael Dillon Avatar asked Mar 02 '26 20:03

Michael Dillon


1 Answers

You need to help compiler a little bit, and specify the desired type (at least it's one of the possible solutions to your problem):

val curried = "\"%s\"".format(_: String) 

Otherwise compiler will produce Seq[Any] => String function, because format method has varargs.

like image 157
tenshi Avatar answered Mar 04 '26 16:03

tenshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!