summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/example/optional
diff options
context:
space:
mode:
Diffstat (limited to 'src/boost/libs/hana/example/optional')
-rw-r--r--src/boost/libs/hana/example/optional/applicative.complex.cpp50
-rw-r--r--src/boost/libs/hana/example/optional/applicative.cpp19
-rw-r--r--src/boost/libs/hana/example/optional/comparable.cpp17
-rw-r--r--src/boost/libs/hana/example/optional/foldable.cpp14
-rw-r--r--src/boost/libs/hana/example/optional/functor.cpp16
-rw-r--r--src/boost/libs/hana/example/optional/is_just.cpp15
-rw-r--r--src/boost/libs/hana/example/optional/is_nothing.cpp15
-rw-r--r--src/boost/libs/hana/example/optional/just.cpp13
-rw-r--r--src/boost/libs/hana/example/optional/make.cpp20
-rw-r--r--src/boost/libs/hana/example/optional/maybe.cpp13
-rw-r--r--src/boost/libs/hana/example/optional/monad.cpp23
-rw-r--r--src/boost/libs/hana/example/optional/monad_plus.cpp18
-rw-r--r--src/boost/libs/hana/example/optional/nothing.cpp13
-rw-r--r--src/boost/libs/hana/example/optional/orderable.cpp17
-rw-r--r--src/boost/libs/hana/example/optional/searchable.cpp27
-rw-r--r--src/boost/libs/hana/example/optional/sfinae.cpp21
-rw-r--r--src/boost/libs/hana/example/optional/sfinae_friendly_metafunctions.cpp44
-rw-r--r--src/boost/libs/hana/example/optional/value.cpp20
-rw-r--r--src/boost/libs/hana/example/optional/value_or.cpp13
19 files changed, 388 insertions, 0 deletions
diff --git a/src/boost/libs/hana/example/optional/applicative.complex.cpp b/src/boost/libs/hana/example/optional/applicative.complex.cpp
new file mode 100644
index 000000000..f8eb84a73
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/applicative.complex.cpp
@@ -0,0 +1,50 @@
+// 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/ap.hpp>
+#include <boost/hana/assert.hpp>
+#include <boost/hana/bool.hpp>
+#include <boost/hana/config.hpp>
+#include <boost/hana/equal.hpp>
+#include <boost/hana/if.hpp>
+#include <boost/hana/lift.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+template <char op>
+constexpr auto function = hana::nothing;
+
+template <>
+BOOST_HANA_CONSTEXPR_LAMBDA auto function<'+'> = hana::just([](auto x, auto y) {
+ return x + y;
+});
+
+template <>
+BOOST_HANA_CONSTEXPR_LAMBDA auto function<'-'> = hana::just([](auto x, auto y) {
+ return x - y;
+});
+
+// and so on...
+
+template <char n>
+constexpr auto digit = hana::if_(hana::bool_c<(n >= '0' && n <= '9')>,
+ hana::just(static_cast<int>(n - 48)),
+ hana::nothing
+);
+
+template <char x, char op, char y>
+BOOST_HANA_CONSTEXPR_LAMBDA auto evaluate = hana::ap(function<op>, digit<x>, digit<y>);
+
+int main() {
+ BOOST_HANA_CONSTEXPR_CHECK(evaluate<'1', '+', '2'> == hana::just(1 + 2));
+ BOOST_HANA_CONSTEXPR_CHECK(evaluate<'4', '-', '2'> == hana::just(4 - 2));
+
+ BOOST_HANA_CONSTANT_CHECK(evaluate<'?', '+', '2'> == hana::nothing);
+ BOOST_HANA_CONSTANT_CHECK(evaluate<'1', '?', '2'> == hana::nothing);
+ BOOST_HANA_CONSTANT_CHECK(evaluate<'1', '+', '?'> == hana::nothing);
+ BOOST_HANA_CONSTANT_CHECK(evaluate<'?', '?', '?'> == hana::nothing);
+
+ static_assert(hana::lift<hana::optional_tag>(123) == hana::just(123), "");
+}
diff --git a/src/boost/libs/hana/example/optional/applicative.cpp b/src/boost/libs/hana/example/optional/applicative.cpp
new file mode 100644
index 000000000..3c67c30e9
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/applicative.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/ap.hpp>
+#include <boost/hana/assert.hpp>
+#include <boost/hana/equal.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+constexpr char next(char c) { return c + 1; }
+
+static_assert(hana::ap(hana::just(next), hana::just('x')) == hana::just('y'), "");
+BOOST_HANA_CONSTANT_CHECK(hana::ap(hana::nothing, hana::just('x')) == hana::nothing);
+BOOST_HANA_CONSTANT_CHECK(hana::ap(hana::just(next), hana::nothing) == hana::nothing);
+BOOST_HANA_CONSTANT_CHECK(hana::ap(hana::nothing, hana::nothing) == hana::nothing);
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/comparable.cpp b/src/boost/libs/hana/example/optional/comparable.cpp
new file mode 100644
index 000000000..ba192b852
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/comparable.cpp
@@ -0,0 +1,17 @@
+// 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/equal.hpp>
+#include <boost/hana/not_equal.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+BOOST_HANA_CONSTANT_CHECK(hana::nothing == hana::nothing);
+static_assert(hana::just('x') == hana::just('x'), "");
+static_assert(hana::just('x') != hana::just('y'), "");
+BOOST_HANA_CONSTANT_CHECK(hana::just('x') != hana::nothing);
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/foldable.cpp b/src/boost/libs/hana/example/optional/foldable.cpp
new file mode 100644
index 000000000..21fc920c6
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/foldable.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/fold_right.hpp>
+#include <boost/hana/optional.hpp>
+#include <boost/hana/plus.hpp>
+namespace hana = boost::hana;
+
+
+static_assert(hana::fold_right(hana::nothing, 1, hana::plus) == 1, "");
+static_assert(hana::fold_right(hana::just(4), 1, hana::plus) == 5, "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/functor.cpp b/src/boost/libs/hana/example/optional/functor.cpp
new file mode 100644
index 000000000..6faf54a0f
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/functor.cpp
@@ -0,0 +1,16 @@
+// 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/equal.hpp>
+#include <boost/hana/functional/placeholder.hpp>
+#include <boost/hana/optional.hpp>
+#include <boost/hana/transform.hpp>
+namespace hana = boost::hana;
+
+
+BOOST_HANA_CONSTANT_CHECK(hana::transform(hana::nothing, hana::_ + 1) == hana::nothing);
+static_assert(hana::transform(hana::just(1), hana::_ + 1) == hana::just(2), "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/is_just.cpp b/src/boost/libs/hana/example/optional/is_just.cpp
new file mode 100644
index 000000000..7c7216ace
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/is_just.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/assert.hpp>
+#include <boost/hana/not.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+BOOST_HANA_CONSTANT_CHECK( hana::is_just(hana::just('x')));
+BOOST_HANA_CONSTANT_CHECK( hana::is_just(hana::just(hana::nothing)));
+BOOST_HANA_CONSTANT_CHECK(!hana::is_just(hana::nothing));
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/is_nothing.cpp b/src/boost/libs/hana/example/optional/is_nothing.cpp
new file mode 100644
index 000000000..ffde02df5
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/is_nothing.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/assert.hpp>
+#include <boost/hana/not.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+BOOST_HANA_CONSTANT_CHECK( hana::is_nothing(hana::nothing));
+BOOST_HANA_CONSTANT_CHECK(!hana::is_nothing(hana::just('x')));
+BOOST_HANA_CONSTANT_CHECK(!hana::is_nothing(hana::just(hana::nothing)));
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/just.cpp b/src/boost/libs/hana/example/optional/just.cpp
new file mode 100644
index 000000000..bf7d32042
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/just.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/assert.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+constexpr auto just_x = hana::just('x');
+BOOST_HANA_CONSTANT_CHECK(hana::is_just(just_x));
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/make.cpp b/src/boost/libs/hana/example/optional/make.cpp
new file mode 100644
index 000000000..fd0034a76
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/make.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/core/make.hpp>
+#include <boost/hana/equal.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+int main() {
+ constexpr auto x = hana::make<hana::optional_tag>();
+ BOOST_HANA_CONSTANT_CHECK(x == hana::make_optional());
+ BOOST_HANA_CONSTANT_CHECK(hana::is_nothing(x));
+
+ constexpr auto just_x = hana::make<hana::optional_tag>('x');
+ static_assert(just_x == hana::make_optional('x'), "");
+ BOOST_HANA_CONSTANT_CHECK(hana::is_just(just_x));
+}
diff --git a/src/boost/libs/hana/example/optional/maybe.cpp b/src/boost/libs/hana/example/optional/maybe.cpp
new file mode 100644
index 000000000..5e90cb652
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/maybe.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/placeholder.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+static_assert(hana::maybe('x', hana::_ + 1, hana::just(1)) == 2, "");
+static_assert(hana::maybe('x', hana::_ + 1, hana::nothing) == 'x', "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/monad.cpp b/src/boost/libs/hana/example/optional/monad.cpp
new file mode 100644
index 000000000..2be7821a6
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/monad.cpp
@@ -0,0 +1,23 @@
+// 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/chain.hpp>
+#include <boost/hana/config.hpp>
+#include <boost/hana/equal.hpp>
+#include <boost/hana/flatten.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+int main() {
+ BOOST_HANA_CONSTEXPR_LAMBDA auto inc = [](auto x) {
+ return hana::just(x + 1);
+ };
+
+ BOOST_HANA_CONSTEXPR_CHECK(hana::chain(hana::just(1), inc) == hana::just(2));
+ BOOST_HANA_CONSTANT_CHECK(hana::chain(hana::nothing, inc) == hana::nothing);
+
+ BOOST_HANA_CONSTEXPR_CHECK(hana::flatten(hana::just(hana::just(2))) == hana::just(2));
+}
diff --git a/src/boost/libs/hana/example/optional/monad_plus.cpp b/src/boost/libs/hana/example/optional/monad_plus.cpp
new file mode 100644
index 000000000..d32681526
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/monad_plus.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/concat.hpp>
+#include <boost/hana/empty.hpp>
+#include <boost/hana/equal.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+static_assert(hana::concat(hana::nothing, hana::just('x')) == hana::just('x'), "");
+BOOST_HANA_CONSTANT_CHECK(hana::concat(hana::nothing, hana::nothing) == hana::nothing);
+static_assert(hana::concat(hana::just('x'), hana::just('y')) == hana::just('x'), "");
+BOOST_HANA_CONSTANT_CHECK(hana::empty<hana::optional_tag>() == hana::nothing);
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/nothing.cpp b/src/boost/libs/hana/example/optional/nothing.cpp
new file mode 100644
index 000000000..81f109e90
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/nothing.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/assert.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+constexpr auto x = hana::nothing;
+BOOST_HANA_CONSTANT_CHECK(hana::is_nothing(x));
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/orderable.cpp b/src/boost/libs/hana/example/optional/orderable.cpp
new file mode 100644
index 000000000..b8cb7e110
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/orderable.cpp
@@ -0,0 +1,17 @@
+// 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/greater.hpp>
+#include <boost/hana/less.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+BOOST_HANA_CONSTANT_CHECK(hana::nothing < hana::just(3));
+BOOST_HANA_CONSTANT_CHECK(hana::just(0) > hana::nothing);
+static_assert(hana::just(1) < hana::just(3), "");
+static_assert(hana::just(3) > hana::just(2), "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/searchable.cpp b/src/boost/libs/hana/example/optional/searchable.cpp
new file mode 100644
index 000000000..e95d451af
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/searchable.cpp
@@ -0,0 +1,27 @@
+// 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/all_of.hpp>
+#include <boost/hana/assert.hpp>
+#include <boost/hana/equal.hpp>
+#include <boost/hana/find_if.hpp>
+#include <boost/hana/integral_constant.hpp>
+#include <boost/hana/mod.hpp>
+#include <boost/hana/not_equal.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+auto odd = [](auto x) {
+ return x % hana::int_c<2> != hana::int_c<0>;
+};
+
+BOOST_HANA_CONSTANT_CHECK(hana::find_if(hana::just(hana::int_c<3>), odd) == hana::just(hana::int_c<3>));
+BOOST_HANA_CONSTANT_CHECK(hana::find_if(hana::just(hana::int_c<2>), odd) == hana::nothing);
+BOOST_HANA_CONSTANT_CHECK(hana::find_if(hana::nothing, odd) == hana::nothing);
+
+BOOST_HANA_CONSTANT_CHECK(hana::all_of(hana::just(hana::int_c<3>), odd));
+BOOST_HANA_CONSTANT_CHECK(hana::all_of(hana::nothing, odd));
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/sfinae.cpp b/src/boost/libs/hana/example/optional/sfinae.cpp
new file mode 100644
index 000000000..1fe9adf0e
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/sfinae.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/equal.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+int main() {
+ BOOST_HANA_CONSTEXPR_LAMBDA auto incr = [](auto x) -> decltype(x + 1) {
+ return x + 1;
+ };
+
+ BOOST_HANA_CONSTEXPR_CHECK(hana::sfinae(incr)(1) == hana::just(2));
+
+ struct invalid { };
+ BOOST_HANA_CONSTANT_CHECK(hana::sfinae(incr)(invalid{}) == hana::nothing);
+}
diff --git a/src/boost/libs/hana/example/optional/sfinae_friendly_metafunctions.cpp b/src/boost/libs/hana/example/optional/sfinae_friendly_metafunctions.cpp
new file mode 100644
index 000000000..392154b39
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/sfinae_friendly_metafunctions.cpp
@@ -0,0 +1,44 @@
+// 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/equal.hpp>
+#include <boost/hana/not.hpp>
+#include <boost/hana/optional.hpp>
+#include <boost/hana/traits.hpp>
+#include <boost/hana/type.hpp>
+
+#include <type_traits>
+#include <utility>
+namespace hana = boost::hana;
+
+
+template <typename ...>
+using void_t = void;
+
+template <typename T, typename = void>
+struct has_type : std::false_type { };
+
+template <typename T>
+struct has_type<T, void_t<typename T::type>>
+ : std::true_type
+{ };
+
+auto common_type_impl = hana::sfinae([](auto t, auto u) -> hana::type<
+ decltype(true ? hana::traits::declval(t) : hana::traits::declval(u))
+> { return {}; });
+
+template <typename T, typename U>
+using common_type = decltype(common_type_impl(hana::type_c<T>, hana::type_c<U>));
+
+BOOST_HANA_CONSTANT_CHECK(
+ common_type_impl(hana::type_c<int>, hana::type_c<float>)
+ ==
+ hana::just(hana::type_c<float>)
+);
+
+static_assert(!has_type<common_type<int, int*>>{}, "");
+static_assert(std::is_same<common_type<int, float>::type, float>{}, "");
+
+int main() { }
diff --git a/src/boost/libs/hana/example/optional/value.cpp b/src/boost/libs/hana/example/optional/value.cpp
new file mode 100644
index 000000000..adbb7aac5
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/value.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/equal.hpp>
+#include <boost/hana/optional.hpp>
+#include <boost/hana/type.hpp>
+
+#include <string>
+namespace hana = boost::hana;
+
+
+int main() {
+ static_assert(hana::just('x').value() == 'x', "");
+ BOOST_HANA_CONSTANT_CHECK(*hana::just(hana::type_c<int>) == hana::type_c<int>);
+ BOOST_HANA_RUNTIME_CHECK(hana::just(std::string{"abcd"})->size() == 4);
+
+ // hana::nothing.value(); // compile-time error
+}
diff --git a/src/boost/libs/hana/example/optional/value_or.cpp b/src/boost/libs/hana/example/optional/value_or.cpp
new file mode 100644
index 000000000..1d0b22960
--- /dev/null
+++ b/src/boost/libs/hana/example/optional/value_or.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/assert.hpp>
+#include <boost/hana/optional.hpp>
+namespace hana = boost::hana;
+
+
+static_assert(hana::just(1).value_or('x') == 1, "");
+static_assert(hana::nothing.value_or('x') == 'x', "");
+
+int main() { }