From ec52d63d3caca690e822e1a07ba9c2f47710b4d5 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 17 Jan 2015 20:34:11 +0100 Subject: Implemented `Nth` function * as its name implies this function returns the _nth_ value of a given _Cons_ structure * added appropriate test case --- src/list/operation/basic.h | 19 +++++++++++++++++++ test.cc | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/src/list/operation/basic.h b/src/list/operation/basic.h index 1e05a50..cee4cbc 100644 --- a/src/list/operation/basic.h +++ b/src/list/operation/basic.h @@ -1,6 +1,8 @@ #ifndef TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ #define TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ +#include "operation/math.h" + namespace tav { template @@ -16,6 +18,23 @@ struct Length { typedef Size<0> type; }; +template < + typename Index, + typename Cons +> +struct Nth { + typedef If< + equal_value>::value, + Head, + typename Nth>, Tail>::type + > type; +}; + +template +struct Nth { + typedef void type; +}; + } #endif // TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ diff --git a/test.cc b/test.cc index 6ce3691..e8f7c47 100644 --- a/test.cc +++ b/test.cc @@ -53,6 +53,15 @@ TEST_F(TypeAsValueTest, List) { EXPECT_EQ(2, ( tav::Head, tav::Int<2>, tav::Int<3>>::type>>::value )); } +TEST_F(TypeAsValueTest, ListNth) { + // (nth 0 (list 1)) + EXPECT_EQ(1, ( tav::Nth, tav::List>::type>::type::value )); + // (nth 0 (list 1 2)) + EXPECT_EQ(1, ( tav::Nth, tav::List, tav::Int<2>>::type>::type::value )); + // (nth 1 (list 1 2)) + EXPECT_EQ(2, ( tav::Nth, tav::List, tav::Int<2>>::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