Execute lambda remotely


I got a cluster and I would like to execute lambda function no matter if it’s a local instance or a remote one.
Lambda seems a good candidate as far as it’s Searializable by default.
So, my idea is to serialize lambda, pass it to instance and execute it.

Is it possible somehow to pass lambda and params at the same time or pass lambda and force it to take params from the context where it should be executed?

Here’s an example of what I’m trying to achieve (pseudo code):

val fun1: () -> Unit = {
    println("Fun without params")

val fun2: (s: String) -> String = {
    "Modified [$it]"

If we receive one of these lambdas at Receiver side how can we execute them in a generic way no matter what params should be provided?

Looks like the solution was quite easy and obvious: instead of making lambda with params we put them inside directly:

val fun = () -> Any = {
    // Your code and processing with variables

Like this we can serialize fun and it can use any kind of serializable variables in it’s body