Esempio n. 1
0
int main() {
    std::vector<int> ivec{1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1};
    for (auto i : takewhile([] (int i) {return i < 5;}, ivec)) {
        std::cout << i << '\n';
    }

    for (auto i : takewhile([] (int i) {return i < 5;}, range(10))) {
        std::cout << i << '\n';
    }

    for (auto i : takewhile([] (int i) {return i < 5;}, 
                {1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    std::cout << "with temporary\n";
    for (auto i : takewhile([] (int i) {return i < 5;}, 
                std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    return 0;
}
Esempio n. 2
0
  bool under_ten(int i) {
    return i < 10;
  }

  struct UnderTen {
    bool operator()(int i) {
      return i < 10;
    }
  };
}

TEST_CASE("takewhile: works with lambda, callable, and function pointer",
    "[takewhile]") {
  Vec ns = {1, 3, 5, 20, 2, 4, 6, 8};
  SECTION("function pointer") {
    auto tw = takewhile(under_ten, ns);
    Vec v(std::begin(tw), std::end(tw));
    Vec vc = {1, 3, 5};
    REQUIRE(v == vc);
  }

  SECTION("callable object") {
    std::vector<int> v;
    SECTION("Normal call") {
      auto tw = takewhile(UnderTen{}, ns);
      v.assign(std::begin(tw), std::end(tw));
    }

    SECTION("Pipe") {
      auto tw = ns | takewhile(UnderTen{});
      v.assign(std::begin(tw), std::end(tw));