2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wshadow"
21 namespace folly { namespace gen {
24 * IsCompatibleSignature - Trait type for testing whether a given Functor
25 * matches an expected signature.
28 * IsCompatibleSignature<FunctorType, bool(int, float)>::value
30 template<class Candidate, class Expected>
31 class IsCompatibleSignature {
32 static constexpr bool value = false;
35 template<class Candidate,
38 class IsCompatibleSignature<Candidate, ExpectedReturn(ArgTypes...)> {
41 decltype(std::declval<F>()(std::declval<ArgTypes>()...)),
42 bool good = std::is_same<ExpectedReturn, ActualReturn>::value>
43 static constexpr bool testArgs(int* p) {
48 static constexpr bool testArgs(...) {
52 static constexpr bool value = testArgs<Candidate>(nullptr);
56 * ArgumentReference - For determining ideal argument type to receive a value.
59 struct ArgumentReference :
60 public std::conditional<std::is_reference<T>::value,
61 T, // T& -> T&, T&& -> T&&, const T& -> const T&
62 typename std::conditional<
63 std::is_const<T>::value,
64 T&, // const int -> const int&
69 * FBounded - Helper type for the curiously recurring template pattern, used
70 * heavily here to enable inlining and obviate virtual functions
74 const Self& self() const {
75 return *static_cast<const Self*>(this);
79 return *static_cast<Self*>(this);
84 * Operator - Core abstraction of an operation which may be applied to a
85 * generator. All operators implement a method compose(), which takes a
86 * generator and produces an output generator.
89 class Operator : public FBounded<Self> {
92 * compose() - Must be implemented by child class to compose a new Generator
93 * out of a given generator. This function left intentionally unimplemented.
95 template<class Source,
97 class ResultGen = void>
98 ResultGen compose(const GenImpl<Value, Source>& source) const;
101 Operator() = default;
102 Operator(const Operator&) = default;
103 Operator(Operator&&) = default;
107 * operator|() - For composing two operators without binding it to a
108 * particular generator.
112 class Composed = detail::Composed<Left, Right>>
113 Composed operator|(const Operator<Left>& left,
114 const Operator<Right>& right) {
115 return Composed(left.self(), right.self());
120 class Composed = detail::Composed<Left, Right>>
121 Composed operator|(const Operator<Left>& left,
122 Operator<Right>&& right) {
123 return Composed(left.self(), std::move(right.self()));
128 class Composed = detail::Composed<Left, Right>>
129 Composed operator|(Operator<Left>&& left,
130 const Operator<Right>& right) {
131 return Composed(std::move(left.self()), right.self());
136 class Composed = detail::Composed<Left, Right>>
137 Composed operator|(Operator<Left>&& left,
138 Operator<Right>&& right) {
139 return Composed(std::move(left.self()), std::move(right.self()));
143 * GenImpl - Core abstraction of a generator, an object which produces values by
144 * passing them to a given handler lambda. All generator implementations must
145 * implement apply(). foreach() may also be implemented to special case the
146 * condition where the entire sequence is consumed.
148 template<class Value,
150 class GenImpl : public FBounded<Self> {
152 // To prevent slicing
154 GenImpl(const GenImpl&) = default;
155 GenImpl(GenImpl&&) = default;
158 typedef Value ValueType;
159 typedef typename std::decay<Value>::type StorageType;
162 * apply() - Send all values produced by this generator to given
163 * handler until the handler returns false. Returns false if and only if the
164 * handler returns false. Note: It should return true even if it completes
165 * (without the handler returning false), as 'Chain' uses the return value of
166 * apply to determine if it should process the second object in its chain.
168 template<class Handler>
169 bool apply(Handler&& handler) const;
172 * foreach() - Send all values produced by this generator to given lambda.
175 void foreach(Body&& body) const {
176 this->self().apply([&](Value value) -> bool {
177 static_assert(!infinite, "Cannot call foreach on infinite GenImpl");
178 body(std::forward<Value>(value));
183 // Child classes should override if the sequence generated is *definitely*
184 // infinite. 'infinite' may be false_type for some infinite sequences
185 // (due the the Halting Problem).
186 static constexpr bool infinite = false;
189 template<class LeftValue,
193 class Chain = detail::Chain<LeftValue, Left, Right>>
194 Chain operator+(const GenImpl<LeftValue, Left>& left,
195 const GenImpl<RightValue, Right>& right) {
197 std::is_same<LeftValue, RightValue>::value,
198 "Generators may ony be combined if Values are the exact same type.");
199 return Chain(left.self(), right.self());
202 template<class LeftValue,
206 class Chain = detail::Chain<LeftValue, Left, Right>>
207 Chain operator+(const GenImpl<LeftValue, Left>& left,
208 GenImpl<RightValue, Right>&& right) {
210 std::is_same<LeftValue, RightValue>::value,
211 "Generators may ony be combined if Values are the exact same type.");
212 return Chain(left.self(), std::move(right.self()));
215 template<class LeftValue,
219 class Chain = detail::Chain<LeftValue, Left, Right>>
220 Chain operator+(GenImpl<LeftValue, Left>&& left,
221 const GenImpl<RightValue, Right>& right) {
223 std::is_same<LeftValue, RightValue>::value,
224 "Generators may ony be combined if Values are the exact same type.");
225 return Chain(std::move(left.self()), right.self());
228 template<class LeftValue,
232 class Chain = detail::Chain<LeftValue, Left, Right>>
233 Chain operator+(GenImpl<LeftValue, Left>&& left,
234 GenImpl<RightValue, Right>&& right) {
236 std::is_same<LeftValue, RightValue>::value,
237 "Generators may ony be combined if Values are the exact same type.");
238 return Chain(std::move(left.self()), std::move(right.self()));
242 * operator|() which enables foreach-like usage:
243 * gen | [](Value v) -> void {...};
245 template<class Value,
248 typename std::enable_if<
249 IsCompatibleSignature<Handler, void(Value)>::value>::type
250 operator|(const GenImpl<Value, Gen>& gen, Handler&& handler) {
251 static_assert(!Gen::infinite,
252 "Cannot pull all values from an infinite sequence.");
253 gen.self().foreach(std::forward<Handler>(handler));
257 * operator|() which enables foreach-like usage with 'break' support:
258 * gen | [](Value v) -> bool { return shouldContinue(); };
260 template<class Value,
263 typename std::enable_if<
264 IsCompatibleSignature<Handler, bool(Value)>::value, bool>::type
265 operator|(const GenImpl<Value, Gen>& gen, Handler&& handler) {
266 return gen.self().apply(std::forward<Handler>(handler));
270 * operator|() for composing generators with operators, similar to boosts' range
272 * gen | map(square) | sum
274 template<class Value,
277 auto operator|(const GenImpl<Value, Gen>& gen, const Operator<Op>& op) ->
278 decltype(op.self().compose(gen.self())) {
279 return op.self().compose(gen.self());
282 template<class Value,
285 auto operator|(GenImpl<Value, Gen>&& gen, const Operator<Op>& op) ->
286 decltype(op.self().compose(std::move(gen.self()))) {
287 return op.self().compose(std::move(gen.self()));
293 * ReferencedSource - Generate values from an STL-like container using
294 * iterators from .begin() until .end(). Value type defaults to the type of
295 * *container->begin(). For std::vector<int>, this would be int&. Note that the
296 * value here is a reference, so the values in the vector will be passed by
297 * reference to downstream operators.
299 * This type is primarily used through the 'from' helper method, like:
301 * string& longestName = from(names)
302 * | maxBy([](string& s) { return s.size() });
304 template<class Container,
306 class ReferencedSource :
307 public GenImpl<Value, ReferencedSource<Container, Value>> {
308 Container* container_;
310 explicit ReferencedSource(Container* container)
311 : container_(container) {}
314 void foreach(Body&& body) const {
315 for (auto& value : *container_) {
316 body(std::forward<Value>(value));
320 template<class Handler>
321 bool apply(Handler&& handler) const {
322 for (auto& value : *container_) {
323 if (!handler(std::forward<Value>(value))) {
332 * CopiedSource - For producing values from eagerly from a sequence of values
333 * whose storage is owned by this class. Useful for preparing a generator for
334 * use after a source collection will no longer be available, or for when the
335 * values are specified literally with an initializer list.
337 * This type is primarily used through the 'fromCopy' function, like:
339 * auto sourceCopy = fromCopy(makeAVector());
340 * auto sum = sourceCopy | sum;
341 * auto max = sourceCopy | max;
343 * Though it is also used for the initializer_list specialization of from().
345 template<class StorageType,
348 public GenImpl<const StorageType&,
349 CopiedSource<StorageType, Container>> {
351 !std::is_reference<StorageType>::value, "StorageType must be decayed");
353 // Generator objects are often copied during normal construction as they are
354 // encapsulated by downstream generators. It would be bad if this caused
355 // a copy of the entire container each time, and since we're only exposing a
356 // const reference to the value, it's safe to share it between multiple
359 !std::is_reference<Container>::value,
360 "Can't copy into a reference");
361 std::shared_ptr<const Container> copy_;
363 typedef Container ContainerType;
365 template<class SourceContainer>
366 explicit CopiedSource(const SourceContainer& container)
367 : copy_(new Container(begin(container), end(container))) {}
369 explicit CopiedSource(Container&& container) :
370 copy_(new Container(std::move(container))) {}
372 // To enable re-use of cached results.
373 CopiedSource(const CopiedSource<StorageType, Container>& source)
374 : copy_(source.copy_) {}
377 void foreach(Body&& body) const {
378 for (const auto& value : *copy_) {
383 template<class Handler>
384 bool apply(Handler&& handler) const {
385 // The collection may be reused by others, we can't allow it to be changed.
386 for (const auto& value : *copy_) {
387 if (!handler(value)) {
396 * Sequence - For generating values from beginning value, incremented along the
397 * way with the ++ and += operators. Iteration may continue indefinitely by
398 * setting the 'endless' template parameter to true. If set to false, iteration
399 * will stop when value reaches 'end', either inclusively or exclusively,
400 * depending on the template parameter 'endInclusive'. Value type specified
403 * This type is primarily used through the 'seq' and 'range' function, like:
405 * int total = seq(1, 10) | sum;
406 * auto indexes = range(0, 10);
408 template<class Value,
411 class Sequence : public GenImpl<const Value&,
412 Sequence<Value, endless, endInclusive>> {
413 static_assert(!std::is_reference<Value>::value &&
414 !std::is_const<Value>::value, "Value mustn't be const or ref.");
415 Value bounds_[endless ? 1 : 2];
417 explicit Sequence(Value begin)
418 : bounds_{std::move(begin)} {
419 static_assert(endless, "Must supply 'end'");
422 Sequence(Value begin,
424 : bounds_{std::move(begin), std::move(end)} {}
426 template<class Handler>
427 bool apply(Handler&& handler) const {
428 Value value = bounds_[0];
429 for (;endless || value < bounds_[1]; ++value) {
430 const Value& arg = value;
435 if (endInclusive && value == bounds_[1]) {
436 const Value& arg = value;
445 void foreach(Body&& body) const {
446 Value value = bounds_[0];
447 for (;endless || value < bounds_[1]; ++value) {
448 const Value& arg = value;
451 if (endInclusive && value == bounds_[1]) {
452 const Value& arg = value;
457 static constexpr bool infinite = endless;
461 * Chain - For concatenating the values produced by two Generators.
463 * This type is primarily used through using '+' to combine generators, like:
465 * auto nums = seq(1, 10) + seq(20, 30);
466 * int total = nums | sum;
468 template<class Value, class First, class Second>
469 class Chain : public GenImpl<Value,
470 Chain<Value, First, Second>> {
474 explicit Chain(First first, Second second)
475 : first_(std::move(first))
476 , second_(std::move(second)) {}
478 template<class Handler>
479 bool apply(Handler&& handler) const {
480 return first_.apply(std::forward<Handler>(handler))
481 && second_.apply(std::forward<Handler>(handler));
485 void foreach(Body&& body) const {
486 first_.foreach(std::forward<Body>(body));
487 second_.foreach(std::forward<Body>(body));
490 static constexpr bool infinite = First::infinite || Second::infinite;
494 * GenratorBuilder - Helper for GENERTATOR macro.
496 template<class Value>
497 struct GeneratorBuilder {
498 template<class Source,
499 class Yield = detail::Yield<Value, Source>>
500 Yield operator+(Source&& source) {
501 return Yield(std::forward<Source>(source));
506 * Yield - For producing values from a user-defined generator by way of a
509 template<class Value, class Source>
510 class Yield : public GenImpl<Value, Yield<Value, Source>> {
513 explicit Yield(Source source)
514 : source_(std::move(source)) {
517 template<class Handler>
518 bool apply(Handler&& handler) const {
520 auto body = [&](Value value) {
521 if (!handler(std::forward<Value>(value))) {
534 void foreach(Body&& body) const {
535 source_(std::forward<Body>(body));
539 template<class Value>
540 class Empty : public GenImpl<Value, Empty<Value>> {
542 template<class Handler>
543 bool apply(Handler&&) const { return true; }
551 * Map - For producing a sequence of values by passing each value from a source
552 * collection through a predicate.
554 * This type is usually used through the 'map' or 'mapped' helper function:
556 * auto squares = seq(1, 10) | map(square) | asVector;
558 template<class Predicate>
559 class Map : public Operator<Map<Predicate>> {
564 explicit Map(Predicate pred)
565 : pred_(std::move(pred))
568 template<class Value,
570 class Result = typename ArgumentReference<
571 typename std::result_of<Predicate(Value)>::type
574 public GenImpl<Result, Generator<Value, Source, Result>> {
578 explicit Generator(Source source, const Predicate& pred)
579 : source_(std::move(source)), pred_(pred) {}
582 void foreach(Body&& body) const {
583 source_.foreach([&](Value value) {
584 body(pred_(std::forward<Value>(value)));
588 template<class Handler>
589 bool apply(Handler&& handler) const {
590 return source_.apply([&](Value value) {
591 return handler(pred_(std::forward<Value>(value)));
595 static constexpr bool infinite = Source::infinite;
598 template<class Source,
600 class Gen = Generator<Value, Source>>
601 Gen compose(GenImpl<Value, Source>&& source) const {
602 return Gen(std::move(source.self()), pred_);
605 template<class Source,
607 class Gen = Generator<Value, Source>>
608 Gen compose(const GenImpl<Value, Source>& source) const {
609 return Gen(source.self(), pred_);
615 * Filter - For filtering values from a source sequence by a predicate.
617 * This type is usually used through the 'filter' helper function, like:
619 * auto nonEmpty = from(strings)
620 * | filter([](const string& str) -> bool {
621 * return !str.empty();
624 template<class Predicate>
625 class Filter : public Operator<Filter<Predicate>> {
629 explicit Filter(Predicate pred)
630 : pred_(std::move(pred))
633 template<class Value,
635 class Generator : public GenImpl<Value, Generator<Value, Source>> {
639 explicit Generator(Source source, const Predicate& pred)
640 : source_(std::move(source)), pred_(pred) {}
643 void foreach(Body&& body) const {
644 source_.foreach([&](Value value) {
645 if (pred_(std::forward<Value>(value))) {
646 body(std::forward<Value>(value));
651 template<class Handler>
652 bool apply(Handler&& handler) const {
653 return source_.apply([&](Value value) -> bool {
654 if (pred_(std::forward<Value>(value))) {
655 return handler(std::forward<Value>(value));
661 static constexpr bool infinite = Source::infinite;
664 template<class Source,
666 class Gen = Generator<Value, Source>>
667 Gen compose(GenImpl<Value, Source>&& source) const {
668 return Gen(std::move(source.self()), pred_);
671 template<class Source,
673 class Gen = Generator<Value, Source>>
674 Gen compose(const GenImpl<Value, Source>& source) const {
675 return Gen(source.self(), pred_);
680 * Until - For producing values from a source until a predicate is satisfied.
682 * This type is usually used through the 'until' helper function, like:
684 * auto best = from(sortedItems)
685 * | until([](Item& item) { return item.score > 100; })
688 template<class Predicate>
689 class Until : public Operator<Until<Predicate>> {
693 explicit Until(Predicate pred)
694 : pred_(std::move(pred))
697 template<class Value,
699 class Result = typename std::result_of<Predicate(Value)>::type>
701 public GenImpl<Result, Generator<Value, Source, Result>> {
705 explicit Generator(Source source, const Predicate& pred)
706 : source_(std::move(source)), pred_(pred) {}
708 template<class Handler>
709 bool apply(Handler&& handler) const {
710 return source_.apply([&](Value value) -> bool {
711 return !pred_(std::forward<Value>(value))
712 && handler(std::forward<Value>(value));
717 template<class Source,
719 class Gen = Generator<Value, Source>>
720 Gen compose(GenImpl<Value, Source>&& source) const {
721 return Gen(std::move(source.self()), pred_);
724 template<class Source,
726 class Gen = Generator<Value, Source>>
727 Gen compose(const GenImpl<Value, Source>& source) const {
728 return Gen(source.self(), pred_);
731 // Theoretically an 'until' might stop an infinite
732 static constexpr bool infinite = false;
736 * Take - For producing up to N values from a source.
738 * This type is usually used through the 'take' helper function, like:
740 * auto best = from(docs)
741 * | orderByDescending(scoreDoc)
744 class Take : public Operator<Take> {
747 explicit Take(size_t count)
750 template<class Value,
753 public GenImpl<Value, Generator<Value, Source>> {
757 explicit Generator(Source source, size_t count)
758 : source_(std::move(source)) , count_(count) {}
760 template<class Handler>
761 bool apply(Handler&& handler) const {
762 if (count_ == 0) { return false; }
764 return source_.apply([&](Value value) -> bool {
765 if (!handler(std::forward<Value>(value))) {
773 template<class Source,
775 class Gen = Generator<Value, Source>>
776 Gen compose(GenImpl<Value, Source>&& source) const {
777 return Gen(std::move(source.self()), count_);
780 template<class Source,
782 class Gen = Generator<Value, Source>>
783 Gen compose(const GenImpl<Value, Source>& source) const {
784 return Gen(source.self(), count_);
789 * Sample - For taking a random sample of N elements from a sequence
790 * (without replacement).
792 template<class Random>
793 class Sample : public Operator<Sample<Random>> {
797 explicit Sample(size_t count, Random rng)
798 : count_(count), rng_(std::move(rng)) {}
800 template<class Value,
803 class StorageType = typename std::decay<Value>::type>
805 public GenImpl<StorageType&&,
806 Generator<Value, Source, Rand, StorageType>> {
807 static_assert(!Source::infinite, "Cannot sample infinite source!");
808 // It's too easy to bite ourselves if random generator is only 16-bit
809 static_assert(Random::max() >= std::numeric_limits<int32_t>::max() - 1,
810 "Random number generator must support big values");
815 explicit Generator(Source source, size_t count, Random rng)
816 : source_(std::move(source)) , count_(count), rng_(std::move(rng)) {}
818 template<class Handler>
819 bool apply(Handler&& handler) const {
820 if (count_ == 0) { return false; }
821 std::vector<StorageType> v;
823 // use reservoir sampling to give each source value an equal chance
824 // of appearing in our output.
826 source_.foreach([&](Value value) -> void {
827 if (v.size() < count_) {
828 v.push_back(std::forward<Value>(value));
830 // alternatively, we could create a std::uniform_int_distribution
831 // instead of using modulus, but benchmarks show this has
832 // substantial overhead.
833 size_t index = rng_() % n;
834 if (index < v.size()) {
835 v[index] = std::forward<Value>(value);
841 // output is unsorted!
843 if (!handler(std::move(val))) {
851 template<class Source,
853 class Gen = Generator<Value, Source, Random>>
854 Gen compose(GenImpl<Value, Source>&& source) const {
855 return Gen(std::move(source.self()), count_, rng_);
858 template<class Source,
860 class Gen = Generator<Value, Source, Random>>
861 Gen compose(const GenImpl<Value, Source>& source) const {
862 return Gen(source.self(), count_, rng_);
867 * Skip - For skipping N items from the beginning of a source generator.
869 * This type is usually used through the 'skip' helper function, like:
871 * auto page = from(results)
872 * | skip(pageSize * startPage)
875 class Skip : public Operator<Skip> {
878 explicit Skip(size_t count)
881 template<class Value,
884 public GenImpl<Value, Generator<Value, Source>> {
888 explicit Generator(Source source, size_t count)
889 : source_(std::move(source)) , count_(count) {}
892 void foreach(Body&& body) const {
894 source_.foreach(body);
898 source_.foreach([&](Value value) {
902 body(std::forward<Value>(value));
907 template<class Handler>
908 bool apply(Handler&& handler) const {
910 return source_.apply(handler);
913 return source_.apply([&](Value value) -> bool {
918 return handler(std::forward<Value>(value));
922 static constexpr bool infinite = Source::infinite;
925 template<class Source,
927 class Gen = Generator<Value, Source>>
928 Gen compose(GenImpl<Value, Source>&& source) const {
929 return Gen(std::move(source.self()), count_);
932 template<class Source,
934 class Gen = Generator<Value, Source>>
935 Gen compose(const GenImpl<Value, Source>& source) const {
936 return Gen(source.self(), count_);
941 * Order - For ordering a sequence of values from a source by key.
942 * The key is extracted by the given selector functor, and this key is then
943 * compared using the specified comparator.
945 * This type is usually used through the 'order' helper function, like:
947 * auto closest = from(places)
948 * | orderBy([](Place& p) {
949 * return -distance(p.location, here);
953 template<class Selector, class Comparer>
954 class Order : public Operator<Order<Selector, Comparer>> {
960 explicit Order(Selector selector)
961 : selector_(std::move(selector))
964 Order(Selector selector,
966 : selector_(std::move(selector))
967 , comparer_(std::move(comparer))
970 template<class Value,
972 class StorageType = typename std::decay<Value>::type,
973 class Result = typename std::result_of<Selector(Value)>::type>
975 public GenImpl<StorageType&&,
976 Generator<Value, Source, StorageType, Result>> {
977 static_assert(!Source::infinite, "Cannot sort infinite source!");
982 typedef std::vector<StorageType> VectorType;
984 VectorType asVector() const {
985 auto comparer = [&](const StorageType& a, const StorageType& b) {
986 return comparer_(selector_(a), selector_(b));
988 auto vals = source_ | as<VectorType>();
989 std::sort(vals.begin(), vals.end(), comparer);
990 return std::move(vals);
993 Generator(Source source,
996 : source_(std::move(source)),
997 selector_(std::move(selector)),
998 comparer_(std::move(comparer)) {}
1000 VectorType operator|(const Collect<VectorType>&) const {
1004 VectorType operator|(const CollectTemplate<std::vector>&) const {
1008 template<class Body>
1009 void foreach(Body&& body) const {
1010 for (auto& value : asVector()) {
1011 body(std::move(value));
1015 template<class Handler>
1016 bool apply(Handler&& handler) const {
1017 auto comparer = [&](const StorageType& a, const StorageType& b) {
1018 // swapped for minHeap
1019 return comparer_(selector_(b), selector_(a));
1021 auto heap = source_ | as<VectorType>();
1022 std::make_heap(heap.begin(), heap.end(), comparer);
1023 while (!heap.empty()) {
1024 std::pop_heap(heap.begin(), heap.end(), comparer);
1025 if (!handler(std::move(heap.back()))) {
1034 template<class Source,
1036 class Gen = Generator<Value, Source>>
1037 Gen compose(GenImpl<Value, Source>&& source) const {
1038 return Gen(std::move(source.self()), selector_, comparer_);
1041 template<class Source,
1043 class Gen = Generator<Value, Source>>
1044 Gen compose(const GenImpl<Value, Source>& source) const {
1045 return Gen(source.self(), selector_, comparer_);
1050 * TypeAssertion - For verifying the exact type of the value produced by a
1051 * generator. Useful for testing and debugging, and acts as a no-op at runtime.
1052 * Pass-through at runtime. Used through the 'assert_type<>()' factory method
1055 * auto c = from(vector) | assert_type<int&>() | sum;
1058 template<class Expected>
1059 class TypeAssertion : public Operator<TypeAssertion<Expected>> {
1061 template<class Source, class Value>
1062 const Source& compose(const GenImpl<Value, Source>& source) const {
1063 static_assert(std::is_same<Expected, Value>::value,
1064 "assert_type() check failed");
1065 return source.self();
1068 template<class Source, class Value>
1069 Source&& compose(GenImpl<Value, Source>&& source) const {
1070 static_assert(std::is_same<Expected, Value>::value,
1071 "assert_type() check failed");
1072 return std::move(source.self());
1077 * Distinct - For filtering duplicates out of a sequence. A selector may be
1078 * provided to generate a key to uniquify for each value.
1080 * This type is usually used through the 'distinct' helper function, like:
1082 * auto closest = from(results)
1083 * | distinctBy([](Item& i) {
1088 template<class Selector>
1089 class Distinct : public Operator<Distinct<Selector>> {
1094 explicit Distinct(Selector selector)
1095 : selector_(std::move(selector))
1098 template<class Value,
1100 class Generator : public GenImpl<Value, Generator<Value, Source>> {
1104 typedef typename std::decay<Value>::type StorageType;
1106 // selector_ cannot be passed an rvalue or it would end up passing the husk
1107 // of a value to the downstream operators.
1108 typedef const StorageType& ParamType;
1110 typedef typename std::result_of<Selector(ParamType)>::type KeyType;
1111 typedef typename std::decay<KeyType>::type KeyStorageType;
1114 Generator(Source source,
1116 : source_(std::move(source)),
1117 selector_(std::move(selector)) {}
1119 template<class Body>
1120 void foreach(Body&& body) const {
1121 std::unordered_set<KeyStorageType> keysSeen;
1122 source_.foreach([&](Value value) {
1123 if (keysSeen.insert(selector_(ParamType(value))).second) {
1124 body(std::forward<Value>(value));
1129 template<class Handler>
1130 bool apply(Handler&& handler) const {
1131 std::unordered_set<KeyStorageType> keysSeen;
1132 return source_.apply([&](Value value) -> bool {
1133 if (keysSeen.insert(selector_(ParamType(value))).second) {
1134 return handler(std::forward<Value>(value));
1141 template<class Source,
1143 class Gen = Generator<Value, Source>>
1144 Gen compose(GenImpl<Value, Source>&& source) const {
1145 return Gen(std::move(source.self()), selector_);
1148 template<class Source,
1150 class Gen = Generator<Value, Source>>
1151 Gen compose(const GenImpl<Value, Source>& source) const {
1152 return Gen(source.self(), selector_);
1157 * Composed - For building up a pipeline of operations to perform, absent any
1158 * particular source generator. Useful for building up custom pipelines.
1160 * This type is usually used by just piping two operators together:
1162 * auto valuesOf = filter([](Optional<int>& o) { return o.hasValue(); })
1163 * | map([](Optional<int>& o) -> int& { return o.value(); });
1165 * auto valuesIncluded = from(optionals) | valuesOf | as<vector>();
1167 template<class First,
1169 class Composed : public Operator<Composed<First, Second>> {
1175 Composed(First first, Second second)
1176 : first_(std::move(first))
1177 , second_(std::move(second)) {}
1179 template<class Source,
1181 class FirstRet = decltype(std::declval<First>()
1182 .compose(std::declval<Source>())),
1183 class SecondRet = decltype(std::declval<Second>()
1184 .compose(std::declval<FirstRet>()))>
1185 SecondRet compose(const GenImpl<Value, Source>& source) const {
1186 return second_.compose(first_.compose(source.self()));
1189 template<class Source,
1191 class FirstRet = decltype(std::declval<First>()
1192 .compose(std::declval<Source>())),
1193 class SecondRet = decltype(std::declval<Second>()
1194 .compose(std::declval<FirstRet>()))>
1195 SecondRet compose(GenImpl<Value, Source>&& source) const {
1196 return second_.compose(first_.compose(std::move(source.self())));
1205 * FoldLeft - Left-associative functional fold. For producing an aggregate value
1206 * from a seed and a folder function. Useful for custom aggregators on a
1209 * This type is primarily used through the 'foldl' helper method, like:
1211 * double movingAverage = from(values)
1212 * | foldl(0.0, [](double avg, double sample) {
1213 * return sample * 0.1 + avg * 0.9;
1216 template<class Seed,
1218 class FoldLeft : public Operator<FoldLeft<Seed, Fold>> {
1225 : seed_(std::move(seed))
1226 , fold_(std::move(fold))
1229 template<class Source,
1231 Seed compose(const GenImpl<Value, Source>& source) const {
1232 static_assert(!Source::infinite, "Cannot foldl infinite source");
1234 source | [&](Value v) {
1235 accum = fold_(std::move(accum), std::forward<Value>(v));
1242 * First - For finding the first value in a sequence.
1244 * This type is primarily used through the 'first' static value, like:
1246 * int firstThreeDigitPrime = seq(100) | filter(isPrime) | first;
1248 class First : public Operator<First> {
1252 template<class Source,
1254 class StorageType = typename std::decay<Value>::type>
1255 StorageType compose(const GenImpl<Value, Source>& source) const {
1256 Optional<StorageType> accum;
1257 source | [&](Value v) -> bool {
1258 accum = std::forward<Value>(v);
1261 if (!accum.hasValue()) {
1262 throw EmptySequence();
1264 return std::move(accum.value());
1270 * Any - For determining whether any values in a sequence satisfy a predicate.
1272 * This type is primarily used through the 'any' static value, like:
1274 * bool any20xPrimes = seq(200, 210) | filter(isPrime) | any;
1276 * Note that it may also be used like so:
1278 * bool any20xPrimes = seq(200, 210) | any(isPrime);
1281 class Any : public Operator<Any> {
1285 template<class Source,
1287 bool compose(const GenImpl<Value, Source>& source) const {
1289 source | [&](Value v) -> bool {
1297 * Convenience function for use like:
1299 * bool found = gen | any([](int i) { return i * i > 100; });
1301 template<class Predicate,
1302 class Filter = Filter<Predicate>,
1303 class Composed = Composed<Filter, Any>>
1304 Composed operator()(Predicate pred) const {
1305 return Composed(Filter(std::move(pred)), Any());
1310 * All - For determining whether all values in a sequence satisfy a predicate.
1312 * This type is primarily used through the 'any' static value, like:
1314 * bool valid = from(input) | all(validate);
1316 * Note: Passing an empty sequence through 'all()' will always return true.
1318 template<class Predicate>
1319 class All : public Operator<All<Predicate>> {
1323 explicit All(Predicate pred)
1324 : pred_(std::move(pred))
1327 template<class Source,
1329 bool compose(const GenImpl<Value, Source>& source) const {
1330 static_assert(!Source::infinite, "Cannot call 'all' on infinite source");
1332 source | [&](Value v) -> bool {
1333 if (!pred_(std::forward<Value>(v))) {
1344 * Reduce - Functional reduce, for recursively combining values from a source
1345 * using a reducer function until there is only one item left. Useful for
1346 * combining values when an empty sequence doesn't make sense.
1348 * This type is primarily used through the 'reduce' helper method, like:
1350 * sring longest = from(names)
1351 * | reduce([](string&& best, string& current) {
1352 * return best.size() >= current.size() ? best : current;
1355 template<class Reducer>
1356 class Reduce : public Operator<Reduce<Reducer>> {
1360 explicit Reduce(Reducer reducer)
1361 : reducer_(std::move(reducer))
1364 template<class Source,
1366 class StorageType = typename std::decay<Value>::type>
1367 StorageType compose(const GenImpl<Value, Source>& source) const {
1368 Optional<StorageType> accum;
1369 source | [&](Value v) {
1370 if (accum.hasValue()) {
1371 accum = reducer_(std::move(accum.value()), std::forward<Value>(v));
1373 accum = std::forward<Value>(v);
1376 if (!accum.hasValue()) {
1377 throw EmptySequence();
1379 return accum.value();
1384 * Count - for simply counting the items in a collection.
1386 * This type is usually used through its singleton, 'count':
1388 * auto shortPrimes = seq(1, 100) | filter(isPrime) | count;
1390 class Count : public Operator<Count> {
1394 template<class Source,
1396 size_t compose(const GenImpl<Value, Source>& source) const {
1397 static_assert(!Source::infinite, "Cannot count infinite source");
1398 return foldl(size_t(0),
1399 [](size_t accum, Value v) {
1406 * Sum - For simply summing up all the values from a source.
1408 * This type is usually used through its singleton, 'sum':
1410 * auto gaussSum = seq(1, 100) | sum;
1412 class Sum : public Operator<Sum> {
1414 Sum() : Operator<Sum>() {}
1416 template<class Source,
1418 class StorageType = typename std::decay<Value>::type>
1419 StorageType compose(const GenImpl<Value, Source>& source) const {
1420 static_assert(!Source::infinite, "Cannot sum infinite source");
1421 return foldl(StorageType(0),
1422 [](StorageType&& accum, Value v) {
1423 return std::move(accum) + std::forward<Value>(v);
1429 * Contains - For testing whether a value matching the given value is contained
1432 * This type should be used through the 'contains' helper method, like:
1434 * bool contained = seq(1, 10) | map(square) | contains(49);
1436 template<class Needle>
1437 class Contains : public Operator<Contains<Needle>> {
1440 explicit Contains(Needle needle)
1441 : needle_(std::move(needle))
1444 template<class Source,
1446 class StorageType = typename std::decay<Value>::type>
1447 bool compose(const GenImpl<Value, Source>& source) const {
1448 static_assert(!Source::infinite,
1449 "Calling contains on an infinite source might cause "
1450 "an infinite loop.");
1451 return !(source | [this](Value value) {
1452 return !(needle_ == std::forward<Value>(value));
1458 * Min - For a value which minimizes a key, where the key is determined by a
1459 * given selector, and compared by given comparer.
1461 * This type is usually used through the singletone 'min' or through the helper
1462 * functions 'minBy' and 'maxBy'.
1464 * auto oldest = from(people)
1465 * | minBy([](Person& p) {
1466 * return p.dateOfBirth;
1469 template<class Selector,
1471 class Min : public Operator<Min<Selector, Comparer>> {
1477 explicit Min(Selector selector)
1478 : selector_(std::move(selector))
1481 Min(Selector selector,
1483 : selector_(std::move(selector))
1484 , comparer_(std::move(comparer))
1487 template<class Value,
1489 class StorageType = typename std::decay<Value>::type,
1490 class Key = typename std::decay<
1491 typename std::result_of<Selector(Value)>::type
1493 StorageType compose(const GenImpl<Value, Source>& source) const {
1494 Optional<StorageType> min;
1495 Optional<Key> minKey;
1496 source | [&](Value v) {
1497 Key key = selector_(std::forward<Value>(v));
1498 if (!minKey.hasValue() || comparer_(key, minKey.value())) {
1500 min = std::forward<Value>(v);
1503 if (!min.hasValue()) {
1504 throw EmptySequence();
1511 * Append - For collecting values from a source into a given output container
1514 * This type is usually used through the helper function 'appendTo', like:
1516 * vector<int64_t> ids;
1517 * from(results) | map([](Person& p) { return p.id })
1520 template<class Collection>
1521 class Append : public Operator<Append<Collection>> {
1522 Collection* collection_;
1524 explicit Append(Collection* collection)
1525 : collection_(collection)
1528 template<class Value,
1530 Collection& compose(const GenImpl<Value, Source>& source) const {
1531 source | [&](Value v) {
1532 collection_->insert(collection_->end(), std::forward<Value>(v));
1534 return *collection_;
1539 * Collect - For collecting values from a source in a collection of the desired
1542 * This type is usually used through the helper function 'as', like:
1544 * std::string upper = from(stringPiece)
1546 * | as<std::string>();
1548 template<class Collection>
1549 class Collect : public Operator<Collect<Collection>> {
1553 template<class Value,
1555 class StorageType = typename std::decay<Value>::type>
1556 Collection compose(const GenImpl<Value, Source>& source) const {
1557 Collection collection;
1558 source | [&](Value v) {
1559 collection.insert(collection.end(), std::forward<Value>(v));
1567 * CollectTemplate - For collecting values from a source in a collection
1568 * constructed using the specified template type. Given the type of values
1569 * produced by the given generator, the collection type will be:
1570 * Container<Value, Allocator<Value>>
1572 * The allocator defaults to std::allocator, so this may be used for the STL
1573 * containers by simply using operators like 'as<set>', 'as<deque>',
1574 * 'as<vector>'. 'as', here is the helper method which is the usual means of
1575 * consturcting this operator.
1579 * set<string> uniqueNames = from(names) | as<set>();
1581 template<template<class, class> class Container,
1582 template<class> class Allocator>
1583 class CollectTemplate : public Operator<CollectTemplate<Container, Allocator>> {
1585 CollectTemplate() { }
1587 template<class Value,
1589 class StorageType = typename std::decay<Value>::type,
1590 class Collection = Container<StorageType, Allocator<StorageType>>>
1591 Collection compose(const GenImpl<Value, Source>& source) const {
1592 Collection collection;
1593 source | [&](Value v) {
1594 collection.insert(collection.end(), std::forward<Value>(v));
1601 * Concat - For flattening generators of generators.
1603 * This type is usually used through the 'concat' static value, like:
1607 * | map([](Node& x) {
1608 * return from(x.neighbors)
1609 * | map([&](Node& y) {
1610 * return Edge(x, y);
1616 class Concat : public Operator<Concat> {
1620 template<class Inner,
1622 class InnerValue = typename std::decay<Inner>::type::ValueType>
1624 public GenImpl<InnerValue, Generator<Inner, Source, InnerValue>> {
1627 explicit Generator(Source source)
1628 : source_(std::move(source)) {}
1630 template<class Handler>
1631 bool apply(Handler&& handler) const {
1632 return source_.apply([&](Inner inner) -> bool {
1633 return inner.apply(std::forward<Handler>(handler));
1637 template<class Body>
1638 void foreach(Body&& body) const {
1639 source_.foreach([&](Inner inner) {
1640 inner.foreach(std::forward<Body>(body));
1644 static constexpr bool infinite = Source::infinite;
1647 template<class Value,
1649 class Gen = Generator<Value, Source>>
1650 Gen compose(GenImpl<Value, Source>&& source) const {
1651 return Gen(std::move(source.self()));
1654 template<class Value,
1656 class Gen = Generator<Value, Source>>
1657 Gen compose(const GenImpl<Value, Source>& source) const {
1658 return Gen(source.self());
1663 * RangeConcat - For flattening generators of iterables.
1665 * This type is usually used through the 'rconcat' static value, like:
1667 * map<int, vector<int>> adjacency;
1674 class RangeConcat : public Operator<RangeConcat> {
1678 template<class Range,
1680 class InnerValue = typename ValueTypeOfRange<Range>::RefType>
1682 : public GenImpl<InnerValue, Generator<Range, Source, InnerValue>> {
1685 explicit Generator(Source source)
1686 : source_(std::move(source)) {}
1688 template<class Body>
1689 void foreach(Body&& body) const {
1690 source_.foreach([&](Range range) {
1691 for (auto& value : range) {
1697 template<class Handler>
1698 bool apply(Handler&& handler) const {
1699 return source_.apply([&](Range range) -> bool {
1700 for (auto& value : range) {
1701 if (!handler(value)) {
1710 template<class Value,
1712 class Gen = Generator<Value, Source>>
1713 Gen compose(GenImpl<Value, Source>&& source) const {
1714 return Gen(std::move(source.self()));
1717 template<class Value,
1719 class Gen = Generator<Value, Source>>
1720 Gen compose(const GenImpl<Value, Source>& source) const {
1721 return Gen(source.self());
1727 * GuardImpl - For handling exceptions from downstream computation. Requires the
1728 * type of exception to catch, and handler function to invoke in the event of
1729 * the exception. Note that the handler may:
1730 * 1) return true to continue processing the sequence
1731 * 2) return false to end the sequence immediately
1732 * 3) throw, to pass the exception to the next catch
1733 * The handler must match the signature 'bool(Exception&, Value)'.
1735 * This type is used through the `guard` helper, like so:
1738 * = byLine(STDIN_FILENO)
1739 * | guard<std::runtime_error>([](std::runtime_error& e,
1741 * LOG(ERROR) << sp << ": " << e.str();
1742 * return true; // continue processing subsequent lines
1747 * TODO(tjackson): Rename this back to Guard.
1749 template<class Exception,
1751 class GuardImpl : public Operator<GuardImpl<Exception, ErrorHandler>> {
1752 ErrorHandler handler_;
1754 GuardImpl(ErrorHandler handler)
1755 : handler_(std::move(handler)) {}
1757 template<class Value,
1759 class Generator : public GenImpl<Value, Generator<Value, Source>> {
1761 ErrorHandler handler_;
1763 explicit Generator(Source source,
1764 ErrorHandler handler)
1765 : source_(std::move(source)),
1766 handler_(std::move(handler)) {}
1768 template<class Handler>
1769 bool apply(Handler&& handler) const {
1770 return source_.apply([&](Value value) -> bool {
1772 handler(std::forward<Value>(value));
1774 } catch (Exception& e) {
1775 return handler_(e, std::forward<Value>(value));
1780 static constexpr bool infinite = Source::infinite;
1783 template<class Value,
1785 class Gen = Generator<Value, Source>>
1786 Gen compose(GenImpl<Value, Source>&& source) const {
1787 return Gen(std::move(source.self()), handler_);
1790 template<class Value,
1792 class Gen = Generator<Value, Source>>
1793 Gen compose(const GenImpl<Value, Source>& source) const {
1794 return Gen(source.self(), handler_);
1799 * Cycle - For repeating a sequence forever.
1801 * This type is usually used through the 'cycle' static value, like:
1808 class Cycle : public Operator<Cycle> {
1809 off_t limit_; // -1 for infinite
1814 explicit Cycle(off_t limit)
1817 template<class Value,
1819 class Generator : public GenImpl<Value, Generator<Value, Source>> {
1821 off_t limit_; // -1 for infinite
1823 explicit Generator(Source source, off_t limit)
1824 : source_(std::move(source))
1827 template<class Handler>
1828 bool apply(Handler&& handler) const {
1830 auto handler2 = [&](Value value) {
1831 cont = handler(std::forward<Value>(value));
1834 for (off_t count = 0; count != limit_; ++count) {
1836 source_.apply(handler2);
1844 // not actually infinite, since an empty generator will end the cycles.
1845 static constexpr bool infinite = Source::infinite;
1848 template<class Source,
1850 class Gen = Generator<Value, Source>>
1851 Gen compose(GenImpl<Value, Source>&& source) const {
1852 return Gen(std::move(source.self()), limit_);
1855 template<class Source,
1857 class Gen = Generator<Value, Source>>
1858 Gen compose(const GenImpl<Value, Source>& source) const {
1859 return Gen(source.self(), limit_);
1863 * Convenience function for use like:
1865 * auto tripled = gen | cycle(3);
1867 Cycle operator()(off_t limit) const {
1868 return Cycle(limit);
1875 * VirtualGen<T> - For wrapping template types in simple polymorphic wrapper.
1877 template<class Value>
1878 class VirtualGen : public GenImpl<Value, VirtualGen<Value>> {
1881 virtual ~WrapperBase() {}
1882 virtual bool apply(const std::function<bool(Value)>& handler) const = 0;
1883 virtual void foreach(const std::function<void(Value)>& body) const = 0;
1884 virtual std::unique_ptr<const WrapperBase> clone() const = 0;
1887 template<class Wrapped>
1888 class WrapperImpl : public WrapperBase {
1891 explicit WrapperImpl(Wrapped wrapped)
1892 : wrapped_(std::move(wrapped)) {
1895 virtual bool apply(const std::function<bool(Value)>& handler) const {
1896 return wrapped_.apply(handler);
1899 virtual void foreach(const std::function<void(Value)>& body) const {
1900 wrapped_.foreach(body);
1903 virtual std::unique_ptr<const WrapperBase> clone() const {
1904 return std::unique_ptr<const WrapperBase>(new WrapperImpl(wrapped_));
1908 std::unique_ptr<const WrapperBase> wrapper_;
1911 template<class Self>
1912 /* implicit */ VirtualGen(Self source)
1913 : wrapper_(new WrapperImpl<Self>(std::move(source)))
1916 VirtualGen(VirtualGen&& source)
1917 : wrapper_(std::move(source.wrapper_))
1920 VirtualGen(const VirtualGen& source)
1921 : wrapper_(source.wrapper_->clone())
1924 VirtualGen& operator=(const VirtualGen& source) {
1925 wrapper_.reset(source.wrapper_->clone());
1929 VirtualGen& operator=(VirtualGen&& source) {
1930 wrapper_= std::move(source.wrapper_);
1934 bool apply(const std::function<bool(Value)>& handler) const {
1935 return wrapper_->apply(handler);
1938 void foreach(const std::function<void(Value)>& body) const {
1939 wrapper_->foreach(body);
1944 * non-template operators, statically defined to avoid the need for anything but
1947 static const detail::Sum sum;
1949 static const detail::Count count;
1951 static const detail::First first;
1954 * Use directly for detecting any values, or as a function to detect values
1955 * which pass a predicate:
1957 * auto nonempty = g | any;
1958 * auto evens = g | any(even);
1960 static const detail::Any any;
1962 static const detail::Min<Identity, Less> min;
1964 static const detail::Min<Identity, Greater> max;
1966 static const detail::Order<Identity> order;
1968 static const detail::Distinct<Identity> distinct;
1970 static const detail::Map<Move> move;
1972 static const detail::Concat concat;
1974 static const detail::RangeConcat rconcat;
1977 * Use directly for infinite sequences, or as a function to limit cycle count.
1979 * auto forever = g | cycle;
1980 * auto thrice = g | cycle(3);
1982 static const detail::Cycle cycle;
1984 inline detail::Take take(size_t count) {
1985 return detail::Take(count);
1988 template<class Random = std::default_random_engine>
1989 inline detail::Sample<Random> sample(size_t count, Random rng = Random()) {
1990 return detail::Sample<Random>(count, std::move(rng));
1993 inline detail::Skip skip(size_t count) {
1994 return detail::Skip(count);
1999 #pragma GCC diagnostic pop