Boost.mpl suggestion: variation of find_if
Hi, I could not come with usable name for such thing but it could be usable anyway. So it is like find_if but applies a function to an sequence and return first non void type that function returned. So implementation would be trivial: template< typename Sequence, typename Func > struct find_type : fold< Sequence, void_, if_< is_void_< _1 >, Func< _2 >, _1 > {}; If it can be implemented by current set of algorithms please point me out. Regards, Vyacheslav
Vyacheslav Kononenko writes:
I could not come with usable name for such thing but it could be usable anyway. So it is like find_if but applies a function to an sequence and return first non void type that function returned. So implementation would be trivial:
template< typename Sequence, typename Func > struct find_type : fold< Sequence, void_, if_< is_void_< _1 >, Func< _2 >, _1 > {};
Or, which is likely to be more efficient,
template< typename Seq, typename F > struct find_type
{
typedef typename find_if<
transform_view
If it can be implemented by current set of algorithms please point me out.
Doesn't your sketch above employ an algorithm from the current set ('fold')? In any case, at the moment this feels too specialized to me to be considered for inclusion in the library. At the very least, it needs a name ;). -- Aleksey Gurtovoy MetaCommunications Engineering
--- Aleksey Gurtovoy
Vyacheslav Kononenko writes:
I could not come with usable name for such thing but it could be usable anyway. So it is like find_if but applies a function to an sequence and return first non void type that function returned. So implementation would be trivial:
template< typename Sequence, typename Func > struct find_type : fold< Sequence, void_, if_< is_void_< _1 >, Func< _2 >, _1 > {};
Or, which is likely to be more efficient,
template< typename Seq, typename F > struct find_type { typedef typename find_if< transform_view
, is_not_void_<_1> >::type iter_; typedef typename eval_if< is_same< typename iter_::base, typename end<Seq>::type > , void_ , deref
>::type type; };
Correct my if I am wrong but your will invoke F on every element when mine will not.
If it can be implemented by current set of algorithms please point me out.
Doesn't your sketch above employ an algorithm from the current set ('fold')? In any case, at the moment this feels too specialized to me to be considered for inclusion in the library. At the very least, it needs a name ;).
There is another topic about mpl for_each and I think my algo would be usable there. But for the name it is an author work to make names isn't it?
-- Aleksey Gurtovoy MetaCommunications Engineering _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org
Vyacheslav Kononenko
--- Aleksey Gurtovoy
wrote: Vyacheslav Kononenko writes:
I could not come with usable name for such thing but it could be usable anyway. So it is like find_if but applies a function to an sequence and return first non void type that function returned. So implementation would be trivial:
template< typename Sequence, typename Func > struct find_type : fold< Sequence, void_, if_< is_void_< _1 >, Func< _2 >, _1 > {};
Or, which is likely to be more efficient,
template< typename Seq, typename F > struct find_type { typedef typename find_if< transform_view
, is_not_void_<_1> >::type iter_; typedef typename eval_if< is_same< typename iter_::base, typename end<Seq>::type > , void_ , deref
>::type type; }; Correct my if I am wrong but your will invoke F on every element when mine will not.
transform_view is lazy, so F will only be invoked until it returns a non-void_ result.
If it can be implemented by current set of algorithms please point me out.
Doesn't your sketch above employ an algorithm from the current set ('fold')? In any case, at the moment this feels too specialized to me to be considered for inclusion in the library. At the very least, it needs a name ;).
There is another topic about mpl for_each and I think my algo would be usable there. But for the name it is an author work to make names isn't it?
You appear to be the author of this algorithm ;-) -- Dave Abrahams Boost Consulting www.boost-consulting.com
BTW some issues in documentation: is_void_ is not referenced in TOC http://www.boost.org/libs/mpl/doc/refmanual/refmanual_toc.html is_void_ referenced as 'is_void' http://www.boost.org/libs/mpl/doc/refmanual/void.html is_not_void_ is not documented Regards, Vyacheslav
participants (3)
-
Aleksey Gurtovoy
-
David Abrahams
-
Vyacheslav Kononenko