aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/list/operation/higher/take_while.h33
-rw-r--r--test.cc38
2 files changed, 70 insertions, 1 deletions
diff --git a/src/list/operation/higher/take_while.h b/src/list/operation/higher/take_while.h
new file mode 100644
index 0000000..01062f4
--- /dev/null
+++ b/src/list/operation/higher/take_while.h
@@ -0,0 +1,33 @@
+#ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_TAKE_WHILE_H_
+#define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_TAKE_WHILE_H_
+
+#include "type.h"
+#include "conditional/if.h"
+
+namespace tav {
+
+template <
+ template<typename> class Predicate,
+ typename Current
+>
+struct TakeWhile {
+ typedef If<
+ Predicate<Head<Current>>::type::value,
+ Cons<
+ Head<Current>,
+ typename TakeWhile<Predicate, Tail<Current>>::type
+ >,
+ void
+ > type;
+};
+
+template <
+ template<typename> class Predicate
+>
+struct TakeWhile<Predicate, void> {
+ typedef void type;
+};
+
+}
+
+#endif // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_TAKE_WHILE_H_
diff --git a/test.cc b/test.cc
index 014a974..6accc0e 100644
--- a/test.cc
+++ b/test.cc
@@ -12,6 +12,7 @@
#include "list/operation/higher/partition.h"
#include "list/operation/higher/query.h"
#include "list/operation/higher/find.h"
+#include "list/operation/higher/take_while.h"
#include "list/generator/iota.h"
#include "list/generator/make_list.h"
#include "list/generator/higher/list_tabulate.h"
@@ -683,7 +684,7 @@ static_assert(
"(count even? (list 1 3 5)) != 0"
);
-//list find
+// list find
static_assert(
std::is_same<
@@ -718,6 +719,41 @@ static_assert(
"(find even? (list 1 3 5)) != #f"
);
+// list take while
+
+static_assert(
+ std::is_same<
+ tav::List<tav::Int<2>, tav::Int<4>>::type,
+ tav::TakeWhile<
+ tav::Even,
+ tav::List<tav::Int<2>, tav::Int<4>, tav::Int<5>, tav::Int<6>>::type
+ >::type
+ >::value,
+ "(take-while even? (list 2 4 5 6)) != (list 2 4)"
+);
+
+static_assert(
+ std::is_same<
+ tav::List<tav::Int<2>, tav::Int<4>, tav::Int<6>>::type,
+ tav::TakeWhile<
+ tav::Even,
+ tav::List<tav::Int<2>, tav::Int<4>, tav::Int<6>>::type
+ >::type
+ >::value,
+ "(take-while even? (list 2 4 6)) != (list 2 4 6)"
+);
+
+static_assert(
+ std::is_same<
+ void,
+ tav::TakeWhile<
+ tav::Odd,
+ tav::List<tav::Int<2>, tav::Int<4>, tav::Int<5>, tav::Int<6>>::type
+ >::type
+ >::value,
+ "(take-while odd? (list 2 4 5 6)) != void"
+);
+
// function apply
static_assert(