c++ - Best way to do flattening of nested getter result inside getter -
sometimes have task of finding if nested getter inside value returned getter has property. classic c++ like:
for (const auto& label: labels) (const auto& artist: label.artists()) if (artist.name = "arcade fire") return true; return false;
what best way ranges? think may work:
labels| transform (&label::artists) | join | transform(&artist::name) | join | find_if_fn([](){/*...*/}) ;
but quite long, (partially because instead of .member_fn must write class:member_fn. there shorter way this?
i think this:
using namespace ranges; auto rng = labels | view::transform(&label::artists) | view::join; return find(rng, "arcade fire", &artist::name) != end(rng);
gets job done in straightforward way. view::filter
formulation:
using namespace ranges; auto rng = labels | view::transform(&label::artists) | view::join | view::filter([](const artist& a){ return a.name() == "arcade fire"; }); return !empty(rng);
is bit wordier, has similar performance. it's clear how generalize "is there foo?" "return of foos."
Comments
Post a Comment