• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2017 Paul Fultz II
3     lambda.cpp
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #include <boost/hof/lambda.hpp>
8 #include <boost/hof/first_of.hpp>
9 #include <boost/hof/partial.hpp>
10 #include <boost/hof/infix.hpp>
11 #include <boost/hof/pipable.hpp>
12 #include <memory>
13 #include "test.hpp"
14 
15 
16 static constexpr auto add_one = BOOST_HOF_STATIC_LAMBDA(int x)
17 {
18     return x + 1;
19 };
20 
21 template<class F>
22 struct wrapper : F
23 {
24     BOOST_HOF_INHERIT_CONSTRUCTOR(wrapper, F)
25 };
26 
27 template<class T>
wrap(T x)28 constexpr wrapper<T> wrap(T x)
29 {
30     return x;
31 }
32 
BOOST_HOF_TEST_CASE()33 BOOST_HOF_TEST_CASE()
34 {
35     BOOST_HOF_TEST_CHECK(3 == add_one(2));
36 }
37 
BOOST_HOF_TEST_CASE()38 BOOST_HOF_TEST_CASE()
39 {
40     constexpr auto add_one_again = add_one;
41     BOOST_HOF_TEST_CHECK(3 == add_one_again(2));
42 }
43 
BOOST_HOF_TEST_CASE()44 BOOST_HOF_TEST_CASE()
45 {
46     constexpr auto add_one_again = wrap(add_one);
47     BOOST_HOF_TEST_CHECK(3 == add_one_again(2));
48 }
49 
50 namespace test_static {
51 
52 BOOST_HOF_STATIC_LAMBDA_FUNCTION(add_one) = [](int x)
__anone40d8c1e0102(int x) 53 {
54     return x + 1;
55 };
56 
BOOST_HOF_TEST_CASE()57 BOOST_HOF_TEST_CASE()
58 {
59     BOOST_HOF_TEST_CHECK(add_one(2) == 3);
60 }
61 
62 BOOST_HOF_STATIC_LAMBDA_FUNCTION(sum_partial) = boost::hof::partial([](int x, int y)
__anone40d8c1e0202(int x, int y) 63 {
64     return x + y;
65 });
66 
BOOST_HOF_TEST_CASE()67 BOOST_HOF_TEST_CASE()
68 {
69 #ifndef _MSC_VER
70     STATIC_ASSERT_EMPTY(sum_partial);
71 #endif
72     BOOST_HOF_TEST_CHECK(3 == sum_partial(1, 2));
73     BOOST_HOF_TEST_CHECK(3 == sum_partial(1)(2));
74 }
75 
76 BOOST_HOF_STATIC_LAMBDA_FUNCTION(add_one_pipable) = boost::hof::pipable([](int x)
__anone40d8c1e0302(int x) 77 {
78     return x + 1;
79 });
80 
BOOST_HOF_TEST_CASE()81 BOOST_HOF_TEST_CASE()
82 {
83 #ifndef _MSC_VER
84     STATIC_ASSERT_EMPTY(add_one_pipable);
85 #endif
86     BOOST_HOF_TEST_CHECK(3 == add_one_pipable(2));
87     BOOST_HOF_TEST_CHECK(3 == (2 | add_one_pipable));
88 }
89 
90 BOOST_HOF_STATIC_LAMBDA_FUNCTION(sum_infix) = boost::hof::infix([](int x, int y)
__anone40d8c1e0402(int x, int y) 91 {
92     return x + y;
93 });
94 
BOOST_HOF_TEST_CASE()95 BOOST_HOF_TEST_CASE()
96 {
97 #ifndef _MSC_VER
98     STATIC_ASSERT_EMPTY(sum_infix);
99 #endif
100     BOOST_HOF_TEST_CHECK(3 == (1 <sum_infix> 2));
101 }
102 
103 }
104