hosseinmoein / DataFrame

C++ DataFrame for statistical, Financial, and ML analysis -- in modern C++ using native types and contiguous memory storage
https://hosseinmoein.github.io/DataFrame/
BSD 3-Clause "New" or "Revised" License
2.46k stars 310 forks source link

how to write get_data_by_sel in one line? #168

Closed xkungfu closed 2 years ago

xkungfu commented 2 years ago
    std::cout << "\nTesting get_data_by_sel() ..." << std::endl;

    std::vector<unsigned long>  idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456 };
    std::vector<double> d1 = { 1, 2, 3, 4, 5, 6, 7 };
    std::vector<double> d2 = { 8, 9, 10, 11, 12, 13, 14 };
    std::vector<double> d3 = { 15, 16, 17, 18, 19, 20, 21 };
    std::vector<double> d4 = { 22, 23, 24, 25 };
    std::vector<std::string> s1 = { "11", "22", "33", "ee", "ff", "gg", "ll" };
    MyDataFrame         df;

    df.load_data(std::move(idx),
                 std::make_pair("col_1", d1),
                 std::make_pair("col_2", d2),
                 std::make_pair("col_3", d3),
                 std::make_pair("col_str", s1));
    df.load_column("col_4", std::move(d4), nan_policy::dont_pad_with_nans);

    auto    functor =
        [](const unsigned long &, const double &val)-> bool { return (val >= 5); };
    auto    result =
        df.get_data_by_sel<double, decltype(functor), double, std::string>("col_1", functor);

how to merge the codes about selecting to one line like below?

auto result = df.get_data_by_sel<double, std::string>("col_1", [](const unsigned long &, const double &val)-> bool { return (val >= 5); });

hosseinmoein commented 2 years ago

Your one-liner looks good. The only thing is you are not specifying all the required types in the template type list. C++ is a statically typed language, so all types must be known at compile Time. So you need this:

df.get_data_by_sel<double, decltype(functor), double, std::string>("col_1", [](const unsigned long &, const double &val)-> bool { return (val >= 5); });

Again, without reading the document you are going to have hard time making this work

xkungfu commented 2 years ago

thank you! I think "decltype(functor)" would trigger error: "undeclared identifier 'functor'". how to remove or modify it?

hosseinmoein commented 2 years ago

typedef it in advance. In the first place, why do you need to this in one line?

xkungfu commented 2 years ago

"get_data_by_sel" is a common function often to use , so the codes would be not pretty if use it too many times. I just want know if there is a way to merge to one line. It is not necessary. you would continue to do the more important work than it.