From 4f2aa4218ec63107b4624e576ff391c2019690a1 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 25 Jan 2015 17:15:59 +0100 Subject: Implemented `Count` in terms of `Fold` and `Map` * as its name implies this function counts the amount of elements satisfying a given _function_ * added appropriate test case --- src/list/operation/higher/query.h | 11 +++++++++++ test.cc | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/list/operation/higher/query.h b/src/list/operation/higher/query.h index 29f65dc..c6eabc7 100644 --- a/src/list/operation/higher/query.h +++ b/src/list/operation/higher/query.h @@ -3,6 +3,7 @@ #include "fold.h" #include "misc.h" +#include "operation/math.h" #include "operation/logic.h" namespace tav { @@ -35,6 +36,16 @@ using None = Not< typename Any::type >; +template < + template class Function, + typename List +> +using Count = Fold< + Add, + tav::Size<0>, + typename Map::type +>; + } #endif // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_QUERY_H_ diff --git a/test.cc b/test.cc index 6476bb8..62c6f85 100644 --- a/test.cc +++ b/test.cc @@ -607,3 +607,27 @@ static_assert( >::value, "(list-tabulate 4 square) != (list 0 1 4 9)" ); + +// list count + +static_assert( + std::is_same< + tav::Size<2>, + tav::Count< + tav::Odd, + tav::List, tav::Int<2>, tav::Int<3>>::type + >::type + >::value, + "(count odd? (list 1 2 3)) != 2" +); + +static_assert( + std::is_same< + tav::Size<0>, + tav::Count< + tav::Even, + tav::List, tav::Int<3>, tav::Int<5>>::type + >::type + >::value, + "(count even? (list 1 3 5)) != 0" +); -- cgit v1.2.3