aboutsummaryrefslogtreecommitdiff
path: root/example/prime/prime.cc
diff options
context:
space:
mode:
Diffstat (limited to 'example/prime/prime.cc')
-rw-r--r--example/prime/prime.cc63
1 files changed, 63 insertions, 0 deletions
diff --git a/example/prime/prime.cc b/example/prime/prime.cc
new file mode 100644
index 0000000..9d10ff4
--- /dev/null
+++ b/example/prime/prime.cc
@@ -0,0 +1,63 @@
+#include <iostream>
+
+#include "type.h"
+#include "operation/math.h"
+
+#include "list/list.h"
+#include "list/operation/higher/filter.h"
+#include "list/generator/iota.h"
+
+#include "runtime/list/for_each.h"
+
+using candidates = tav::Iota<tav::Size<1000>, tav::Int<2>, tav::Int<1>>::type;
+
+template <
+ typename Candidate,
+ typename Base
+>
+using isMultipleOf = tav::EqualValue<
+ tav::Modulo<Candidate, Base>,
+ tav::Int<0>
+>;
+
+template <
+ typename Candidates,
+ typename Base
+>
+class removeMultiplesOf {
+ private:
+ template <typename Element>
+ using predicate_wrapper = isMultipleOf<Element, Base>;
+
+ public:
+ typedef typename tav::Remove<
+ predicate_wrapper,
+ Candidates
+ >::type type;
+};
+
+template <typename Candidates>
+struct Sieve {
+ typedef tav::Cons<
+ tav::Head<Candidates>,
+ typename Sieve<
+ typename removeMultiplesOf<
+ tav::Tail<Candidates>,
+ tav::Head<Candidates>
+ >::type
+ >::type
+ > type;
+};
+
+template <>
+struct Sieve<void> {
+ typedef void type;
+};
+
+using primes = Sieve<candidates>::type;
+
+int main(int, char **) {
+ tav::runtime::for_each<primes>([](const int x) {
+ std::cout << x << std::endl;
+ });
+}