Closed fledglings closed 5 years ago
Counts avg for even and odd elements:
int m[] = {2,5,4,15,6,25};
std::cout << boolinq::from(m)
.groupBy([](int a){return a%2;})
.select([](auto p){return p.second.avg();});
Result:
4 15
Method groupBy
returns std::pair
with first
key and second
Linq object.
Your exmaple should be something like this:
std::cout << boolinq::from(m)
.groupBy([](const User &a){return user.name + " " + std::to_string(user.day);})
.select([](auto p){return p.first + " " + p.second.avg(
[](const User &a){return a. time_on_phone;}
);});
Thanks very much k06a!
could the above be adjusted to return a std::set? for example, a key, value for dayNumber and average?
I experimented with .toStdSet() to no avail.
@fledglings if you wish to receive std::set<YourStructure>
you have to define operator <
for YourStructure
.
std::set<string>
:
User users[] = {
{"A", 1, 2},
{"B", 2, 5},
{"A", 1, 4},
{"B", 2, 15},
{"A", 1, 6},
{"B", 2, 25},
{"B", 3, 7},
};
std::cout << boolinq::from(
boolinq::from(users)
.groupBy([](User user){return "name: " + user.name + ", day: " + std::to_string(user.day);})
.select([](auto p){
return p.first + " => " + std::to_string(
p.second.avg([](const User & u){return u.time_on_phone;})
) + "\n";
})
.toStdSet()
);
Result:
name: A, day: 1 => 4
name: B, day: 2 => 15
@k06a Thanks!
Is it possible to groupBy, and then return the average for each group?
for example, this SQL query?