HiPipe  0.7.0
C++17 data pipeline with Python bindings.
example.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  * hipipe library
3  * Copyright (c) 2017, Cognexa Solutions s.r.o.
4  * Copyright (c) 2018, Iterait a.s.
5  * Author(s) Filip Matzner
6  *
7  * This file is distributed under the MIT License.
8  * See the accompanying file LICENSE.txt for the complete license agreement.
9  ****************************************************************************/
11 
12 #define BOOST_TEST_DYN_LINK
13 #define BOOST_TEST_MODULE example_test
14 
15 #include <hipipe/core.hpp>
16 
17 #include <boost/test/unit_test.hpp>
18 #include <range/v3/view/zip.hpp>
19 
20 #include <cassert>
21 #include <string>
22 #include <tuple>
23 #include <vector>
24 
25 BOOST_AUTO_TEST_CASE(test_example)
26 {
27  namespace hps = hipipe::stream;
28  using hps::from; using hps::to; using hps::by; using hps::dim;
29 
30  HIPIPE_DEFINE_COLUMN(login, std::string) // helper macro to define a column of strings
31  HIPIPE_DEFINE_COLUMN(age, int)
32 
33  std::vector<std::string> logins = {"marry", "ted", "anna", "josh"};
34  std::vector<int> ages = { 24, 41, 16, 59};
35 
36  auto stream = ranges::views::zip(logins, ages)
37  // create a batched stream out of the raw data
38  | hps::create<login, age>(2)
39  // make everyone older by one year
40  | hps::transform(from<age>, to<age>, [](int a) { return a + 1; })
41  // increase each letter in the logins by one (i.e., a->b, e->f ...)
42  | hps::transform(from<login>, to<login>, [](char c) { return c + 1; }, dim<2>)
43  // increase the ages by the length of the login
44  | hps::transform(from<login, age>, to<age>, [](std::string l, int a) {
45  return a + l.length();
46  })
47  // probabilistically rename 50% of the people to "buzz"
48  | hps::transform(from<login>, to<login>, 0.5, [](std::string) -> std::string {
49  return "buzz";
50  })
51  // drop the login column from the stream
52  | hps::drop<login>
53  // introduce the login column back to the stream
54  | hps::transform(from<age>, to<login>, [](int a) {
55  return "person_" + std::to_string(a) + "_years_old";
56  })
57  // filter only people older than 30 years
58  | hps::filter(from<login, age>, by<age>, [](int a) { return a > 30; })
59  // asynchronously buffer the stream during iteration
60  | hps::buffer(2);
61 
62  // extract the ages from the stream to std::vector
63  ages = hps::unpack(stream, from<age>);
64  std::vector<int> desired = {45, 64};
65  BOOST_TEST(ages == desired, boost::test_tools::per_element());
66  assert((ages == std::vector<int>{45, 64}));
67 }
hipipe::utility::to_string
std::string to_string(const T &value)
Convert the given type to std::string.
Definition: string.hpp:90
hipipe::stream::filter
auto filter(from_t< FromColumns... > f, by_t< ByColumns... > b, Fun fun, dim_t< Dim > d=dim_t< 1 >{})
Filter stream data.
Definition: filter.hpp:154
hipipe::stream::buffer
rgv::view< buffer_fn > buffer
Asynchronously buffers the given range.
Definition: buffer.hpp:195
HIPIPE_DEFINE_COLUMN
#define HIPIPE_DEFINE_COLUMN(column_name_, example_type_)
Macro for fast column definition.
Definition: column_t.hpp:250
hipipe::stream::unpack
auto unpack(Rng &&rng, from_t< FromColumns... > f, dim_t< Dim > d=dim_t< 1 >{})
Unpack a stream into a tuple of ranges.
Definition: unpack.hpp:118
hipipe::stream::transform
auto transform(from_t< FromColumns... > f, to_t< ToColumns... > t, Fun fun, dim_t< Dim > d=dim_t< 1 >{})
Transform a subset of hipipe columns to a different subset of hipipe columns.
Definition: transform.hpp:218