All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
functors_example.cpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
16 #include "paal/utils/functors.hpp"
17 
18 #include <vector>
19 #include <string>
20 
21 using namespace paal::utils;
22 
23 void functors_example() {
24  // skip
25  skip_functor skip;
26 
27  skip(2, 2.1, "asda");
28 
29  // identity
31 
32  assert(id("asd") == std::string("asd"));
33  assert(id(7) == 7);
34 
35  // return something
36  always_true retTrue;
37  always_false retFalse;
38  return_zero_functor retZero;
39 
40  assert(retTrue(2, 2.3, "abc"));
41  assert(!retFalse(2, 2.3, "abc"));
42  assert(retZero(2, 2.3, "abc") == 0);
43 
44  // assert
45  // assert_functor assertFun;
46  // assertFun(); //aborts
47 
48  // array to functor
49  std::vector<int> vec{ 1, 2, 3 };
50  auto vecFun = make_array_to_functor(vec);
51  assert(vecFun(1) == 2);
52 
53  auto vecFunWithOffset = make_array_to_functor(vec, 1);
54  assert(vecFunWithOffset(1) == 3);
55 }
56 
57 void compare_functors() {
58  greater g;
59  greater_equal ge;
60  less l;
61  less_equal le;
62  equal_to e;
63  not_equal_to ne;
64 
65  assert(!g(1, 2));
66  assert(!g(1, 1));
67  assert(g(2, 1));
68 
69  assert(!ge(1, 2));
70  assert(ge(1, 1));
71  assert(ge(2, 1));
72 
73  assert(l(1, 2));
74  assert(!l(1, 1));
75  assert(!l(2, 1));
76 
77  assert(le(1, 2));
78  assert(le(1, 1));
79  assert(!le(2, 1));
80 
81  assert(!e(1, 2));
82  assert(e(1, 1));
83  assert(!e(2, 1));
84 
85  assert(ne(1, 2));
86  assert(!ne(1, 1));
87  assert(ne(2, 1));
88 }
89 
90 void comparator_functor() {
91  auto getFirst = [](std::pair<int, int> p) { return p.first; };
92  auto compareFirst = make_functor_to_comparator(getFirst);
93 
94  assert(!compareFirst(std::make_pair(1, 2), std::make_pair(0, 1)));
95 
96  auto compareFirstDesc = make_functor_to_comparator(getFirst, greater());
97 
98  assert(compareFirstDesc(std::make_pair(1, 2), std::make_pair(0, 1)));
99 }
100 
101 void boolean_functors() {
102  Not notFun;
103  Or orFun;
104  And andFun;
105 
106  assert(!notFun(true));
107  assert(notFun(false));
108 
109  assert(!orFun(false, false));
110  assert(orFun(true, false));
111  assert(orFun(false, true));
112  assert(orFun(true, true));
113 
114  assert(!andFun(false, false));
115  assert(!andFun(true, false));
116  assert(!andFun(false, true));
117  assert(andFun(true, true));
118 }
119 
120 void lift_operator_functor() {
121  auto oper = [](int a, int b) { return a + b > 0; };
122  return_zero_functor zero;
124 
125  auto f = make_lift_binary_operator_functor(zero, five, oper);
126 
127  assert(f(1, 2, 41243.2, "dada"));
128 }
129 
130 void boolean_functors_on_functors() {
131  always_true retTrue;
132  always_false retFalse;
133 
134  {
135  auto trueFunctor = make_not_functor(retFalse);
136  assert(trueFunctor(1.2, "xada", 3));
137  }
138 
139  {
140  auto falseFunctor = make_not_functor(retTrue);
141  assert(!falseFunctor(1.2, "xada", 3));
142  }
143 
144  {
145  auto trueFunctor = make_or_functor(retTrue, retFalse);
146  assert(trueFunctor(1.2, "xada", 3));
147  }
148 
149  {
150  auto falseFunctor = make_and_functor(retTrue, retFalse);
151  assert(!falseFunctor(1.2, "xada", 3));
152  }
153 
154  {
155  auto trueFunctor = make_xor_functor(retTrue, retFalse);
156  assert(trueFunctor(1.2, "xada", 3));
157  }
158 }
159 
160 int main() {
161  functors_example();
162  boolean_functors();
163  comparator_functor();
164  boolean_functors();
165  lift_operator_functor();
166  boolean_functors_on_functors();
167 
168  return 0;
169 }
auto make_or_functor(FunctorLeft left, FunctorRight right)
make for or_functor
Definition: functors.hpp:945
greater_equal functor
Definition: functors.hpp:516
equal_to functor
Definition: functors.hpp:556
less functor
Definition: functors.hpp:497
functor return false
Definition: functors.hpp:222
Functor returns always the same number. The number has to be known at compile time.
Definition: functors.hpp:69
auto make_xor_functor(FunctorLeft left, FunctorRight right)
make for Xor
Definition: functors.hpp:995
not_equal_to functor
Definition: functors.hpp:593
auto make_functor_to_comparator(Functor functor, Compare compare=Compare())
make for functor to comparator
Definition: functors.hpp:654
This file contains set of simple useful functors or functor adapters.
auto make_not_functor(Functor functor)
make for Not
Definition: functors.hpp:922
Functor does nothing.
Definition: functors.hpp:52
auto make_array_to_functor(const Array &a, int offset=0)
make function for array_to_functor
Definition: functors.hpp:364
auto make_lift_binary_operator_functor(FunctorLeft left, FunctorRight right, Operator op)
make function for lift_binary_operator_functor
Definition: functors.hpp:884
less_equal functor
Definition: functors.hpp:536
greater functor
Definition: functors.hpp:478
functor returns its argument
Definition: functors.hpp:128
auto make_and_functor(FunctorLeft left, FunctorRight right)
make and_functor
Definition: functors.hpp:970
functor return true
Definition: functors.hpp:227