From 28dd281dd3e7c16185ecdb06b2a83f15bb074717 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 17 Jan 2015 22:45:31 +0100 Subject: Implemented `Take` function * as its name implies this function _takes_ a maximum of _Count_ elements of a list * added appropriate test case --- src/list/operation/basic.h | 35 ++++++++++++++++++++++++++++++++++- test.cc | 9 +++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/list/operation/basic.h b/src/list/operation/basic.h index cee4cbc..dbd080e 100644 --- a/src/list/operation/basic.h +++ b/src/list/operation/basic.h @@ -2,6 +2,7 @@ #define TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ #include "operation/math.h" +#include "conditional/if.h" namespace tav { @@ -26,7 +27,10 @@ struct Nth { typedef If< equal_value>::value, Head, - typename Nth>, Tail>::type + typename Nth< + Substract>, + Tail + >::type > type; }; @@ -35,6 +39,35 @@ struct Nth { typedef void type; }; +template < + typename Count, + typename Current +> +struct Take { + typedef Cons< + Head, + typename Take< + Substract>, + Tail + >::type + > type; +}; + +template +struct Take, Current> { + typedef void type; +}; + +template +struct Take { + typedef void type; +}; + +template <> +struct Take, void> { + typedef void type; +}; + } #endif // TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ diff --git a/test.cc b/test.cc index e8f7c47..f60adbd 100644 --- a/test.cc +++ b/test.cc @@ -62,6 +62,15 @@ TEST_F(TypeAsValueTest, ListNth) { EXPECT_EQ(2, ( tav::Nth, tav::List, tav::Int<2>>::type>::type::value )); } +TEST_F(TypeAsValueTest, ListTake) { + // (length (take 1 (list 1 2))) + EXPECT_EQ(1, ( tav::Length, tav::List, tav::Int<2>>::type>::type>::type::value )); + // (length (take 2 (list 1 2))) + EXPECT_EQ(2, ( tav::Length, tav::List, tav::Int<2>>::type>::type>::type::value )); + // (length (take 3 (list 1 2))) + EXPECT_EQ(2, ( tav::Length, tav::List, tav::Int<2>>::type>::type>::type::value )); +} + TEST_F(TypeAsValueTest, ListConcatenate) { // (length (concatenate (list 1) (list 2))) EXPECT_EQ(2, ( tav::Length>::type, tav::List>::type>::type>::type::value )); -- cgit v1.2.3