aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/list/operation/basic.h19
-rw-r--r--test.cc9
2 files changed, 28 insertions, 0 deletions
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 <typename Cons>
@@ -16,6 +18,23 @@ struct Length<void> {
typedef Size<0> type;
};
+template <
+ typename Index,
+ typename Cons
+>
+struct Nth {
+ typedef If<
+ equal_value<Index, Size<0>>::value,
+ Head<Cons>,
+ typename Nth<Substract<Index, Size<1>>, Tail<Cons>>::type
+ > type;
+};
+
+template <typename Index>
+struct Nth<Index, void> {
+ 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::Tail<tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type>>::value ));
}
+TEST_F(TypeAsValueTest, ListNth) {
+ // (nth 0 (list 1))
+ EXPECT_EQ(1, ( tav::Nth<tav::Size<0>, tav::List<tav::Int<1>>::type>::type::value ));
+ // (nth 0 (list 1 2))
+ EXPECT_EQ(1, ( tav::Nth<tav::Size<0>, tav::List<tav::Int<1>, tav::Int<2>>::type>::type::value ));
+ // (nth 1 (list 1 2))
+ EXPECT_EQ(2, ( tav::Nth<tav::Size<1>, tav::List<tav::Int<1>, tav::Int<2>>::type>::type::value ));
+}
+
TEST_F(TypeAsValueTest, ListConcatenate) {
// (length (concatenate (list 1) (list 2)))
EXPECT_EQ(2, ( tav::Length<tav::Concatenate<tav::List<tav::Int<1>>::type, tav::List<tav::Int<2>>::type>::type>::type::value ));