Just do one thing, before your objects passes through the closure do Serialize them, and then, de-serialize them afterwards. This will work even if your classes aren't Serializable, because it uses Kryo.
Here's an example of how I did it:
def genMapper(kryoWrapper: KryoSerializationWrapper[(Foo => Bar)]) (foo: Foo) : Bar = { kryoWrapper.value.apply(foo) } val mapper = genMapper(KryoSerializationWrapper(new Blah(abc))) _ rdd.flatMap(mapper).collectAsMap() object Blah(abc: ABC) extends (Foo => Bar) { def apply(foo: Foo) : Bar = { //This is the real function } }
If you want to know more about Scala, then do check out this awesome video tutorial: