}
};
+/**
+ * Contains - For testing whether a value matching the given value is contained
+ * in a sequence.
+ *
+ * This type should be used through the 'contains' helper method, like:
+ *
+ * bool contained = seq(1, 10) | map(square) | contains(49);
+ */
+template<class Needle>
+class Contains : public Operator<Contains<Needle>> {
+ Needle needle_;
+ public:
+ explicit Contains(Needle needle)
+ : needle_(std::move(needle))
+ {}
+
+ template<class Source,
+ class Value,
+ class StorageType = typename std::decay<Value>::type>
+ bool compose(const GenImpl<Value, Source>& source) const {
+ return !(source | [this](Value value) {
+ return !(needle_ == std::forward<Value>(value));
+ });
+ }
+};
+
/**
* Min - For a value which minimizes a key, where the key is determined by a
* given selector, and compared by given comparer.
/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
template<class Value>
class GeneratorBuilder {};
+template<class Needle>
+class Contains;
+
}
/**
return Append(&collection);
}
+template<class Needle,
+ class Contains = detail::Contains<typename std::decay<Needle>::type>>
+Contains contains(Needle&& needle) {
+ return Contains(std::forward<Needle>(needle));
+}
+
}} // folly::gen
#include "folly/experimental/Gen-inl.h"
EXPECT_EQ(expected, actual);
}
+TEST(Gen, Contains) {
+ {
+ auto gen =
+ seq(1, 9)
+ | map(square);
+ EXPECT_TRUE(gen | contains(49));
+ EXPECT_FALSE(gen | contains(50));
+ }
+ {
+ auto gen =
+ seq(1) // infinite, to prove laziness
+ | map(square)
+ | eachTo<std::string>();
+
+ // std::string gen, const char* needle
+ EXPECT_TRUE(gen | contains("49"));
+ }
+}
+
TEST(Gen, Take) {
auto expected = vector<int>{1, 4, 9, 16};
auto actual =