2
0
mirror of https://github.com/boostorg/hana.git synced 2026-02-01 08:32:11 +00:00
Files
hana/example/range.cpp
Louis Dionne 2c86ba2390 [Foldable] Rename the different fold variants
- Rename fold{l,r,l1,r1} to fold.{right,left} with overloads.
- Rename foldlM/foldrM to monadic_fold.{left,right}
- Add no-state variants of the monadic folds
- Improve the fold's documentation
- Deprecate the previous folds; they'll be removed soon

Fixes #18.
2015-03-30 17:29:37 -04:00

81 lines
2.1 KiB
C++

/*
@copyright Louis Dionne 2015
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/integral_constant.hpp>
#include <boost/hana/maybe.hpp>
#include <boost/hana/monoid.hpp>
#include <boost/hana/range.hpp>
#include <boost/hana/tuple.hpp>
using namespace boost::hana;
int main() {
{
//! [make<Range>]
constexpr auto irange = make<Range>(int_<0>, int_<10>); // [0, 10) int
constexpr auto lrange = make<Range>(int_<0>, long_<10>); // [0, 10) long
BOOST_HANA_CONSTANT_CHECK(lrange == make<Range>(long_<0>, long_<10>));
//! [make<Range>]
(void)irange;
}{
//! [comparable]
// empty ranges are equal
BOOST_HANA_CONSTANT_CHECK(range(int_<6>, int_<6>) == range(int_<0>, int_<0>));
// otherwise, ranges are equal if and only if they span the same interval
BOOST_HANA_CONSTANT_CHECK(range(int_<2>, int_<5>) == range(int_<2>, int_<5>));
BOOST_HANA_CONSTANT_CHECK(range(int_<0>, int_<3>) != range(int_<-1>, int_<3>));
//! [comparable]
}{
//! [foldable]
BOOST_HANA_CONSTANT_CHECK(
fold.left(range(int_<0>, int_<4>), int_<0>, plus) == int_<6>
);
BOOST_HANA_CONSTANT_CHECK(
unpack(range(int_<-2>, int_<2>), make<Tuple>) ==
make<Tuple>(int_<-2>, int_<-1>, int_<0>, int_<1>)
);
//! [foldable]
}{
//! [iterable]
constexpr auto r = range(int_<0>, int_<1000>);
BOOST_HANA_CONSTANT_CHECK(head(r) == int_<0>);
BOOST_HANA_CONSTANT_CHECK(last(r) == int_<999>);
BOOST_HANA_CONSTANT_CHECK(tail(r) == range(int_<1>, int_<1000>));
BOOST_HANA_CONSTANT_CHECK(!is_empty(r));
BOOST_HANA_CONSTANT_CHECK(is_empty(range(int_<3>, int_<3>)));
//! [iterable]
}{
//! [searchable]
BOOST_HANA_CONSTANT_CHECK(find(range(int_<1>, int_<25>), int_<10>) == just(int_<10>));
BOOST_HANA_CONSTANT_CHECK(find(range(int_<1>, int_<25>), int_<200>) == nothing);
//! [searchable]
}{
//! [range_c]
BOOST_HANA_CONSTANT_CHECK(head(range_c<int, 0, 5>) == int_<0>);
BOOST_HANA_CONSTANT_CHECK(last(range_c<unsigned long, 0, 5>) == ulong<4>);
BOOST_HANA_CONSTANT_CHECK(tail(range_c<int, 0, 5>) == range(int_<1>, int_<5>));
//! [range_c]
}
}