Quick reference for the range-v3 library

This page serves as a quick reference for the range-v3 library to get you started. It is being continuosly updated. To try out the library, add #include <range/v3/all.hpp> in https://wandbox.org/. Haskell equivalents are included where applicable (import Prelude, import Data.List) .


accumulate

Ex.1 Sum up elements of a sequence, starting from the value 0.0.

    // (a -> b -> a) -> a -> [b] -> a
    // foldl (+) 0.0 [1.5,2.7,3.8,4.2]
    std::vector<double> v = {1.5,2.7,3.8,4.2};
    auto val = ranges::accumulate(v, 0.0); // 12.2

Ex.2 Fold elements of a sequence via multiplication, starting from the value 1.

    // (a -> b -> a) -> a -> [b] -> a
    // foldl (*) 1 [1,2,3,4]
    std::vector<int> v = {1,2,3,4};
    auto val = ranges::accumulate(v, 1, [](int a, int b){return a * b;}); // 24

count

Ex.1 Count the number of occurrences of number 7.

    std::vector<int> v = {1,2,7,4,1,7};
    auto val = ranges::count(v,7); // 2

equal

Ex.1 Determine equality of two vectors.

    // [1, 2, 3] == [3, 2, 1]
    std::vector<int> v1 = {1,2,3};
    std::vector<int> v2 = {3,2,1};   
    auto val = ranges::equal(v1,v2); // false

Ex.2 Determine equality of two strings.

    std::string s1 = "world";
    std::string s2 = "WORLD";
    auto val = ranges::equal(s1,s2); // false

inner_product

Ex.1 Calculate the dot product of two vectors.

    const std::vector<int> v1 = {1,3,-5};
    const std::vector<int> v2 = {4,-2,-1};
    auto val = ranges::inner_product(v1,v2,0); // 1*4 + 3*(-2) + (-5)*(-1) = 3

sort

Ex.1 Sort elements of a sequence in ascending order.

    // Ord a => [a] -> [a]
    // sort [6,7,1,3]
    std::vector<int> v = {6,7,1,3};
    ranges::sort(v); // [1,3,6,7]

Ex.2 Sort elements of a sequence in descending order.

    // Ord a => [a] -> [a]
    // sort [6,7,1,3]
    std::vector<int> v = {6,7,1,3};
    ranges::sort(v, std::greater<>()); // [7,6,3,1]

view::all

Ex.1 Create a range containing all the elements of the sequence. Useful for conversion.

    // [a] -> [a]
    std::vector<int> v = {1,2,3,4,5};
    auto rng = v | ranges::view::all; // [1,2,3,4,5]
    std::cout << rng;

view::cartesian_product

Ex.1 Create a vector of tuples, i.e. a cartesian product of two sequences.

    const std::vector<char> letters = {'A', 'B', 'C'};
    const std::vector<int> numbers = {1, 2};	
    
    auto rng = ranges::view::cartesian_product(letters, numbers);
    for(auto&&[a,b] : rng)
        std::cout << a << ' ' << b << '\n';
// A 1
// A 2
// B 1
// B 2
// C 1
// C 2

view::cycle

Ex.1 Create a quasi-infinite circular sequence from a finite sequence.

    // [a] -> [a]
    // cycle [1,3,9]
    std::vector<int> v = {1,3,9};
    auto rng = v | ranges::view::cycle; // [1,3,9,1,3,9,1,3,9...]

view::drop

Ex.1 Create a sequence by dropping the first two elements of the original sequence.

    // Int -> [a] -> [a]
    // drop 2 [1,2,3,4,5]
    std::vector<int> v = {1,2,3,4,5};
    auto rng = v | ranges::view::drop(2); // [3,4,5]

view::for_each

Ex.1 Print all the elements of a sequence.

    std::vector<int> v = {1,2,3,4};
    ranges::for_each(v, [](int i){ std::cout << i  << " "; });

Ex.2 Generate i+1 and i-1 for every element of a sequence and flatten the result (flatmap).

    std::vector<int> v = {1,10,100};
    std::vector<int> rng =
    ranges::view::for_each(v, [](int i){
        return ranges::view::linear_distribute(i-1,i+1,2);
    }); // [0,2,9,11,99,101]

view::generate

Ex.1 Generate an infinite sequence of positive even numbers.

    //    [2,4..]
    auto rng = ranges::view::generate(
                     [n = 1] () mutable { return 2*(n++); }); // [2,4,6,8...]

Ex.2 Generate an infinite sequence of powers of two.

    // (a -> a) -> a -> [a]
    // iterate (2*) 1
    auto rng = ranges::view::generate(
                     [n = 1] () mutable { return (n <<= 1) >> 1; }); // [1,2,4,8...]

view::ints

Ex.1 Create a quasi-infinite sequence of integers.

    // [0..]
    auto rng = ranges::view::ints; // [0,1,2,3,4...]

Ex.2 Create a quasi-infinite sequence of integers [4,∞).

    // [4..]
    auto rng = ranges::view::ints(4); // [4,5,6,7...]

Ex.3 Create a sequence of integers [4,5,6].

    // [4..6]
    auto rng = ranges::view::ints(4,7); // [4,5,6]

view::iota

Ex.1 Create a sequence of integers [8,9,10].

    // [8..10]
    auto rng = ranges::view::iota(8,11); // [8,9,10]

Ex.2 Create a sequence of letters [A,B,C,D,E].

    // ['A'..'E']
    auto rng = ranges::view::iota('A','F'); // [A,B,C,D,E]

view::join

Ex.1 Flatten a list of lists

   // [[a]] -> [a]
   // concat [[1,3], [11,13,15], [25]]   
   const auto v = std::vector<std::vector<int>>{
                 {1,3},
                 {11,13,15},
                 {25},
   };
   auto rng = v | ranges::view::join; // [1,3,11,13,15,25]

view::linear_distribute

Ex.1 Create a sequence of monotonically increasing values between 1.0 and 2.0 with a step 0.25.

   auto rng = ranges::view::linear_distribute(1.0,2.0,5); // [1,1.25,1.5,1.75,2]

view::partial_sum

Ex.1 Create a sequence by cumulatively adding elements of another sequence.

    // [a] -> [a]
    // scanl1 (+) [1,2,3,4]    
    const std::vector<int> v = {1,2,3,4};
    auto rng = v | ranges::view::partial_sum(std::plus<>()); // [1,3,6,10]

view::remove_if

Ex.1 Create a sequence by removing even elements from the original sequence.

    // (a -> Bool) -> [a] -> [a]
    // filter odd [1,2,3,4,5]    
    const auto v = std::vector<int>{1,2,3,4,5};
    auto rng = v | ranges::view::remove_if(
                         [](int x){return x % 2 == 0;}); // [1,3,5]

view::repeat

Ex.1 Create a quasi-infinite sequence with all elements equal to 4.

    // a -> [a]
    // repeat 4
    auto rng = ranges::view::repeat(4); // [4,4,4,4,4,4,4,..]

view::reverse

Ex.1 Create a sequence by reversing the original sequence.

    // [a] -> [a]
    // reverse [1,2,3,4]
    const auto v = std::vector<int>{1,2,3,4};
    auto rng = v | ranges::view::reverse; // [4,3,2,1]

view::set_difference

Ex.1 Create a sequence as a difference of two input sequences.

    // Eq a => [a] -> [a] -> [a]
    // [3,4,5,6,7] \\ [4,5]
    std::vector<int> v1 = {3,4,5,6,7};
    std::vector<int> v2 = {4,5};
    auto rng = ranges::view::set_difference(v1,v2); // [3,6,7]

view::set_intersection

Ex.1 Create a sequence as an intersection of two input sequences.

    // Eq a => [a] -> [a] -> [a]
    // intersect [3,4,5,6] [5,6,7,8] 
    std::vector<int> v1 = {3,4,5,6};
    std::vector<int> v2 = {5,6,7,8};
    auto rng = ranges::view::set_intersection(v1,v2); // [5,6]

view::set_union

Ex.1 Create a sequence as a union of two input sequences.

    // Eq a => [a] -> [a] -> [a]
    // union [1,2,3] [4,5,6] 
    std::vector<int> v1 = {1,2,3};
    std::vector<int> v2 = {4,5,6};
    auto rng = ranges::view::set_union(v1,v2); // [1,2,3,4,5,6]

view::stride

Ex.1 Create a sequence using every third element of the original sequence.

    const auto v = std::vector<int>{0,1,2,3,4,5,6};
    auto rng = v | ranges::view::stride(3); // [0,3,6]

view::tail

Ex.1 Create a sequence without the first element of the original sequence.

    // [a] -> [a]
    // tail [1,2,3,4]
    const auto v = std::vector<int>{1,2,3,4};
    auto rng = v | ranges::view::tail; // [2,3,4]

view::take

Ex.1 Create a sequence that contains the first three elements of the original sequence.

    // Int -> [a] -> [a]
    // take 3 [1,2,3,4,5]
    std::vector<int> v = {1,2,3,4,5};
    auto rng = v | ranges::view::take(3); // [1,2,3]

view::transform

Ex.1 Multiply each element of the sequence by 2.

    // (a -> b) -> [a] -> [b]
    // map (2*) [3,2,1] 
    const auto v = std::vector<int> {3,2,1};
    auto rng = v | ranges::view::transform(
                         [](int x){return 2*x;}); // [6,4,2]

view::unique

Ex.1 Create a sequence by removing duplicate neighbouring elements of the original sequence.

    // Eq a => [a] -> [a]
    std::vector<int> v = {1,2,2,3,1,1,2,2};
    auto rng = v | ranges::view::unique; // [1,2,3,1,2]

view::zip_with

Ex.1 Join two sequences by applying addition.

    // (a -> b -> c) -> [a] -> [b] -> [c]
    // zipWith (+) [4,2,7] [3,2,1]
    const std::vector<int> v1 = {4,2,7};
    const std::vector<int> v2 = {3,2,1};
    auto rng = ranges::view::zip_with(
                     [](int a, int b){return a + b;}, v1, v2); // [7,4,8]

© Walletfox.com, 2018