From 3cac56267edfbfd26e203ad674b9b8e74e44b14e Mon Sep 17 00:00:00 2001
From: Adrian Kummerlaender
Date: Mon, 19 Jan 2015 17:09:39 +0100
Subject: Implemented `Filter` in terms of `Fold` * as its name implies this
 _function_ only returns elements which evaluate to _true_ when passed to a
 given _function_ ** this marks the moment where _TypeAsValue_ supports
 something _ConstList_ does not, i.e. primary goal is achieved *** namely
 returning different types depending on the actual _values_ of a _Cons_
 structure * added appropriate test case

---
 src/list/operation/basic.h       |  4 ++--
 src/list/operation/higher/misc.h | 32 ++++++++++++++++++++++++++++----
 test.cc                          |  8 ++++++++
 3 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/src/list/operation/basic.h b/src/list/operation/basic.h
index 364a542..d80b347 100644
--- a/src/list/operation/basic.h
+++ b/src/list/operation/basic.h
@@ -13,12 +13,12 @@ class Length {
 			typename,
 			typename Previous
 		>
-		struct Count {
+		struct count {
 			typedef Add<Size<1>, Previous> type;
 		};
 
 	public:
-		typedef typename Fold<Count, Size<0>, Cons>::type type;
+		typedef typename Fold<count, Size<0>, Cons>::type type;
 
 };
 
diff --git a/src/list/operation/higher/misc.h b/src/list/operation/higher/misc.h
index 280c0d5..95a5f50 100644
--- a/src/list/operation/higher/misc.h
+++ b/src/list/operation/higher/misc.h
@@ -3,6 +3,7 @@
 
 #include "fold.h"
 #include "list/operation/concatenate.h"
+#include "conditional/if.h"
 
 namespace tav {
 
@@ -16,12 +17,35 @@ class Map {
 			typename Current,
 			typename Previous
 		>
-		struct FunctionWrapper {
+		struct function_wrapper {
 			typedef Cons<Function<Current>, Previous> type;
 		};
 
 	public:
-		typedef typename Fold<FunctionWrapper, void, List>::type type;
+		typedef typename Fold<function_wrapper, void, List>::type type;
+
+};
+
+template <
+	template<typename> class Function,
+	typename                 List
+>
+class Filter {
+	private:
+		template <
+			typename Current,
+			typename Previous
+		>
+		struct function_wrapper {
+			typedef If<
+				Function<Current>::type::value,
+				Cons<Current, Previous>,
+				Previous
+			> type;
+		};
+
+	public:
+		typedef typename Fold<function_wrapper, void, List>::type type;
 
 };
 
@@ -32,7 +56,7 @@ class Reverse {
 			typename Current,
 			typename Previous
 		>
-		struct ReversedConcatenate {
+		struct reversed_concatenate {
 			typedef typename Concatenate<
 				Previous,
 				Cons<Current, void>
@@ -40,7 +64,7 @@ class Reverse {
 		};
 
 	public:
-		typedef typename Fold<ReversedConcatenate, void, List>::type type;
+		typedef typename Fold<reversed_concatenate, void, List>::type type;
 
 };
 
diff --git a/test.cc b/test.cc
index 655aed3..35126fb 100644
--- a/test.cc
+++ b/test.cc
@@ -14,6 +14,9 @@ class TypeAsValueTest : public ::testing::Test { };
 template <typename Element>
 using quadruple = tav::Multiply<tav::Int<4>, Element>;
 
+template <typename Element>
+using odd = tav::Boolean<(Element::value % 2 == 1)>;
+
 TEST_F(TypeAsValueTest, BasicMath) {
 	// (+ 1 2)
 	EXPECT_EQ(3,  ( tav::Add<tav::Int<1>, tav::Int<2>>::value ));
@@ -99,6 +102,11 @@ TEST_F(TypeAsValueTest, ListMap) {
 	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 ));
 }
 
+TEST_F(TypeAsValueTest, ListFilter) {
+	// (filter odd (list 1 2 3))
+	EXPECT_TRUE(( std::is_same<tav::List<tav::Int<1>, tav::Int<3>>::type, tav::Filter<odd, tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type>::type>::value ));
+}
+
 TEST_F(TypeAsValueTest, ListReverse) {
 	// (reverse (list 1 2 3))
 	EXPECT_TRUE(( std::is_same<tav::List<tav::Int<3>, tav::Int<2>, tav::Int<1>>::type, tav::Reverse<tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type>::type>::value ));
-- 
cgit v1.2.3