diff options
author | Adrian Kummerlaender | 2015-01-20 17:06:20 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2015-01-20 17:06:20 +0100 |
commit | 5f2fdb01bdb9892cb8cf5bf1b1787db36587e1d6 (patch) | |
tree | 4ddfb07f8ee8feba27ed82996c7f81c0aad8adca | |
parent | b5fc92b5377e3effe4410348f4199316b88fba7f (diff) | |
download | TypeAsValue-5f2fdb01bdb9892cb8cf5bf1b1787db36587e1d6.tar TypeAsValue-5f2fdb01bdb9892cb8cf5bf1b1787db36587e1d6.tar.gz TypeAsValue-5f2fdb01bdb9892cb8cf5bf1b1787db36587e1d6.tar.bz2 TypeAsValue-5f2fdb01bdb9892cb8cf5bf1b1787db36587e1d6.tar.lz TypeAsValue-5f2fdb01bdb9892cb8cf5bf1b1787db36587e1d6.tar.xz TypeAsValue-5f2fdb01bdb9892cb8cf5bf1b1787db36587e1d6.tar.zst TypeAsValue-5f2fdb01bdb9892cb8cf5bf1b1787db36587e1d6.zip |
Implemented higher order list queries `All` and `Any`
* in terms of `Fold` and `Map`, not the most efficient but reasonably concise
* added appropriate test cases
-rw-r--r-- | src/list/operation/higher/query.h | 30 | ||||
-rw-r--r-- | test.cc | 49 |
2 files changed, 78 insertions, 1 deletions
diff --git a/src/list/operation/higher/query.h b/src/list/operation/higher/query.h new file mode 100644 index 0000000..56bb203 --- /dev/null +++ b/src/list/operation/higher/query.h @@ -0,0 +1,30 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_QUERY_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_QUERY_H_ + +#include "operation/logic.h" + +namespace tav { + +template < + template<typename> class Function, + typename List +> +using Any = Fold< + Or, + Boolean<false>, + typename Map<Function, List>::type +>; + +template < + template<typename> class Function, + typename List +> +using All = Fold< + And, + Boolean<true>, + typename Map<Function, List>::type +>; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_QUERY_H_ @@ -7,6 +7,7 @@ #include "list/list.h" #include "list/operation/higher/fold.h" #include "list/operation/higher/misc.h" +#include "list/operation/higher/query.h" int main(int, char **) { } @@ -382,7 +383,7 @@ static_assert( tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type >::type >::value, - "(filter odd (list 1 2 3)) != (list 1 3)" + "(filter odd? (list 1 2 3)) != (list 1 3)" ); // list reverse @@ -396,3 +397,49 @@ static_assert( >::value, "(reverse (list 1 2 3)) != (list 3 2 1)" ); + +// list query + +static_assert( + std::is_same< + tav::Boolean<true>, + tav::Any< + tav::Odd, + tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type + >::type + >::value, + "(any odd? (list 1 2 3)) != #t" +); + +static_assert( + std::is_same< + tav::Boolean<false>, + tav::Any< + tav::Odd, + tav::List<tav::Int<2>, tav::Int<4>, tav::Int<6>>::type + >::type + >::value, + "(any odd? (list 2 4 6)) != #f" +); + +static_assert( + std::is_same< + tav::Boolean<true>, + tav::All< + tav::Even, + tav::List<tav::Int<2>, tav::Int<4>, tav::Int<6>>::type + >::type + >::value, + "(all even? (list 2 4 6)) != #t" +); + +static_assert( + std::is_same< + tav::Boolean<false>, + tav::All< + tav::Odd, + tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type + >::type + >::value, + "(all odd? (list 1 2 3)) != #f" +); |