How can I call the following Scala method from Java?
def mult[A,B: ClassTag,C: ClassTag](rdd1:RDD[A], rdd2:RDD[B])(implicit multiplier: Multiplier[A,B,C]): RDD[C] =
rdd1.zip(rdd2).map(p => multiplier.multiply(p._1, p._2))
Is it possible? Eclipse isn't giving me any help from its autocomplete.
Ugh. Must you? The B and C ClassTags are added to the list of implicit parameters (before the explicit ones), so you can add appropriate ones generated with the scala.reflect.ClassTag object. But it's going to be ugly.
Something like (untested):
mult(rdd1, rdd2, scala.reflect.ClassTag.apply(B.class), scala.reflect.ClassTag.apply(C.class), myMult);
Related
On an Android project with both Kotlin and java, I want to use the Kotlin functions substringBeforeLast(delimiter) and substringAfterLast(delimiter) in some java files.
So i thought of using extensions. I did the following which works,
fun String.getStringAfterLast(): String{
return this.substringAfterLast(".")
}
however i was thinking of passing the delimiter as a parameter, but it gives me an error when trying to use in java "remove 2nd argument..."
fun String.getStringBeforeLast(delimiter: String): String{
return this.substringBeforeLast(delimiter)
}
Is the approach correct ? Can it be done ?
Your approach to this problem is correct and it should work. For example, with this Kotlin code:
fun String.getStringBeforeLast(delimiter: String): String{
return this.substringBeforeLast(delimiter)
}
You should be able to invoke the function from Java like this:
ExtensionKt.getStringBeforeLast(str, ",");
If it didn't work then I guess there had to be some small mistake, like for example: you added delimiter param to getStringBeforeLast() function, but mistakenely tried to invoke getStringAfterLast() function from Java.
Also, you can always invoke functions of Kotlin stdlib directly from Java. Just note that substringBeforeLast()/substringAfterLast() actually receive one additional, optional parameter and you need to provide it from Java, making the code a little more verbose:
StringsKt.substringBeforeLast(str, ",", str);
I want to do exactly what java's String Template does, but in scala. This library however does not work with case classes:
case class Obj(str:String)
val st = new ST("xx $obj.str$ xx",'$','$')
st.add("obj",Obj("replacement"))
st.render() //returns "xx xx"
ST tries to find property "str" with reflection, but it just does not work with scala.
How can I achieve it without ST?
Try to create your class like this:
case class Obj(#BeanProperty str: String)
Here is the scala doc: http://www.scala-lang.org/api/current/#scala.beans.BeanProperty
Also you can take a look at the project Scalasti which is a interface for StringTemplate: http://software.clapper.org/scalasti/
It's built into the language (in an extendable way). Just
val obj = Obj("replacement")
s"xx ${obj.str} xx"
You can have any Scala expression inside ${...}.
See http://docs.scala-lang.org/overviews/core/string-interpolation.html (or just search for "Scala string interpolation") for more.
I am trying to build a form with Play Framework 2, the usual syntax is:
#helper.form(action = routes.Application.submit, 'id -> "myForm") {
}
Note that the single quotation mark is before id is opened and never closed.
Is there another syntax that I can use to do the same thing?
The 'id is a Symbol.
You could use the Symbol("sym") syntax if you don't like this one, but it is not standard.
scala> 'symbol == Symbol("symbol")
res0: Boolean = true
You could work around it with an implicit conversion. This will require using a scala source file, though (seems like you're using java, but you can mix them).
app/libs/SymbolImplicits.scala
package example.libs
object SymbolImplicits {
implicit def string2Symbol[A](s: (String, A)): (Symbol, A) = (Symbol(s._1), s._2)
}
Then in your view you would #import example.libs.SymbolImplicits._, so you can then do:
#helper.form(action = routes.Application.submit, "id" -> "myForm") {
}
"id" -> "myForm" is then implicitly converted to 'id -> "myForm".
To avoid using that import in every view, you could also add this line to build.sbt (or in Build.scala project properties) instead:
TwirlKeys.templateImports += "example.libs.SymbolImplicits._"
No, that's required syntax for Scala's Symbol as pointed in other answer, anyway except that it looks weird for it's perfectly valid and there's no reason to fight with it.
Assume i have lots of variables
String not_id, not_section, not_steet, not_sqTotal, not_sqLiving, not_sqKitchen, not_flat, not_floor, not_floors, not_text, user_phone1, user_phone2, user_contact, not_region, not_district, not_settle, not_price, not_photo, not_date, not_date_till, not_up, not_premium, not_status;
not_id= not_section= not_steet= not_sqTotal= not_sqLiving=
not_sqKitchen= not_flat= not_floor= not_floors= not_text=
user_phone1= user_phone2= user_contact= not_region= not_district=
not_settle= not_price= not_photo= not_date= not_date_till= not_up=
not_premium= not_status=region_title=district_title=settle_title=section_title="";
i need to change their values using someFunction
not_id = someFunction(not_id);
How can i do such action for all variables?
Please, dont propose to use arrays, lists and other sort of collections if it assumes changing variable names to some uniform name.
I van to know if there is such possibility within java itself, eclipse ide or eclipse plugins.
This is going to lead to somewhat unmaintainable code, but you could do it pretty easily with a regular expression search & replace on that second statement, replacing "var_name=" with "var_name = someFunction(var_name);".
Find: ([^=])+=
Replace with: \1 = someFunction(\1);
is someFunction a java method? or it is some simple transformer that is changing from not_([a-z])(\w*) to not[A-Z]\2? If you are only changing variable names use eclipse's excellent refactoring rename bound to Ctrl+Shift+R that can change all occurrences taking to account scope rules
I am trying to call a constructor for a custom collection object. This custom object takes in a parameter of type Class.
In java, this is done like this:
ICollection col = new PersistentCollection(ContentX.class);
This is my first dive into rhino, and I haven't been able to figure out quite how to pass this parameter. I figured out that "class" is a reserved word and therefor not usable.
I figured that I could get the Class from Class.forName like this:
importPackage(Packages.something.collections);
importPackage(Packages.something.content4);
var col = new PersistentCollection(Class.forName(ContentX));
But it just tosses ClassNotFoundException - with the fully qualified path something.content4.ContentX! So obviously it found the class or it wouldn't have known the path to it.
Am I doing it wrong? Sadly, I'm not in any position to change the java library right now, I need to fix the data without a new deploy.
Googling for javascript class just yields DOM/CSS problems.
I think you simply need to do:
var col = new PersistentCollection(ContentX);
Or, if your class name is a string:
var col = new PersistentCollection(
java.lang.Class.forName('something.content4.ContentX'));