I believe I've found some possible snags, which seem to be related to Jorge's prior observations. I can't do import scala.math.random. While I can do import scala.math.random, I can't seem to call scala.math.random even so.
Another issue, how do we do reflection within main? My best attempt was probably this:
The most serious trouble relateds to numerous issues that occur in the spark.parallelize call I've attempted to bring over, and I believe this is an issue with interop with scala collections:
Here’s the Scala code
// Run spark job
val count = spark.parallelize(1 to n, slices).map { i =>
val x = random * 2 - 1
val y = random * 2 - 1
if (x*x + y*y < 1) 1 else 0
}.reduce(_ + _)
Here's my attempt at Kotlin:
// Run spark job
val count = spark.parallelize(1 to n, slices).map {
val x = scala.math.random() * 2 - 1
val y = scala.math.random() * 2 - 1
if (x*x + y*y < 1) 1 else 0
}.reduce{a: Int, b: Int -> a + b}
and here is what happens when I attempt to build and run
$ ~/gradle-2.4/bin/gradle runSpark -PsparkMain="com.cloudera.sa.kotlin.SparkPi.SparkPiPackage" -PskipHadoopJar -PsparkArgs="local[2] 100"
:compileAvro
:compileKotline: C:cygwin64homebrand_000spark-demosrcmainkotlincom.cloudera.saSparkPi.kt: (30, 23): Type inference failed: fun <T> parallelize(seq: scala.collection.Seq<T!>!, numSlices: kotlin.Int, `evidence$1`: scala.reflect.ClassTag<T!>!): org.apache.spark.rdd.RDD<T!>!
cannot be applied to
(kotlin.Pair<kotlin.Int, kotlin.Int>,kotlin.Int)
e: C:cygwin64homebrand_000spark-demosrcmainkotlincom.cloudera.saSparkPi.kt: (30, 37): Type inference failed. Expected type mismatch: found: kotlin.Pair<kotlin.Int, kotlin.Int> required: scala.collection.Seq<(???..???)>!
e: C:cygwin64homebrand_000spark-demosrcmainkotlincom.cloudera.saSparkPi.kt: (30, 49): No value passed for parameter evidence$1
e: C:cygwin64homebrand_000spark-demosrcmainkotlincom.cloudera.saSparkPi.kt: (31, 28): Unresolved reference: random
e: C:cygwin64homebrand_000spark-demosrcmainkotlincom.cloudera.saSparkPi.kt: (32, 28): Unresolved reference: random
e: C:cygwin64homebrand_000spark-demosrcmainkotlincom.cloudera.saSparkPi.kt: (34, 14): Cannot infer a type for this parameter. Please specify it explicitly.
e: C:cygwin64homebrand_000spark-demosrcmainkotlincom.cloudera.saSparkPi.kt: (34, 17): Cannot infer a type for this parameter. Please specify it explicitly.
FAILED
FAILURE: Build failed with an exception.
I put this code on a new branch since it doesn't compile.
Any feedback welcome! Also, I’m not dead set on using Kotlin right now. If this isn’t the right time for Kotlin-Scala interop, that’s fine, I understand. I think I can still become a better Kotlin programmer by learning Scala a bit more anyway, though I think the syntax may just be similar enough to make it interesting to switch between the two!