# Execute lambda remotely

**URL:** <https://discuss.kotlinlang.org/t/execute-lambda-remotely/14956>\
**Category:** Support\
**Created:** [November 9, 2019, 9:24am UTC](https://discuss.kotlinlang.org/t/execute-lambda-remotely/14956 "2019-11-09T09:24:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmytro.tolstyi](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dmytro.tolstyi](https://discuss.kotlinlang.org/u/dmytro.tolstyi)\
**Post date:** [November 9, 2019, 9:24am UTC](https://discuss.kotlinlang.org/t/execute-lambda-remotely/14956/1 "2019-11-09T09:24:27Z")

</div>

Hi,

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):  
**Sender**

```auto
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?

---

<div class="post-metadata">

**Author:** ![dmytro.tolstyi](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dmytro.tolstyi](https://discuss.kotlinlang.org/u/dmytro.tolstyi)\
**Post date:** [November 9, 2019, 3:30pm UTC](https://discuss.kotlinlang.org/t/execute-lambda-remotely/14956/2 "2019-11-09T15:30:48Z")

</div>

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

```auto
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
