summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/example/functional
diff options
context:
space:
mode:
Diffstat (limited to 'src/boost/libs/hana/example/functional')
-rw-r--r--src/boost/libs/hana/example/functional/always.cpp12
-rw-r--r--src/boost/libs/hana/example/functional/apply.cpp12
-rw-r--r--src/boost/libs/hana/example/functional/arg.cpp15
-rw-r--r--src/boost/libs/hana/example/functional/capture.cpp18
-rw-r--r--src/boost/libs/hana/example/functional/compose.cpp21
-rw-r--r--src/boost/libs/hana/example/functional/curry.cpp26
-rw-r--r--src/boost/libs/hana/example/functional/demux.cpp30
-rw-r--r--src/boost/libs/hana/example/functional/fix.cpp18
-rw-r--r--src/boost/libs/hana/example/functional/flip.cpp21
-rw-r--r--src/boost/libs/hana/example/functional/id.cpp12
-rw-r--r--src/boost/libs/hana/example/functional/infix.cpp20
-rw-r--r--src/boost/libs/hana/example/functional/iterate.cpp19
-rw-r--r--src/boost/libs/hana/example/functional/lockstep.cpp20
-rw-r--r--src/boost/libs/hana/example/functional/on.cpp36
-rw-r--r--src/boost/libs/hana/example/functional/overload.cpp31
-rw-r--r--src/boost/libs/hana/example/functional/overload_linearly.cpp22
-rw-r--r--src/boost/libs/hana/example/functional/partial.cpp13
-rw-r--r--src/boost/libs/hana/example/functional/placeholder.cpp21
-rw-r--r--src/boost/libs/hana/example/functional/reverse_partial.cpp14
19 files changed, 381 insertions, 0 deletions
diff --git a/src/boost/libs/hana/example/functional/always.cpp b/src/boost/libs/hana/example/functional/always.cpp
new file mode 100644
index 000000000..5c4594421
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/always.cpp
@@ -0,0 +1,12 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/functional/always.hpp>
+namespace hana = boost::hana;
+
+
+static_assert(hana::always(1)() == 1, "");
+static_assert(hana::always('2')(1, 2, 3) == '2', "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/apply.cpp b/src/boost/libs/hana/example/functional/apply.cpp
new file mode 100644
index 000000000..a5686a9c6
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/apply.cpp
@@ -0,0 +1,12 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/functional/apply.hpp>
+#include <boost/hana/plus.hpp>
+namespace hana = boost::hana;
+
+
+static_assert(hana::apply(hana::plus, 1, 2) == 3, "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/arg.cpp b/src/boost/libs/hana/example/functional/arg.cpp
new file mode 100644
index 000000000..53030a668
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/arg.cpp
@@ -0,0 +1,15 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/functional/arg.hpp>
+namespace hana = boost::hana;
+
+
+// hana::arg<0>(1, '2', 3.3); // static assertion (regardless of the number of arguments)
+static_assert(hana::arg<1>(1, '2', 3.3) == 1, "");
+static_assert(hana::arg<2>(1, '2', 3.3) == '2', "");
+static_assert(hana::arg<3>(1, '2', 3.3) == 3.3, "");
+// hana::arg<4>(1, '2', 3.3); // static assertion
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/capture.cpp b/src/boost/libs/hana/example/functional/capture.cpp
new file mode 100644
index 000000000..588c9b3f7
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/capture.cpp
@@ -0,0 +1,18 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/assert.hpp>
+#include <boost/hana/config.hpp>
+#include <boost/hana/functional/capture.hpp>
+namespace hana = boost::hana;
+
+
+int main() {
+ BOOST_HANA_CONSTEXPR_LAMBDA auto sum = [](auto x, auto y, auto z) {
+ return x + y + z;
+ };
+
+ BOOST_HANA_CONSTEXPR_CHECK(hana::capture(1, 2, 3)(sum)() == 6);
+ BOOST_HANA_CONSTEXPR_CHECK(hana::capture(1, 2)(sum)(3) == 6);
+}
diff --git a/src/boost/libs/hana/example/functional/compose.cpp b/src/boost/libs/hana/example/functional/compose.cpp
new file mode 100644
index 000000000..a1e026df3
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/compose.cpp
@@ -0,0 +1,21 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/assert.hpp>
+#include <boost/hana/config.hpp>
+#include <boost/hana/functional/compose.hpp>
+namespace hana = boost::hana;
+
+
+int main() {
+ BOOST_HANA_CONSTEXPR_LAMBDA auto to_char = [](int x) {
+ return static_cast<char>(x + 48);
+ };
+
+ BOOST_HANA_CONSTEXPR_LAMBDA auto increment = [](auto x) {
+ return x + 1;
+ };
+
+ BOOST_HANA_CONSTEXPR_CHECK(hana::compose(to_char, increment)(3) == '4');
+}
diff --git a/src/boost/libs/hana/example/functional/curry.cpp b/src/boost/libs/hana/example/functional/curry.cpp
new file mode 100644
index 000000000..6e24b039b
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/curry.cpp
@@ -0,0 +1,26 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/assert.hpp>
+#include <boost/hana/config.hpp>
+#include <boost/hana/functional/curry.hpp>
+namespace hana = boost::hana;
+
+
+int main() {
+ BOOST_HANA_CONSTEXPR_LAMBDA auto add = [](auto x, auto y, auto z) {
+ return x + y + z;
+ };
+
+ BOOST_HANA_CONSTEXPR_CHECK(hana::curry<3>(add)(1)(2)(3) == 1 + 2 + 3);
+ BOOST_HANA_CONSTEXPR_CHECK(hana::curry<3>(add)(1)(2, 3) == hana::curry<3>(add)(1)(2)(3));
+ BOOST_HANA_CONSTEXPR_CHECK(hana::curry<3>(add)(1, 2, 3) == hana::curry<3>(add)(1)(2)(3));
+
+ // curry with a nullary function
+ BOOST_HANA_CONSTEXPR_LAMBDA auto two = []() {
+ return 2;
+ };
+
+ BOOST_HANA_CONSTEXPR_CHECK(hana::curry<0>(two)() == two());
+}
diff --git a/src/boost/libs/hana/example/functional/demux.cpp b/src/boost/libs/hana/example/functional/demux.cpp
new file mode 100644
index 000000000..8dfd69eba
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/demux.cpp
@@ -0,0 +1,30 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/equal.hpp>
+#include <boost/hana/functional/demux.hpp>
+#include <boost/hana/functional/placeholder.hpp>
+#include <boost/hana/tuple.hpp>
+namespace hana = boost::hana;
+using hana::_;
+
+
+constexpr auto f = hana::demux(hana::make_tuple)(
+ _ + _,
+ _ - _,
+ _ * _,
+ _ / _
+);
+
+static_assert(
+ f(10, 4) == hana::make_tuple(
+ 10 + 4,
+ 10 - 4,
+ 10 * 4,
+ 10 / 4
+ )
+, "");
+
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/fix.cpp b/src/boost/libs/hana/example/functional/fix.cpp
new file mode 100644
index 000000000..62ad03742
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/fix.cpp
@@ -0,0 +1,18 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/assert.hpp>
+#include <boost/hana/config.hpp>
+#include <boost/hana/functional/fix.hpp>
+namespace hana = boost::hana;
+
+
+BOOST_HANA_CONSTEXPR_STATELESS_LAMBDA auto factorial = hana::fix([](auto fact, auto n) -> int {
+ if (n == 0) return 1;
+ else return n * fact(n - 1);
+});
+
+int main() {
+ BOOST_HANA_CONSTEXPR_CHECK(factorial(5) == 120);
+}
diff --git a/src/boost/libs/hana/example/functional/flip.cpp b/src/boost/libs/hana/example/functional/flip.cpp
new file mode 100644
index 000000000..9afb2bcad
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/flip.cpp
@@ -0,0 +1,21 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/assert.hpp>
+#include <boost/hana/config.hpp>
+#include <boost/hana/functional/flip.hpp>
+namespace hana = boost::hana;
+
+
+BOOST_HANA_CONSTEXPR_LAMBDA auto minus = [](int x, int y, int z = 0) {
+ return x - y - z;
+};
+
+int main() {
+ BOOST_HANA_CONSTEXPR_CHECK(minus(3, 0) == 3 - 0);
+ BOOST_HANA_CONSTEXPR_CHECK(hana::flip(minus)(3, 0) == 0 - 3);
+
+ BOOST_HANA_CONSTEXPR_CHECK(minus(3, 0, 1) == 3 - 0 - 1);
+ BOOST_HANA_CONSTEXPR_CHECK(hana::flip(minus)(3, 0, 1) == 0 - 3 - 1);
+}
diff --git a/src/boost/libs/hana/example/functional/id.cpp b/src/boost/libs/hana/example/functional/id.cpp
new file mode 100644
index 000000000..16a39c1e7
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/id.cpp
@@ -0,0 +1,12 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/functional/id.hpp>
+namespace hana = boost::hana;
+
+
+static_assert(hana::id(1) == 1, "");
+static_assert(hana::id('x') == 'x', "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/infix.cpp b/src/boost/libs/hana/example/functional/infix.cpp
new file mode 100644
index 000000000..3b74044d8
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/infix.cpp
@@ -0,0 +1,20 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/assert.hpp>
+#include <boost/hana/config.hpp>
+#include <boost/hana/equal.hpp>
+#include <boost/hana/functional/infix.hpp>
+#include <boost/hana/pair.hpp>
+namespace hana = boost::hana;
+
+
+BOOST_HANA_CONSTEXPR_LAMBDA auto divmod = hana::infix([](auto x, auto y) {
+ // this could be a more efficient implementation
+ return hana::make_pair(x / y, x % y);
+});
+
+int main() {
+ BOOST_HANA_CONSTEXPR_CHECK((42 ^divmod^ 23) == hana::make_pair(1, 19));
+}
diff --git a/src/boost/libs/hana/example/functional/iterate.cpp b/src/boost/libs/hana/example/functional/iterate.cpp
new file mode 100644
index 000000000..40aa7c93c
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/iterate.cpp
@@ -0,0 +1,19 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/equal.hpp>
+#include <boost/hana/functional/iterate.hpp>
+#include <boost/hana/functional/placeholder.hpp>
+#include <boost/hana/tuple.hpp>
+namespace hana = boost::hana;
+
+
+constexpr auto next_10 = hana::iterate<10>(hana::_ + 1);
+static_assert(next_10(3) == 13, "");
+
+constexpr auto xs = hana::make_tuple(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
+static_assert(hana::iterate<3>(hana::make_tuple, xs) ==
+ hana::make_tuple(hana::make_tuple(hana::make_tuple(xs))), "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/lockstep.cpp b/src/boost/libs/hana/example/functional/lockstep.cpp
new file mode 100644
index 000000000..cbb8266ca
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/lockstep.cpp
@@ -0,0 +1,20 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/functional/lockstep.hpp>
+#include <boost/hana/plus.hpp>
+namespace hana = boost::hana;
+
+
+constexpr int to_int(char c) {
+ return static_cast<int>(c) - 48;
+}
+
+constexpr int increment(int i) {
+ return i + 1;
+}
+
+static_assert(hana::lockstep(hana::plus)(to_int, increment)('3', 4) == 3 + 5, "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/on.cpp b/src/boost/libs/hana/example/functional/on.cpp
new file mode 100644
index 000000000..bc5e5662b
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/on.cpp
@@ -0,0 +1,36 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/first.hpp>
+#include <boost/hana/functional/on.hpp>
+#include <boost/hana/integral_constant.hpp>
+#include <boost/hana/less.hpp>
+#include <boost/hana/pair.hpp>
+#include <boost/hana/plus.hpp>
+#include <boost/hana/sort.hpp>
+#include <boost/hana/tuple.hpp>
+#include <boost/hana/type.hpp>
+namespace hana = boost::hana;
+
+
+// infix application
+constexpr auto sorted = hana::sort.by(hana::less ^hana::on^ hana::first, hana::make_tuple(
+ hana::make_pair(hana::int_c<3>, 'x'),
+ hana::make_pair(hana::int_c<1>, hana::type_c<void>),
+ hana::make_pair(hana::int_c<2>, 9876)
+));
+
+static_assert(sorted == hana::make_tuple(
+ hana::make_pair(hana::int_c<1>, hana::type_c<void>),
+ hana::make_pair(hana::int_c<2>, 9876),
+ hana::make_pair(hana::int_c<3>, 'x')
+), "");
+
+
+// function call syntax
+constexpr auto x = hana::make_pair(1, 2);
+constexpr auto y = hana::make_pair(10, 20);
+static_assert(hana::on(hana::plus, hana::first)(x, y) == 1 + 10, "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/overload.cpp b/src/boost/libs/hana/example/functional/overload.cpp
new file mode 100644
index 000000000..2a78f7b5d
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/overload.cpp
@@ -0,0 +1,31 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/assert.hpp>
+#include <boost/hana/functional/overload.hpp>
+
+#include <iostream>
+#include <string>
+namespace hana = boost::hana;
+
+
+auto on_string = [](std::string const& s) {
+ std::cout << "matched std::string: " << s << std::endl;
+ return s;
+};
+
+auto on_int = [](int i) {
+ std::cout << "matched int: " << i << std::endl;
+ return i;
+};
+
+auto f = hana::overload(on_int, on_string);
+
+int main() {
+ // prints "matched int: 1"
+ BOOST_HANA_RUNTIME_CHECK(f(1) == 1);
+
+ // prints "matched std::string: abcdef"
+ BOOST_HANA_RUNTIME_CHECK(f("abcdef") == std::string{"abcdef"});
+}
diff --git a/src/boost/libs/hana/example/functional/overload_linearly.cpp b/src/boost/libs/hana/example/functional/overload_linearly.cpp
new file mode 100644
index 000000000..632234f56
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/overload_linearly.cpp
@@ -0,0 +1,22 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/assert.hpp>
+#include <boost/hana/functional/overload_linearly.hpp>
+
+#include <string>
+namespace hana = boost::hana;
+
+
+auto f = hana::overload_linearly(
+ [](int i) { return i + 1; },
+ [](std::string s) { return s + "d"; },
+ [](double) { BOOST_HANA_RUNTIME_CHECK(false && "never called"); }
+);
+
+int main() {
+ BOOST_HANA_RUNTIME_CHECK(f(1) == 2);
+ BOOST_HANA_RUNTIME_CHECK(f("abc") == "abcd");
+ BOOST_HANA_RUNTIME_CHECK(f(2.2) == static_cast<int>(2.2) + 1);
+}
diff --git a/src/boost/libs/hana/example/functional/partial.cpp b/src/boost/libs/hana/example/functional/partial.cpp
new file mode 100644
index 000000000..ac40f12a4
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/partial.cpp
@@ -0,0 +1,13 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/functional/partial.hpp>
+#include <boost/hana/plus.hpp>
+namespace hana = boost::hana;
+
+
+constexpr auto increment = hana::partial(hana::plus, 1);
+static_assert(increment(2) == 3, "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/placeholder.cpp b/src/boost/libs/hana/example/functional/placeholder.cpp
new file mode 100644
index 000000000..31cbc4a55
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/placeholder.cpp
@@ -0,0 +1,21 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/functional/placeholder.hpp>
+namespace hana = boost::hana;
+
+
+constexpr auto plus = hana::_ + hana::_;
+static_assert(plus(1, 2) == 1 + 2, "");
+
+constexpr auto increment = hana::_ + 1;
+static_assert(increment(1) == 2, "");
+
+constexpr auto twice = 2 * hana::_;
+static_assert(twice(1) == 2, "");
+
+// Extra arguments are ignored.
+static_assert(twice(1, "ignored") == 2, "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/functional/reverse_partial.cpp b/src/boost/libs/hana/example/functional/reverse_partial.cpp
new file mode 100644
index 000000000..45ba8f18b
--- /dev/null
+++ b/src/boost/libs/hana/example/functional/reverse_partial.cpp
@@ -0,0 +1,14 @@
+// Copyright Louis Dionne 2013-2017
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
+
+#include <boost/hana/div.hpp>
+#include <boost/hana/functional/reverse_partial.hpp>
+namespace hana = boost::hana;
+
+
+constexpr auto half = hana::reverse_partial(hana::div, 2);
+static_assert(half(4) == 2, "");
+static_assert(half(8) == 4, "");
+
+int main() { }