Skip to content

Commit 58da968

Browse files
committed
[view] Implement 'ap' using cartesian_product_view
1 parent 339366b commit 58da968

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

include/boost/hana/view.hpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ Distributed under the Boost Software License, Version 1.0.
2323
#include <boost/hana/detail/unpack_flatten.hpp>
2424
#include <boost/hana/equal.hpp>
2525
#include <boost/hana/fold_left.hpp>
26+
#include <boost/hana/functional/apply.hpp>
2627
#include <boost/hana/functional/compose.hpp>
2728
#include <boost/hana/functional/on.hpp>
29+
#include <boost/hana/fuse.hpp>
2830
#include <boost/hana/fwd/ap.hpp>
2931
#include <boost/hana/fwd/concat.hpp>
3032
#include <boost/hana/fwd/core/make.hpp>
@@ -625,10 +627,14 @@ BOOST_HANA_NAMESPACE_BEGIN
625627
template <>
626628
struct ap_impl<hana::view_tag> {
627629
template <typename F, typename X>
628-
static constexpr auto apply(F&& f, X&& x) {
629-
// TODO: Implement cleverly; we most likely need a cartesian_product
630-
// view or something like that.
631-
return hana::ap(hana::to_tuple(f), hana::to_tuple(x));
630+
static constexpr auto apply(F f, X x) {
631+
// Note: `f` and `x` must both be view, so we're not returning
632+
// dangling references below.
633+
auto fs = detail::single_view(f);
634+
auto xs = detail::single_view(x);
635+
auto joined = detail::joined(fs, xs);
636+
auto product = detail::cartesian_product_view(joined);
637+
return detail::transformed(product, hana::fuse(hana::apply));
632638
}
633639
};
634640

@@ -669,7 +675,6 @@ BOOST_HANA_NAMESPACE_BEGIN
669675
//////////////////////////////////////////////////////////////////////////
670676
// Comparable
671677
//////////////////////////////////////////////////////////////////////////
672-
673678
template <typename Xs, typename Ys>
674679
inline constexpr auto equal_helper(Xs const& xs, Ys const& ys) {
675680
constexpr std::size_t xs_size = decltype(hana::length(xs))::value;

test/_include/auto/ap.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ TestCase test_ap{[]{
7171
MAKE_TUPLE(f(ct_eq<0>{}), f(ct_eq<1>{}), f(ct_eq<2>{}),
7272
g(ct_eq<0>{}), g(ct_eq<1>{}), g(ct_eq<2>{}))
7373
));
74+
75+
BOOST_HANA_CONSTANT_CHECK(hana::equal(
76+
hana::ap(MAKE_TUPLE(f, g),
77+
MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}),
78+
MAKE_TUPLE(ct_eq<10>{}, ct_eq<11>{})),
79+
MAKE_TUPLE(
80+
f(ct_eq<0>{}, ct_eq<10>{}),
81+
f(ct_eq<0>{}, ct_eq<11>{}),
82+
f(ct_eq<1>{}, ct_eq<10>{}),
83+
f(ct_eq<1>{}, ct_eq<11>{}),
84+
85+
g(ct_eq<0>{}, ct_eq<10>{}),
86+
g(ct_eq<0>{}, ct_eq<11>{}),
87+
g(ct_eq<1>{}, ct_eq<10>{}),
88+
g(ct_eq<1>{}, ct_eq<11>{})
89+
)
90+
));
7491
}};
7592

7693
#endif // !BOOST_HANA_TEST_AUTO_AP_HPP

test/view/auto/ap.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright Louis Dionne 2013-2016
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4+
5+
#include "_specs.hpp"
6+
#include <auto/ap.hpp>
7+
8+
int main() { }

0 commit comments

Comments
 (0)