diff options
author | Adrian Kummerlaender | 2015-01-18 19:58:35 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2015-01-18 19:58:35 +0100 |
commit | 02c6e34f9859047efe702cf5b866702f7b02d878 (patch) | |
tree | 6181e5de2e97b06917189c6e17aeac410a82962f | |
parent | ffe8e376086cddda8bb7ca37b02a48f67cbe513d (diff) | |
download | TypeAsValue-02c6e34f9859047efe702cf5b866702f7b02d878.tar TypeAsValue-02c6e34f9859047efe702cf5b866702f7b02d878.tar.gz TypeAsValue-02c6e34f9859047efe702cf5b866702f7b02d878.tar.bz2 TypeAsValue-02c6e34f9859047efe702cf5b866702f7b02d878.tar.lz TypeAsValue-02c6e34f9859047efe702cf5b866702f7b02d878.tar.xz TypeAsValue-02c6e34f9859047efe702cf5b866702f7b02d878.tar.zst TypeAsValue-02c6e34f9859047efe702cf5b866702f7b02d878.zip |
Implemented `Map` in terms of `Fold`
* as its name implies this _function_ applies a given _function_ to each element of a _Cons_ structure
* added appropriate test case
-rw-r--r-- | src/list/operation/basic.h | 5 | ||||
-rw-r--r-- | src/list/operation/higher/misc.h | 29 | ||||
-rw-r--r-- | test.cc | 10 |
3 files changed, 42 insertions, 2 deletions
diff --git a/src/list/operation/basic.h b/src/list/operation/basic.h index ff0a4d7..364a542 100644 --- a/src/list/operation/basic.h +++ b/src/list/operation/basic.h @@ -11,14 +11,15 @@ class Length { private: template < typename, - typename Current + typename Previous > struct Count { - typedef Add<Size<1>, Current> type; + typedef Add<Size<1>, Previous> type; }; public: typedef typename Fold<Count, Size<0>, Cons>::type type; + }; template < diff --git a/src/list/operation/higher/misc.h b/src/list/operation/higher/misc.h new file mode 100644 index 0000000..708265a --- /dev/null +++ b/src/list/operation/higher/misc.h @@ -0,0 +1,29 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_MISC_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_MISC_H_ + +#include "fold.h" + +namespace tav { + +template < + template<typename> class Function, + typename List +> +class Map { + private: + template < + typename Current, + typename Previous + > + struct FunctionWrapper { + typedef Cons<Function<Current>, Previous> type; + }; + + public: + typedef typename Fold<FunctionWrapper, void, List>::type type; + +}; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_MISC_H_ @@ -3,12 +3,17 @@ #include "type.h" #include "operation/math.h" #include "conditional/if.h" + #include "list/cons.h" #include "list/list.h" #include "list/operation/higher/fold.h" +#include "list/operation/higher/misc.h" class TypeAsValueTest : public ::testing::Test { }; +template <typename Element> +using quadruple = tav::Multiply<tav::Int<4>, Element>; + TEST_F(TypeAsValueTest, BasicMath) { // (+ 1 2) EXPECT_EQ(3, ( tav::Add<tav::Int<1>, tav::Int<2>>::value )); @@ -89,6 +94,11 @@ TEST_F(TypeAsValueTest, ListFold) { EXPECT_EQ(6, ( tav::Fold<tav::Add, tav::Int<0>, tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type>::type::value )); } +TEST_F(TypeAsValueTest, ListMap) { + // (map quadruple (list 1 2 3)) + EXPECT_TRUE(( std::is_same<tav::List<tav::Int<4>, tav::Int<8>, tav::Int<12>>::type, tav::Map<quadruple, tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type>::type>::value )); +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); |