diff --git a/example/comparable/comparing.cpp b/example/comparable/comparing.cpp new file mode 100644 index 000000000..0fb96cf24 --- /dev/null +++ b/example/comparable/comparing.cpp @@ -0,0 +1,41 @@ +/* +@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 +#include +#include +#include +#include +using namespace boost::hana; + + +int main() { + //! [main] + BOOST_HANA_CONSTEXPR_LAMBDA auto grouped = group_by(comparing(length), list( + list(1, 2, 3), + list('x', 'y', 'z'), + range_c, + type_list, + range_c, + list(123.4, nullptr) + )); + + BOOST_HANA_CONSTEXPR_ASSERT(grouped == list( + list( + list(1, 2, 3), + list('x', 'y', 'z') + ), + list( + range_c + ), + list( + type_list, + range_c, + list(123.4, nullptr) + ) + )); + //! [main] +} diff --git a/include/boost/hana/comparable/comparable.hpp b/include/boost/hana/comparable/comparable.hpp index b1ec7b5cb..7be38911c 100644 --- a/include/boost/hana/comparable/comparable.hpp +++ b/include/boost/hana/comparable/comparable.hpp @@ -80,6 +80,24 @@ namespace boost { namespace hana { >::not_equal_impl(x, y); }; + //! Returns a function performing `equal` after applying a transformation + //! to both arguments. + //! @relates Comparable + //! + //! This is not a method of the `Comparable` type class, but just a + //! convenience function provided with it. Also note that + //! @code + //! comparing(f) == equal ^on^ f + //! @endcode + //! + //! ### Example + //! @snippet example/comparable/comparing.cpp main + BOOST_HANA_CONSTEXPR_LAMBDA auto comparing = [](auto f) { + return [=](auto x, auto y) { + return equal(f(x), f(y)); + }; + }; + //! Minimal complete definition : `equal` struct Comparable::equal_mcd { template diff --git a/include/boost/hana/orderable/orderable.hpp b/include/boost/hana/orderable/orderable.hpp index c93ce2dca..eb4a225e5 100644 --- a/include/boost/hana/orderable/orderable.hpp +++ b/include/boost/hana/orderable/orderable.hpp @@ -115,9 +115,11 @@ namespace boost { namespace hana { //! to both arguments. //! @relates Orderable //! - //! @note //! This is not a method of the `Orderable` type class, but just a - //! convenience function provided with it. + //! convenience function provided with it. Also note that + //! @code + //! ordering(f) == less ^on^ f + //! @endcode //! //! ### Example //! @snippet example/orderable/ordering.cpp main diff --git a/test/comparable/comparing.cpp b/test/comparable/comparing.cpp new file mode 100644 index 000000000..fd9abf32b --- /dev/null +++ b/test/comparable/comparing.cpp @@ -0,0 +1,36 @@ +/* +@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 +#include + +#include +#include +#include +using namespace boost::hana; + + +template +void test() { + constexpr auto comp = detail::minimal::comparable; + + BOOST_HANA_CONSTEXPR_LAMBDA auto comp_ = [=](auto i) { + return [=] { return comp(i); }; + }; + + BOOST_HANA_CONSTEXPR_LAMBDA auto equal_ = comparing([](auto x) { + return x(); + }); + + BOOST_HANA_CONSTEXPR_ASSERT(equal_(comp_(0), comp_(1)) == equal(comp(0), comp(1))); + BOOST_HANA_CONSTEXPR_ASSERT(equal_(comp_(0), comp_(0)) == equal(comp(0), comp(0))); + BOOST_HANA_CONSTEXPR_ASSERT(equal_(comp_(1), comp_(0)) == equal(comp(1), comp(0))); +} + +int main() { + test(); + test(); +}