Projection Operators - cemderv/linq GitHub Wiki

select()

Takes an element from the range and returns a new element using a given transform function. The returned element can be of any type but void.

Signature:

template <typename TTransform>
range select(const TTransform& transform) const;

Example:

const vector<string> words { "some", "example", "words" };

// Select the first letter of every word.
auto query = linq::from(&words)
                  .select( [](const string& word) { return word.at(0); } );
                  
for (const char letter : query) {
    print("{} ", letter);
}

Output: s e w


select_to_string()

A variation of the select operator that applies std::to_string to the range's elements, thus producing values of type std::string.

Signature:

range select_to_string() const;

Example:

const vector numbers { 1, 2, 3 };

auto query = linq::from(&numbers).select_to_string();

for (const string& s : query) {
    print("{} ", s);
}

Output: 1 2 3


select_many()

Selects a range from each element and subsequently combines them:

Signature:

template <typename TTransform>
range select_many(const TTransform& transform) const;

Example:

struct Person {
    vector<int> favorite_numbers;
};

const vector<Person> people {
    { .favorite_numbers = { 1, 2, 3, 4 } },
    { .favorite_numbers = { 5, 6, 7, 8 } },
    { .favorite_numbers = { 9, 10, 11, 12 } },
};
    
auto query = linq::from(&people)
                  .select_many( [](const Person& p) {
                      // select_many expects a range to be returned.
                      return linq::from(&p.favorite_numbers);
                  });
                      
for (const int number : query) {
    print("{} ", number);
}

Output: 1 2 3 4 5 6 7 8 9 10 11 12

⚠️ **GitHub.com Fallback** ⚠️