aboutsummaryrefslogtreecommitdiff
path: root/example/prime/prime.cc
blob: 93b7db581925ea0746ce68511f98c3bad626af4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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
>
using removeMultiplesOf = tav::Remove<
	tav::Apply<isMultipleOf, tav::_0, Base>::template single_type,
	Candidates
>;

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;
	});
}