From 6291f3ce10aa8ebffa895f21c4ccb91b7349c66a Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 30 Jan 2015 19:31:30 +0100 Subject: Implemented `Remove` and `Partition` higher order list functions * `Remove` is a basic `Filter` alias that negates its predicate * `Partition` builds on both `Remove` and `Filter` to return both the elements satisfying a predicate and those which don't --- src/list/operation/higher/filter.h | 22 ++++++++++++++++++---- src/list/operation/higher/map.h | 1 - src/list/operation/higher/partition.h | 21 +++++++++++++++++++++ test.cc | 30 ++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/list/operation/higher/partition.h diff --git a/src/list/operation/higher/filter.h b/src/list/operation/higher/filter.h index 8788aed..d91735e 100644 --- a/src/list/operation/higher/filter.h +++ b/src/list/operation/higher/filter.h @@ -7,7 +7,7 @@ namespace tav { template < - template class Function, + template class Predicate, typename List > class Filter { @@ -16,16 +16,30 @@ class Filter { typename Current, typename Previous > - struct function_wrapper { + struct predicate_wrapper { typedef If< - Function::type::value, + Predicate::type::value, Cons, Previous > type; }; public: - typedef typename Fold::type type; + typedef typename Fold::type type; + +}; + +template < + template class Predicate, + typename List +> +class Remove { + private: + template + using predicate_negator = Not>; + + public: + typedef typename Filter::type type; }; diff --git a/src/list/operation/higher/map.h b/src/list/operation/higher/map.h index 1c577ca..a451b2a 100644 --- a/src/list/operation/higher/map.h +++ b/src/list/operation/higher/map.h @@ -2,7 +2,6 @@ #define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_MAP_H_ #include "fold.h" -#include "conditional/if.h" namespace tav { diff --git a/src/list/operation/higher/partition.h b/src/list/operation/higher/partition.h new file mode 100644 index 0000000..e42b971 --- /dev/null +++ b/src/list/operation/higher/partition.h @@ -0,0 +1,21 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_PARTITION_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_PARTITION_H_ + +#include "filter.h" + +namespace tav { + +template < + template class Predicate, + typename List +> +struct Partition { + typedef Cons< + typename Filter::type, + typename Remove::type + > type; +}; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_PARTITION_H_ diff --git a/test.cc b/test.cc index e7817f0..9168038 100644 --- a/test.cc +++ b/test.cc @@ -9,6 +9,7 @@ #include "list/operation/contains.h" #include "list/operation/higher/map.h" #include "list/operation/higher/filter.h" +#include "list/operation/higher/partition.h" #include "list/operation/higher/query.h" #include "list/generator/iota.h" #include "list/generator/make_list.h" @@ -427,6 +428,35 @@ static_assert( "(filter odd? (list 1 2 3)) != (list 1 3)" ); +// list remove + +static_assert( + std::is_same< + tav::List, tav::Int<3>>::type, + tav::Remove< + tav::Even, + tav::List, tav::Int<2>, tav::Int<3>>::type + >::type + >::value, + "(remove even? (list 1 2 3)) != (list 1 3)" +); + +// list partition + +static_assert( + std::is_same< + tav::Cons< + tav::List, tav::Int<3>>::type, + tav::List>::type + >, + tav::Partition< + tav::Odd, + tav::List, tav::Int<2>, tav::Int<3>>::type + >::type + >::value, + "(partition odd? (list 1 2 3)) != [(list 1 3) (list 2)]" +); + // list reverse static_assert( -- cgit v1.2.3