aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-01-23 19:22:01 +0100
committerAdrian Kummerlaender2015-01-23 19:22:01 +0100
commit0cbc85dd74103ec5c7b2ac5802721ccf051d8454 (patch)
treeffd4d2d9f176457438117520422fa67318e9f2ff
parentb1d2d745156a27df6f7d4e47227b8fdc70e8678b (diff)
downloadTypeAsValue-0cbc85dd74103ec5c7b2ac5802721ccf051d8454.tar
TypeAsValue-0cbc85dd74103ec5c7b2ac5802721ccf051d8454.tar.gz
TypeAsValue-0cbc85dd74103ec5c7b2ac5802721ccf051d8454.tar.bz2
TypeAsValue-0cbc85dd74103ec5c7b2ac5802721ccf051d8454.tar.lz
TypeAsValue-0cbc85dd74103ec5c7b2ac5802721ccf051d8454.tar.xz
TypeAsValue-0cbc85dd74103ec5c7b2ac5802721ccf051d8454.tar.zst
TypeAsValue-0cbc85dd74103ec5c7b2ac5802721ccf051d8454.zip
Implemented `Iota` list constructor
* recursively generates a list of `Count` elements starting at `Initial` and consecutively adding `Step` ** as most functionality of this library this function was modeled after its _Scheme_ equivalent `iota` ** it may be used as the foundation of a set of higher order list generators including e.g. `list-tabulate` * added appropriate test cases
-rw-r--r--src/list/iota.h34
-rw-r--r--test.cc39
2 files changed, 73 insertions, 0 deletions
diff --git a/src/list/iota.h b/src/list/iota.h
new file mode 100644
index 0000000..9a8d22d
--- /dev/null
+++ b/src/list/iota.h
@@ -0,0 +1,34 @@
+#ifndef TYPEASVALUE_SRC_LIST_IOTA_H_
+#define TYPEASVALUE_SRC_LIST_IOTA_H_
+
+#include "operation/math.h"
+
+namespace tav {
+
+template <
+ typename Count,
+ typename Initial,
+ typename Step
+>
+struct Iota {
+ typedef Cons<
+ Initial,
+ typename Iota<
+ Substract<Count, Size<1>>,
+ Add<Initial, Step>,
+ Step
+ >::type
+ > type;
+};
+
+template <
+ typename Initial,
+ typename Step
+>
+struct Iota<Size<1>, Initial, Step> {
+ typedef Cons<Initial, void> type;
+};
+
+}
+
+#endif // TYPEASVALUE_SRC_LIST_IOTA_H_
diff --git a/test.cc b/test.cc
index b945899..173aa19 100644
--- a/test.cc
+++ b/test.cc
@@ -5,6 +5,7 @@
#include "list/cons.h"
#include "list/list.h"
+#include "list/iota.h"
#include "list/operation/reverse.h"
#include "list/operation/contains.h"
#include "list/operation/higher/fold.h"
@@ -507,3 +508,41 @@ static_assert(
>::value,
"(length (concatenate (list (list 1 2) (list 3) (list 4 5 6)))) != 6"
);
+
+// list iota
+
+static_assert(
+ std::is_same<
+ tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type,
+ tav::Iota<
+ tav::Size<3>,
+ tav::Int<1>,
+ tav::Int<1>
+ >::type
+ >::value,
+ "(iota 3 1 1) != (list 1 2 3)"
+);
+
+static_assert(
+ std::is_same<
+ tav::List<tav::Int<0>, tav::Int<2>, tav::Int<4>, tav::Int<6>, tav::Int<8>>::type,
+ tav::Iota<
+ tav::Size<5>,
+ tav::Int<0>,
+ tav::Int<2>
+ >::type
+ >::value,
+ "(iota 5 0 2) != (list 0 2 4 6 8)"
+);
+
+static_assert(
+ std::is_same<
+ tav::List<tav::Int<5>, tav::Int<4>, tav::Int<3>, tav::Int<2>, tav::Int<1>>::type,
+ tav::Iota<
+ tav::Size<5>,
+ tav::Int<5>,
+ tav::Int<-1>
+ >::type
+ >::value,
+ "(iota 5 5 -1) != (list 5 4 3 2 1)"
+);