diff options
-rw-r--r-- | src/list/operation/higher/find.h | 30 | ||||
-rw-r--r-- | src/list/operation/higher/query.h | 16 | ||||
-rw-r--r-- | test.cc | 36 |
3 files changed, 74 insertions, 8 deletions
diff --git a/src/list/operation/higher/find.h b/src/list/operation/higher/find.h new file mode 100644 index 0000000..47b88ad --- /dev/null +++ b/src/list/operation/higher/find.h @@ -0,0 +1,30 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_FIND_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_FIND_H_ + +#include "type.h" +#include "conditional/if.h" + +namespace tav { + +template < + template<typename> class Predicate, + typename Current +> +struct Find { + typedef If< + Predicate<Head<Current>>::type::value, + Head<Current>, + typename Find<Predicate, Tail<Current>>::type + > type; +}; + +template < + template<typename> class Predicate +> +struct Find<Predicate, void> { + typedef Boolean<false> type; +}; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_FIND_H_ diff --git a/src/list/operation/higher/query.h b/src/list/operation/higher/query.h index 198ffa5..d2b8e76 100644 --- a/src/list/operation/higher/query.h +++ b/src/list/operation/higher/query.h @@ -9,41 +9,41 @@ namespace tav { template < - template<typename> class Function, + template<typename> class Predicate, typename List > using Any = Fold< Or, Boolean<false>, - typename Map<Function, List>::type + typename Map<Predicate, List>::type >; template < - template<typename> class Function, + template<typename> class Predicate, typename List > using All = Fold< And, Boolean<true>, - typename Map<Function, List>::type + typename Map<Predicate, List>::type >; template < - template<typename> class Function, + template<typename> class Predicate, typename List > using None = Not< - typename Any<Function, List>::type + typename Any<Predicate, List>::type >; template < - template<typename> class Function, + template<typename> class Predicate, typename List > using Count = Fold< Add, tav::Size<0>, - typename Map<Function, List>::type + typename Map<Predicate, List>::type >; } @@ -11,6 +11,7 @@ #include "list/operation/higher/filter.h" #include "list/operation/higher/partition.h" #include "list/operation/higher/query.h" +#include "list/operation/higher/find.h" #include "list/generator/iota.h" #include "list/generator/make_list.h" #include "list/generator/higher/list_tabulate.h" @@ -682,6 +683,41 @@ static_assert( "(count even? (list 1 3 5)) != 0" ); +//list find + +static_assert( + std::is_same< + tav::Int<4>, + tav::Find< + tav::Even, + tav::List<tav::Int<1>, tav::Int<3>, tav::Int<4>, tav::Int<6>>::type + >::type + >::value, + "(find even? (list 1 3 4 6)) != 4" +); + +static_assert( + std::is_same< + tav::Int<6>, + tav::Find< + tav::Even, + tav::List<tav::Int<1>, tav::Int<3>, tav::Int<5>, tav::Int<6>>::type + >::type + >::value, + "(find even? (list 1 3 5 6)) != 6" +); + +static_assert( + std::is_same< + tav::Boolean<false>, + tav::Find< + tav::Even, + tav::List<tav::Int<1>, tav::Int<3>, tav::Int<5>>::type + >::type + >::value, + "(find even? (list 1 3 5)) != #f" +); + // function apply static_assert( |