# Put lambdas of different signatures in a vector for RDataFrame

**URL:** <https://root-forum.cern.ch/t/put-lambdas-of-different-signatures-in-a-vector-for-rdataframe/49619>\
**Category:** ROOT\
**Created:** [April 19, 2022, 7:51am UTC](https://root-forum.cern.ch/t/put-lambdas-of-different-signatures-in-a-vector-for-rdataframe/49619 "2022-04-19T07:51:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yipengsun](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yipengsun/32/22782_2.png) [@yipengsun](https://root-forum.cern.ch/u/yipengsun)\
**Post date:** [April 19, 2022, 7:51am UTC](https://root-forum.cern.ch/t/put-lambdas-of-different-signatures-in-a-vector-for-rdataframe/49619/1 "2022-04-19T07:51:12Z")

</div>

Dear experts,

In this post, [Running RDataFrame's Define In For Loop](https://root-forum.cern.ch/t/running-rdataframes-define-in-for-loop/32484), an example of putting expressions-as-strings in a vector then define dataframes based on the expressions in a loop is provided.

I’m wondering if I can do something like this:

```auto
auto lambdas = vector<F>{[](double x) { return x; }, [](double x, double y) { return x+y; }};
auto names = vector<string>{"a", "b"};
auto params = vector<vector<string>> {{"x"}, {"x", "y"}};

initDf = RDataFrame("tree", "ntp.root");

auto applyDefine(RNode df, vector<F> lambdas, vector<string> names, vector<vector<string> params, int idx=0) {
  if (i == names.size()) return df;

  return applyDefines(df.Define(names[i], lambdas[i], params[i]), lambdas, names, paras, i + 1);
}

applyDefine(initDf, names, lambdas, params);

```

Also, in the doc [ROOT: ROOT::RDF::RInterface\< Proxied, DataSource \> Class Template Reference](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a4698601205a55ac49279150d56fc904f), the function pointer is of type `F`, but I think the doc doesn’t link the definition of `F` properly, as it links to a macro in a `md5_*` file. Could you provide some hint on the implementation of `F`?

Thanks.

* * *

_ROOT Version:_ 6.24.2  
_Platform:_ Linux  
_Compiler:_ 10.3.0

* * *

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [April 19, 2022, 8:03am UTC](https://root-forum.cern.ch/t/put-lambdas-of-different-signatures-in-a-vector-for-rdataframe/49619/2 "2022-04-19T08:03:55Z")

</div>

Hi @yipengsun ,  
In C++ each lambda has its own different type which only the compiler knows, so you cannot create a `std::vector<lambda>` container. You can create a vector of std::function and then pass lambdas which will be converted into the function type

```cpp
std::vector<std::function<double()>> functions;
functions.push_back([]{ return 1.; });

```

But that only works with functions of the _same_ signature, since std::vector can only contain objects of the same type. From your example above I see already two different signatures, so that wouldn’t be possible at all.

As for the `F` type you see in the documentation, that’s just the typename of the template parameter, a generic callable type that you may pass to the `Define` operation. That includes a lambda, a function, an std::function, a class that has an `operator()` .  
Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![yipengsun](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yipengsun/32/22782_2.png) [@yipengsun](https://root-forum.cern.ch/u/yipengsun)\
**Post date:** [April 19, 2022, 8:14am UTC](https://root-forum.cern.ch/t/put-lambdas-of-different-signatures-in-a-vector-for-rdataframe/49619/3 "2022-04-19T08:14:51Z")

</div>

I see. So the only way to make it work would be to declare these functions of different signatures in `gInterepreter`, and call them by the string (say, `"func(x)"`), then these Define’s will be JIT’ed at run-time, by the interpreter (even if the source code is compiled).

Is my understanding right?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 19, 2022, 8:35am UTC](https://root-forum.cern.ch/t/put-lambdas-of-different-signatures-in-a-vector-for-rdataframe/49619/4 "2022-04-19T08:35:23Z")

</div>

Hi @yipengsun ,  
In C++17, with some template trickery, you can use `std::tuple` and fold expressions instead of a vector of lambdas and a for loop. Example coming.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 19, 2022, 8:44am UTC](https://root-forum.cern.ch/t/put-lambdas-of-different-signatures-in-a-vector-for-rdataframe/49619/5 "2022-04-19T08:44:39Z")

</div>

You can write e.g. this:

```cpp
int main() {
  std::tuple funcs{[] { return 42; }, [](ULong64_t) { return 1; }};

  ROOT::RDataFrame df(10);
  std::vector<std::vector<std::string>> inputs{{}, {"rdfentry_"}};
  std::vector<std::string> outputs{{"x"}, {"y"}};

  auto df2 = ApplyDefines(df, outputs, funcs, inputs);

  df2.Display()->Print();
}

```

where `ApplyDefines` can be implemented e.g. like this:

```cpp
#include <ROOT/RDataFrame.hxx>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

template <typename Funcs, std::size_t... Idx>
auto ApplyDefinesHelper(ROOT::RDF::RNode df,
                        const std::vector<std::string> &outputs, Funcs &funcs,
                        const std::vector<std::vector<std::string>> &inputs,
                        std::index_sequence<Idx...>) {
  auto applyOne = [&](auto f, int i) {
    df = df.Define(outputs[i], f, inputs[i]);
  };

  (applyOne(std::get<Idx>(funcs), Idx), ...);

  return df;
}

template <typename Funcs>
auto ApplyDefines(ROOT::RDF::RNode df, const std::vector<std::string> &outputs,
                  Funcs &funcs,
                  const std::vector<std::vector<std::string>> &inputs) {
  constexpr auto indices = std::make_index_sequence<std::tuple_size<Funcs>{}>();
  return ApplyDefinesHelper(df, outputs, funcs, inputs, indices);
}

```

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 3, 2022, 8:45am UTC](https://root-forum.cern.ch/t/put-lambdas-of-different-signatures-in-a-vector-for-rdataframe/49619/6 "2022-05-03T08:45:15Z")

</div>

Closed on May 3, 2022, 8:45am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
