2 * Copyright 2014 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 #ifndef FOLLY_GEN_BASE_H
18 #define FOLLY_GEN_BASE_H
22 #include <type_traits>
27 #include <unordered_set>
29 #include "folly/Range.h"
30 #include "folly/Optional.h"
31 #include "folly/Conv.h"
32 #include "folly/gen/Core.h"
35 * Generator-based Sequence Comprehensions in C++, akin to C#'s LINQ
36 * @author Tom Jackson <tjackson@fb.com>
38 * This library makes it possible to write declarative comprehensions for
39 * processing sequences of values efficiently in C++. The operators should be
40 * familiar to those with experience in functional programming, and the
41 * performance will be virtually identical to the equivalent, boilerplate C++
44 * Generator objects may be created from either an stl-like container (anything
45 * supporting begin() and end()), from sequences of values, or from another
46 * generator (see below). To create a generator that pulls values from a vector,
47 * for example, one could write:
49 * vector<string> names { "Jack", "Jill", "Sara", "Tom" };
50 * auto gen = from(names);
52 * Generators are composed by building new generators out of old ones through
53 * the use of operators. These are reminicent of shell pipelines, and afford
54 * similar composition. Lambda functions are used liberally to describe how to
55 * handle individual values:
58 * | mapped([](const fbstring& name) { return name.size(); });
60 * Generators are lazy; they don't actually perform any work until they need to.
61 * As an example, the 'lengths' generator (above) won't actually invoke the
62 * provided lambda until values are needed:
64 * auto lengthVector = lengths | as<std::vector>();
65 * auto totalLength = lengths | sum;
67 * 'auto' is useful in here because the actual types of the generators objects
68 * are usually complicated and implementation-sensitive.
70 * If a simpler type is desired (for returning, as an example), VirtualGen<T>
71 * may be used to wrap the generator in a polymorphic wrapper:
73 * VirtualGen<float> powersOfE() {
74 * return seq(1) | mapped(&expf);
77 * To learn more about this library, including the use of infinite generators,
78 * see the examples in the comments, or the docs (coming soon).
81 namespace folly { namespace gen {
83 class EmptySequence : public std::exception {
85 virtual const char* what() const noexcept {
86 return "This operation cannot be called on an empty sequence";
94 auto operator()(const First& first, const Second& second) const ->
95 decltype(first < second) {
96 return first < second;
102 template<class First,
104 auto operator()(const First& first, const Second& second) const ->
105 decltype(first > second) {
106 return first > second;
113 template<class Value>
114 auto operator()(Value&& value) const ->
115 decltype(std::get<n>(std::forward<Value>(value))) {
116 return std::get<n>(std::forward<Value>(value));
120 template<class Class,
122 class MemberFunction {
124 typedef Result (Class::*MemberPtr)();
128 explicit MemberFunction(MemberPtr member)
132 Result operator()(Class&& x) const {
133 return (x.*member_)();
136 Result operator()(Class& x) const {
137 return (x.*member_)();
141 template<class Class,
143 class ConstMemberFunction{
145 typedef Result (Class::*MemberPtr)() const;
149 explicit ConstMemberFunction(MemberPtr member)
153 Result operator()(const Class& x) const {
154 return (x.*member_)();
158 template<class Class,
162 typedef FieldType (Class::*FieldPtr);
166 explicit Field(FieldPtr field)
170 const FieldType& operator()(const Class& x) const {
174 FieldType& operator()(Class& x) const {
178 FieldType&& operator()(Class&& x) const {
179 return std::move(x.*field_);
185 template<class Value>
186 auto operator()(Value&& value) const ->
187 decltype(std::move(std::forward<Value>(value))) {
188 return std::move(std::forward<Value>(value));
194 template<class Value>
195 auto operator()(Value&& value) const ->
196 decltype(std::forward<Value>(value)) {
197 return std::forward<Value>(value);
201 template <class Dest>
204 template <class Value>
205 Dest operator()(Value&& value) const {
206 return Dest(std::forward<Value>(value));
210 template <class Dest>
213 template <class Value>
214 Dest operator()(Value&& value) const {
215 return ::folly::to<Dest>(std::forward<Value>(value));
219 // Specialization to allow String->StringPiece conversion
221 class To<StringPiece> {
223 StringPiece operator()(StringPiece src) const {
236 template<class Container>
237 struct ValueTypeOfRange {
239 static Container container_;
241 typedef decltype(*std::begin(container_))
243 typedef typename std::decay<decltype(*std::begin(container_))>::type
251 template<class Container,
252 class Value = typename ValueTypeOfRange<Container>::RefType>
253 class ReferencedSource;
255 template<class Value,
256 class Container = std::vector<typename std::decay<Value>::type>>
259 template<class Value, bool endless = false, bool endInclusive = false>
262 template<class Value, class Source>
265 template<class Value>
268 template<class Value>
274 template<class Predicate>
277 template<class Predicate>
280 template<class Predicate>
290 template<class Selector, class Comparer = Less>
293 template<class Selector>
296 template<class Operators>
299 template<class Expected>
323 template<class Predicate>
326 template<class Reducer>
331 template<class Selector,
335 template<class Container>
338 template<template<class, class> class Collection = std::vector,
339 template<class> class Allocator = std::allocator>
340 class CollectTemplate;
342 template<class Collection>
345 template<class Value>
346 struct GeneratorBuilder;
348 template<class Needle>
351 template<class Exception,
358 * Polymorphic wrapper
360 template<class Value>
366 template<class Container,
367 class From = detail::ReferencedSource<const Container>>
368 From fromConst(const Container& source) {
369 return From(&source);
372 template<class Container,
373 class From = detail::ReferencedSource<Container>>
374 From from(Container& source) {
375 return From(&source);
378 template<class Container,
380 typename detail::ValueTypeOfRange<Container>::StorageType,
381 class CopyOf = detail::CopiedSource<Value>>
382 CopyOf fromCopy(Container&& source) {
383 return CopyOf(std::forward<Container>(source));
386 template<class Value,
387 class From = detail::CopiedSource<Value>>
388 From from(std::initializer_list<Value> source) {
392 template<class Container,
393 class From = detail::CopiedSource<typename Container::value_type,
395 From from(Container&& source) {
396 return From(std::move(source));
399 template<class Value, class Gen = detail::Sequence<Value, false, false>>
400 Gen range(Value begin, Value end) {
401 return Gen(begin, end);
404 template<class Value,
405 class Gen = detail::Sequence<Value, false, true>>
406 Gen seq(Value first, Value last) {
407 return Gen(first, last);
410 template<class Value,
411 class Gen = detail::Sequence<Value, true>>
412 Gen seq(Value begin) {
416 template<class Value,
418 class Yield = detail::Yield<Value, Source>>
419 Yield generator(Source&& source) {
420 return Yield(std::forward<Source>(source));
424 * Create inline generator, used like:
426 * auto gen = GENERATOR(int) { yield(1); yield(2); };
428 #define GENERATOR(TYPE) \
429 ::folly::gen::detail::GeneratorBuilder<TYPE>() + \
430 [=](const std::function<void(TYPE)>& yield)
433 * empty() - for producing empty sequences.
435 template<class Value>
436 detail::Empty<Value> empty() {
440 template<class Value>
441 detail::Just<Value> just(Value value) {
442 return detail::Just<Value>(std::move(value));
448 template<class Predicate,
449 class Map = detail::Map<Predicate>>
450 Map mapped(Predicate pred = Predicate()) {
451 return Map(std::move(pred));
454 template<class Predicate,
455 class Map = detail::Map<Predicate>>
456 Map map(Predicate pred = Predicate()) {
457 return Map(std::move(pred));
461 * mapOp - Given a generator of generators, maps the application of the given
462 * operator on to each inner gen. Especially useful in aggregating nested data
465 * chunked(samples, 256)
466 * | mapOp(filter(sampleTest) | count)
469 template<class Operator,
470 class Map = detail::Map<detail::Composer<Operator>>>
471 Map mapOp(Operator op) {
472 return Map(detail::Composer<Operator>(std::move(op)));
476 * member(...) - For extracting a member from each value.
478 * vector<string> strings = ...;
479 * auto sizes = from(strings) | member(&string::size);
481 * If a member is const overridden (like 'front()'), pass template parameter
482 * 'Const' to select the const version, or 'Mutable' to select the non-const
485 * auto heads = from(strings) | member<Const>(&string::front);
492 template<MemberType Constness = Const,
495 class Mem = ConstMemberFunction<Class, Return>,
496 class Map = detail::Map<Mem>>
497 typename std::enable_if<Constness == Const, Map>::type
498 member(Return (Class::*member)() const) {
499 return Map(Mem(member));
502 template<MemberType Constness = Mutable,
505 class Mem = MemberFunction<Class, Return>,
506 class Map = detail::Map<Mem>>
507 typename std::enable_if<Constness == Mutable, Map>::type
508 member(Return (Class::*member)()) {
509 return Map(Mem(member));
513 * field(...) - For extracting a field from each value.
515 * vector<Item> items = ...;
516 * auto names = from(items) | field(&Item::name);
518 * Note that if the values of the generator are rvalues, any non-reference
519 * fields will be rvalues as well. As an example, the code below does not copy
520 * any strings, only moves them:
522 * auto namesVector = from(items)
524 * | field(&Item::name)
527 template<class Class,
529 class Field = Field<Class, FieldType>,
530 class Map = detail::Map<Field>>
531 Map field(FieldType Class::*field) {
532 return Map(Field(field));
535 template<class Predicate,
536 class Filter = detail::Filter<Predicate>>
537 Filter filter(Predicate pred = Predicate()) {
538 return Filter(std::move(pred));
541 template<class Predicate,
542 class All = detail::All<Predicate>>
543 All all(Predicate pred = Predicate()) {
544 return All(std::move(pred));
547 template<class Predicate,
548 class Until = detail::Until<Predicate>>
549 Until until(Predicate pred = Predicate()) {
550 return Until(std::move(pred));
553 template<class Selector,
554 class Comparer = Less,
555 class Order = detail::Order<Selector, Comparer>>
556 Order orderBy(Selector selector = Identity(),
557 Comparer comparer = Comparer()) {
558 return Order(std::move(selector),
559 std::move(comparer));
562 template<class Selector,
563 class Order = detail::Order<Selector, Greater>>
564 Order orderByDescending(Selector selector = Identity()) {
565 return Order(std::move(selector));
568 template<class Selector,
569 class Distinct = detail::Distinct<Selector>>
570 Distinct distinctBy(Selector selector = Identity()) {
571 return Distinct(std::move(selector));
575 class Get = detail::Map<Get<n>>>
580 // construct Dest from each value
581 template <class Dest,
582 class Cast = detail::Map<Cast<Dest>>>
587 // call folly::to on each value
588 template <class Dest,
589 class To = detail::Map<To<Dest>>>
594 template<class Value>
595 detail::TypeAssertion<Value> assert_type() {
604 class FoldLeft = detail::FoldLeft<Seed, Fold>>
605 FoldLeft foldl(Seed seed = Seed(),
606 Fold fold = Fold()) {
607 return FoldLeft(std::move(seed),
611 template<class Reducer,
612 class Reduce = detail::Reduce<Reducer>>
613 Reduce reduce(Reducer reducer = Reducer()) {
614 return Reduce(std::move(reducer));
617 template<class Selector = Identity,
618 class Min = detail::Min<Selector, Less>>
619 Min minBy(Selector selector = Selector()) {
620 return Min(std::move(selector));
623 template<class Selector,
624 class MaxBy = detail::Min<Selector, Greater>>
625 MaxBy maxBy(Selector selector = Selector()) {
626 return MaxBy(std::move(selector));
629 template<class Collection,
630 class Collect = detail::Collect<Collection>>
635 template<template<class, class> class Container = std::vector,
636 template<class> class Allocator = std::allocator,
637 class Collect = detail::CollectTemplate<Container, Allocator>>
642 template<class Collection,
643 class Append = detail::Append<Collection>>
644 Append appendTo(Collection& collection) {
645 return Append(&collection);
648 template<class Needle,
649 class Contains = detail::Contains<typename std::decay<Needle>::type>>
650 Contains contains(Needle&& needle) {
651 return Contains(std::forward<Needle>(needle));
654 template<class Exception,
659 typename std::decay<ErrorHandler>::type>>
660 GuardImpl guard(ErrorHandler&& handler) {
661 return GuardImpl(std::forward<ErrorHandler>(handler));
666 #include "folly/gen/Base-inl.h"
668 #endif // FOLLY_GEN_BASE_H