2
0
mirror of https://github.com/boostorg/hana.git synced 2026-02-01 08:32:11 +00:00
Files
hana/example/functor.cpp
Louis Dionne dab93cc263 [Comparable] Update docs, split methods and more
- Use == for cross-type EqualityComparable data types
- Remove the not_equal mcd
2015-02-10 19:18:40 -05:00

90 lines
2.2 KiB
C++

/*
@copyright Louis Dionne 2014
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/detail/constexpr.hpp>
#include <boost/hana/maybe.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>
#include <boost/hana/type_list.hpp>
#include <sstream>
#include <string>
#include <type_traits>
using namespace boost::hana;
using namespace std::literals;
int main() {
{
//! [adjust]
BOOST_HANA_CONSTEXPR_LAMBDA auto negative = [](auto x) {
return x < 0;
};
BOOST_HANA_CONSTEXPR_LAMBDA auto negate = [](auto x) {
return -x;
};
BOOST_HANA_CONSTEXPR_CHECK(
adjust(tuple(-3, -2, -1, 0, 1, 2, 3), negative, negate)
==
tuple(3, 2, 1, 0, 1, 2, 3)
);
//! [adjust]
}
{
//! [fill]
BOOST_HANA_CONSTEXPR_CHECK(
fill(tuple(1, '2', 3.3, nullptr), 'x') == tuple('x', 'x', 'x', 'x')
);
BOOST_HANA_CONSTANT_CHECK(fill(nothing, 'x') == nothing);
BOOST_HANA_CONSTEXPR_CHECK(fill(just('y'), 'x') == just('x'));
//! [fill]
}
{
//! [fmap]
auto to_string = [](auto x) {
std::ostringstream tmp;
tmp << x;
return tmp.str();
};
BOOST_HANA_RUNTIME_CHECK(
fmap(tuple(1, '2', "345", std::string{"67"}), to_string)
==
tuple("1"s, "2"s, "345"s, "67"s)
);
BOOST_HANA_CONSTANT_CHECK(fmap(nothing, to_string) == nothing);
BOOST_HANA_RUNTIME_CHECK(fmap(just(123), to_string) == just("123"s));
BOOST_HANA_CONSTANT_CHECK(
fmap(type_list<void, int(), char[10]>, template_<std::add_pointer_t>)
==
type_list<void*, int(*)(), char(*)[10]>
);
//! [fmap]
}
{
//! [replace]
BOOST_HANA_CONSTEXPR_LAMBDA auto negative = [](auto x) {
return x < 0;
};
BOOST_HANA_CONSTEXPR_CHECK(
replace(tuple(-3, -2, -1, 0, 1, 2, 3), negative, 0)
==
tuple(0, 0, 0, 0, 1, 2, 3)
);
//! [replace]
}
}