diff options
-rw-r--r-- | src/list/operation/higher/fold.h | 28 | ||||
-rw-r--r-- | test.cc | 6 |
2 files changed, 34 insertions, 0 deletions
diff --git a/src/list/operation/higher/fold.h b/src/list/operation/higher/fold.h new file mode 100644 index 0000000..6ad4bd7 --- /dev/null +++ b/src/list/operation/higher/fold.h @@ -0,0 +1,28 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_FOLD_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_FOLD_H_ + +namespace tav { + +template < + template<typename, typename> class Function, + typename Initial, + typename Current +> +struct Fold { + typedef typename Function< + Head<Current>, + typename Fold<Function, Initial, Tail<Current>>::type + >::type type; +}; + +template < + template<typename, typename> class Function, + typename Initial +> +struct Fold<Function, Initial, void> { + typedef Initial type; +}; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_FOLD_H_ @@ -5,6 +5,7 @@ #include "conditional/if.h" #include "list/cons.h" #include "list/list.h" +#include "list/operation/higher/fold.h" class TypeAsValueTest : public ::testing::Test { }; @@ -83,6 +84,11 @@ TEST_F(TypeAsValueTest, ListConcatenate) { EXPECT_EQ(2, ( tav::Head<tav::Tail<tav::Concatenate<tav::List<tav::Int<1>>::type, tav::List<tav::Int<2>>::type>::type>>::value )); } +TEST_F(TypeAsValueTest, ListFold) { + // (fold + 0 (list 1 2 3)) + EXPECT_EQ(6, ( tav::Fold<tav::Add, tav::Int<0>, tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type>::type::value )); +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); |