[fusion] how to get the tuple of min values of 2 tuples whatever their length?

I have two tuples:
typedef boost::tuple

2013/7/17 Frédéric Bron
I have two tuples: typedef boost::tuple
Key; Key k1, k2; I would like to get the tuple of the min values for each corresponding value between k1 and k2: Key min=Key(std::min(k1.get<0>(), k2.get<0>()), std::min(k1.get<1>(), k2.get<1>()), std::min(k1.get<2>(), k2.get<2>()));
but I do not know the length of the tuple so I am looking for a generic algorithm... Is this possible with boost::fusion or boost::mpl?
Probably like this: struct min_functor { template<class T> T operator()(T const& a, T const& b) const { return std::min(a, b); } }; auto ret = fusion::transform(k1, k2, min_functor()); fusion::copy(ret, min); HTH

On 7/17/2013 7:17 AM, Frédéric Bron wrote:
I have two tuples: typedef boost::tuple
Key; Key k1, k2; I would like to get the tuple of the min values for each corresponding value between k1 and k2: Key min=Key(std::min(k1.get<0>(), k2.get<0>()), std::min(k1.get<1>(), k2.get<1>()), std::min(k1.get<2>(), k2.get<2>()));
but I do not know the length of the tuple so I am looking for a generic algorithm... Is this possible with boost::fusion or boost::mpl?
I have tried with boost::fusion::accumulate but I need to now in the functor the index of the value that is currently accessed to compare to the right one in k1.
Have you tried the binary version of fusion::transform described at: http://www.boost.org/doc/libs/1_54_0/libs/fusion/doc/html/fusion/algorithm/t... You'll need a polymorphic function object wrapping std::min(unless fusion has an adapter), something like(untested): struct PolyMin { template<typename T> struct result { typedef typename boost::remove_const< typename boost::remove_reference<T>::type>::type type; }; template<typename T> T operator()(const T& lhs, const T& rhs) const { return (std::min)(lhs, rhs); } }; Jeff

On 07/17/2013 01:17 PM, Frédéric Bron wrote:
I have tried with boost::fusion::accumulate but I need to now in the functor the index of the value that is currently accessed to compare to the right one in k1.
Others have already pointed you to fusion::transform. There is a complete example of this in the documentation, albeit it does pairwise subtraction instead of min: http://www.boost.org/fusion/doc/html/fusion/functional/adapters/fused_functi...

Thank you all!
It works now with the code below.
What was the most difficult was to find the headers to include.
Frédéric
#include <iostream>
#include <string>
#include

On 7/18/2013 4:12 AM, Frédéric Bron wrote:
Thank you all! It works now with the code below. What was the most difficult was to find the headers to include. Frédéric
#include <iostream> #include <string>
#include
#include #include #include #include #include #include typedef boost::fusion::vector
Key; template
typename boost::fusion::result_of::transform< boost::fusion::zip_view<SeqOfSeqs> const, boost::fusion::fused_function_object >::type n_ary_transform(const SeqOfSeqs &s, const Func &f) { return boost::fusion::transform( boost::fusion::zip_view<SeqOfSeqs>(s), boost::fusion::fused_function_object (f)); } struct min { template <typename Sig> struct result;
template
struct result< Self(T, T) > { typedef typename boost::remove_reference<T>::type type; }; template<typename T> T operator()(const T &lhs, const T &rhs) const { return std::min(lhs, rhs); } };
struct print { template <class T> void operator()(const T &rhs) const { std::cout<
int main() { Key a(1., 0, "zz", 'a'); Key b(2., -1, "aa", 'a'); Key c; c=n_ary_transform(boost::fusion::vector_tie(a, b), min()); boost::fusion::for_each(c, print()); return 0; }
I'm curious, wouldn't fusion's binary transform algorithm work for you? c = boost::fusion::transform(a, b, min()); Jeff

I'm curious, wouldn't fusion's binary transform algorithm work for you? c = boost::fusion::transform(a, b, min());
Much simpler indeed! Thanks. Below is the full code.
The part with struct result is a bit strange to me!
I do not understand what Self(T, T) means.
Frédéric
#include <iostream>
#include <string>
#include

The part with struct result is a bit strange to me! I do not understand what Self(T, T) means.
See [1] and [2]. Regards, Nate [1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1454.html [2] http://www.boost.org/doc/libs/1_54_0/libs/utility/utility.htm#result_of
participants (5)
-
Bjorn Reese
-
Frédéric Bron
-
Jeff Flinn
-
Nathan Ridge
-
TONGARI J