CodePlexProject Hosting for Open Source Software

The examples in this document are all written in C++11 and assume the query operators have been imported using the using clause:

using namespace cpplinq;

template<typename TPredicate> detail::where_builder<TPredicate> where (TPredicate predicate);

int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; auto c = from_array(numbers) >> where([](int i) {return i%2==0}) >> count();

detail::ref_builder ref ();

// ref_result is a std::vector<std::reference_wrapper<customer const>> auto ref_result = from_array (customers) >> ref () >> to_vector () ;

template<typename TPredicate> detail::select_builder<TPredicate> select (TPredicate predicate);

auto ids = from_array (customers) >> select ([](customer const & c){return c.id;}) >> to_vector ();

template<typename TPredicate> detail::select_many_builder<TPredicate> select_many (TPredicate predicate);

auto order_rows = from_array (customers) >> select_many ([](customer const & c) { return from (c.orders); }) >> select_many ([](order const & o) { return from (o.order_rows); }) >> to_vector () ;

detail::take_builder take (size_type count);

auto last_customers = from_array(customers) >> orderby_descending([](customer const &c) {return c.ID;}) >> take(5) >> to_vector();

template<typename TPredicate> detail::take_while_builder<TPredicate> take_while (TPredicate predicate);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto result = from_array(numbers) >> take_while([](int i) {return i < 5;}) >> to_vector();

detail::skip_builder skip (size_type count);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto result = from_array(numbers) >> skip(5) >> to_vector();

template <typename TPredicate> detail::skip_while_builder<TPredicate> skip_while (TPredicate predicate);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto result = from_array(numbers) >> skip_while([](int i) {return i<5;}) >> to_vector();

template<typename TOtherRange, typename TKeySelector, typename TOtherKeySelector, typename TCombiner> detail::join_builder<TOtherRange, TKeySelector, TOtherKeySelector, TCombiner> join ( TOtherRange other_range, TKeySelector key_selector, TOtherKeySelector other_key_selector, TCombiner combiner);

When the object returned by

The

The following examples shows how to join a sequence of customers and customers_addresses.

struct customer { std::size_t id ; std::string first_name ; std::string last_name ; }; struct customer_address { std::size_t id ; std::size_t customer_id ; std::string country ; }; customer customers[] = { customer (1 , "Bill" , "Gates" ), customer (2 , "Steve" , "Jobs" ), customer (3 , "Richard" , "Stallman"), customer (4 , "Linus" , "Torvalds"), }; customer_address customer_addresses[] = { customer_address (1, 1, "USA" ), customer_address (2, 4, "Finland" ), customer_address (3, 4, "USA" ), }; auto result = from_array(customers) >> join ( from_array(customer_addresses), [](customer const & c) {return c.id;}, [](customer_address const & ca) {return ca.customer_id;}, [](customer const & c, customer_address const & ca) {return std::make_pair (c, ca);}, ) >> to_vector();

template <typename TOtherRange> detail::concat_builder<TOtherRange> concat(TOtherRange other_range);

auto result = range(0,5) >> concat (range(5,5)) >> to_vector(); // yields {0,1,2,3,4,5,6,7,8,9}

from(source) >> orderby_xxx(..) >> thenby_xxx()

template<typename TPredicate> detail::orderby_builder<TPredicate> orderby (TPredicate predicate, bool sort_ascending = true); template<typename TPredicate> detail::orderby_builder<TPredicate> orderby_ascending (TPredicate predicate); template<typename TPredicate> detail::orderby_builder<TPredicate> orderby_descending (TPredicate predicate); template<typename TPredicate> detail::thenby_builder<TPredicate> thenby (TPredicate predicate, bool sort_ascending = true); template<typename TPredicate> detail::thenby_builder<TPredicate> thenby_ascending (TPredicate predicate); template<typename TPredicate> detail::thenby_builder<TPredicate> thenby_descending (TPredicate predicate);

auto sequence = from_array (customers) >> orderby ([] (customer const & c) {return c.last_name;}, true) >> thenby ([] (customer const & c) {return c.first_name;}, false) >> thenby_descending ([] (customer const & c) {return c.birthdate;}) >> to_vector ();

detail::reverse_builder reverse();

auto result = range(0,10) >> reverse() >> to_vector(); // result is {9,8,7,6,5,4,3,2,1,0}

detail::distinct_builder distinct();

int numbers[] = {5,4,3,2,1,2,3,4,5}; auto result = from_array(numbers) >> distinct() >> to_vector(); // yields {5,4,3,2,1}

template <typename TOtherRange> detail::union_builder<TOtherRange> union_with(TOtherRange other_range);

auto result = empty<int>() >> union_with(range(1,5)) >> to_vector(); // yields {1,2,3,4,5} int set1[] = {5,4,3,2,1,2,3,4,5}; int set2[] = {4,5,6,7}; auto set3 = from_array(set1) >> union_with(from_array(set2)) >> to_vector(); // yields {5,4,3,2,1,6,7} auto set4 = from_array(set2) >> union_with(from_array(set1)) >> to_vector(); // yields {4,5,6,7,3,2,1}

template <typename TOtherRange> detail::intersect_builder<TOtherRange> intersect_with(TOtherRange other_range);

auto set1 = empty<int>() >> intersect_with( range(0, 10) ) >> to_vector(); // yields {} auto set2 = range(0, 10) >> intersect_with( empty<int>() ) >> to_vector(); // yields {}

int set1[] = {5,4,3,2,1,2,3,4,5}; int set2[] = {4,5,6}; auto set3 = from_array(set1) >> intersect_with(from_array(set2)) >> to_vector(); // yields {5,4} auto set3 = from_array(set2) >> intersect_with(from_array(set1)) >> to_vector(); // yields {4,5}

template <typename TOtherRange> detail::except_builder<TOtherRange> except(TOtherRange other_range);

auto set1 = empty<int>() >> except( range(0, 10) ) >> to_vector(); // yields {} auto set2 = range(0, 10) >> except( empty<int>() ) >> to_vector(); // yields {0,1,2,3,4,5,6,7,8,9}

int set1[] = {5,4,3,2,1,2,3,4,5}; int set2[] = {4,5,6,7}; auto set3 = from_array(set1) >> except(from_array(set2)) >> to_vector(); // yields {3,2,1} auto set4 = from_array(set2) >> except(from_array(set1)) >> to_vector(); // yields {6,7}

detail::to_vector_builder to_vector (size_type capacity = 16U);

```
auto result = from_array(ints)
>> to_vector();
```

detail::to_list_builder to_list();

```
auto result = from_array(ints)
>> to_list();
```

template<typename TKeyPredicate> detail::to_map_builder<TKeyPredicate> to_map(TKeyPredicate key_predicate);

In the following examples a map is created from an array of customers. The key is represented by the ID of the customer. Notice that if there are more customers with the same ID the first one will be added to the map.

auto to_map_result = from_array (customers) >> to_map ([](customer const & c){return c.id;});

template<typename TKeyPredicate> detail::to_lookup_builder<TKeyPredicate> to_lookup(TKeyPredicate key_predicate);

The

customer_address customer_addresses[] = { customer_address (2, 4, "Finland" ), customer_address (3, 4, "USA" ), customer_address (1, 1, "USA" ), }; auto lookup = from_array (customer_addresses) >> to_lookup ([] (customer_address const & ca){return ca.customer_id;}); auto countries = lookup[4] >> select([](customer_address const & ca) {return ca.country;}) >> to_vector(); // yields {"Finland", "USA"}

template <typename TOtherRange> detail::sequence_equal_builder<TOtherRange> sequence_equal(TOtherRange other_range); template <typename TOtherRange, typename TComparer> detail::sequence_equal_predicate_builder<TOtherRange, TComparer> sequence_equal (TOtherRange other_range, TComparer comparer);

auto result1 = range(0,10) >> sequence_equal(range(0, 10)); // yields true int set1[] = {1,2,3,4,5}; int set2[] = {1,2,4,5}; auto result = from_array(set1) >> sequence_equal(from_array(set2)); // yields false

detail::first_builder first () template <typename TPredicate> detail::first_predicate_builder<TPredicate> first (TPredicate predicate);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto f1 = from_array(numbers) >> first(); // yields 3 auto f2 = from_array(numbers) >> first([](int i) {return i%2==0;}); // yields 4 auto f3 = from_array(numbers) >> first([](int i) {return i>9;}); // throws cpplinq::sequence_empty_exception

detail::first_or_default_builder first_or_default(); template <typename TPredicate> detail::first_or_default_predicate_builder<TPredicate> first_or_default (TPredicate predicate);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto f1 = from_array(numbers) >> first_or_default(); // yields 3 auto f2 = from_array(numbers) >> first_or_default([](int i) {return i%2==0;}); // yields 4 auto f3 = from_array(numbers) >> first_or_default([](int i) {return i>9;}); // yields 0

detail::last_or_default_builder last_or_default(); template <typename TPredicate> detail::last_or_default_predicate_builder<TPredicate> last_or_default(TPredicate predicate);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto l1 = from_array(numbers) >> last_or_default(); // yields 6 auto l2 = from_array(numbers) >> last_or_default([](int i) {return i%2==1;}); // yields 9 auto l3 = from_array(numbers) >> last_or_default([](int i) {return i>9;}); // yields 0

detail::element_at_or_default_builder element_at_or_default(size_type index);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto e1 = from_array(numbers) >> element_at_or_default(0); // yields 3 auto e2 = from_array(numbers) >> element_at_or_default(3); // yields 1 auto e3 = from_array(numbers) >> element_at_or_default(42); // yields 0

detail::int_range range(int start, int count);

The following example creates a vector of integers with values between 10 and 99.

auto start = 10; auto count = 90; auto r = range(start, count); >> to_vector();

template <typename TValue> detail::repeat_range<TValue> repeat (TValue element, int count);

The following example creates a list with 10 elements having the value "cpplinq".

auto r = repeat("cpplinq", 10) >> to_list();

template <typename TValue> detail::empty_range<TValue> empty();

```
auto customers = empty<customer>()
>> to_list();
```

template<typename TValue> detail::singleton_range<typename detail::cleanup_type<TValue>::type> singleton (TValue&& value)

auto result = singleton (1); // yields 1

template<typename TPredicate> detail::generate_range<TPredicate> generate (TPredicate predicate)

The

auto x = -1; auto result = generate ([&]() { return (++x < 3) ? to_opt (x) : to_opt<int> ();}) // yields 0, 1, 2

detail::any_builder any(); template <typename TPredicate> detail::any_predicate_builder<TPredicate> any(TPredicate predicate);

The enumeration of the source range is terminated as soon as the result is known.

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto a1 = from_array(numbers) >> any(); // yields true auto a2 = from_array(numbers) >> any([](int i) {return i>9;}); // yields false

template <typename TPredicate> detail::all_predicate_builder<TPredicate> all(TPredicate predicate);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto a1 = from_array(numbers) >> all([](int i) {return i%2==1;}); // yields false auto a2 = from_array(numbers) >> all([](int i) {return i<10;}); // yields true

template <typename TValue> detail::contains_builder<TValue> contains(TValue value); template <typename TValue, typename TPredicate> detail::contains_predicate_builder<TValue, TPredicate> contains(TValue value, TPredicate predicate);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto c = from_array(numbers) >> contains(1); // yields true bool result = from (customers) >> contains (customer(1, "Bill", "Gates"), [](customer const& c1, customer const& c2) {return c1.id == c2.id;});

detail::count_builder count(); template <typename TPredicate> detail::count_predicate_builder<TPredicate> count(TPredicate predicate);

The following example counts the number of even numbers in an array.

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto c = from_array(numbers) >> count([](int i) {return i%2==0;}); // yields 2

detail::sum_builder sum(); template<typename TSelector> detail::sum_selector_builder<TSelector> sum(TSelector selector);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto s1 = from_array(numbers) >> sum(); // yields 31 auto s2 = from_array(numbers) >> sum([](int i) {return i*i;}); // yields 173

detail::min_builder min(); template<typename TSelector> detail::min_selector_builder<TSelector> min(TSelector selector);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto m1 = from_array(numbers) >> min(); // yields 1 auto m2 = from_array(numbers) >> min([](int i) {return i+i;}); // yields 2

detail::max_builder max(); template<typename TSelector> detail::max_selector_builder<TSelector> max(TSelector selector);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto m1 = from_array(numbers) >> max(); // yields 9 auto m2 = from_array(numbers) >> min([](int i) {return i*i;}); // yields 81

detail::avg_builder avg(); template<typename TSelector> detail::avg_selector_builder<TSelector>avg(TSelector selector);

std::vector<int> numbers; auto a = from(numbers) >> avg(); // yields 0

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto a1 = from_array(numbers) >> avg(); // yields 3 auto a2 = from_array(numbers) >> avg([](int i) {return i*i;}); // yields 21

template <typename TAccumulate, typename TAccumulator> detail::aggregate_builder<TAccumulate, TAccumulator> aggregate(TAccumulate seed, TAccumulator accumulator); template <typename TAccumulate, typename TAccumulator, typename TSelector> detail::aggregate_result_selector_builder<TAccumulate, TAccumulator, TSelector> aggregate( TAccumulate seed, TAccumulator accumulator, TSelector result_selector);

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto s = from_array(numbers) >> aggregate(0, [](int s, int i) {return s+i;}); // yields 31 auto p = from_array(numbers) >> aggregate(1, [](int s, int i) {return s*i;}); // yields 6480

detail::pairwise_builder pairwise();

The

int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; auto pairs = from_array(numbers) >> pairwise(); // yields (3,1), (1,4), (4,1), (1,5), (5,9), (9,2), (2,6)

template <typename TOtherRange> detail::zip_with_builder<TOtherRange> zip_with (TOtherRange other_range)

The

int a[] = {3, 1, 4}; int b[] = {5, 9, 2, 7}; auto zipped = from_array(a) >> zip_with(from_array(b)); // yields (3,5), (1,9), (4,2)

Last edited May 30, 2014 at 9:49 AM by marten_range, version 25