-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectors.h
81 lines (60 loc) · 1.24 KB
/
collectors.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include "list.h"
template <typename S>
S evalHelper (const S stream)
{
if(stream.end()) return stream;
stream.get();
return evalHelper( stream.next());
}
template<typename S>
void eval (S stream)
{
static_assert(
std::is_same<
void, typename S::ValueType>::value,
"requires stream with void value type");
evalHelper( stream);
}
struct CollectList{};
template<typename T, typename S>
class CollectListInstance
{
const List<T> res;
List<T> streamBuild (const List<T> l , const S s)
{
if(s.end())
return l;
return streamBuild(l.push(s.get()), s.next());
}
public:
List<T> get (void) const
{
return res.reverse();
}
operator List<T> (void) const
{
return res.reverse();
}
CollectListInstance (const S s)
: res( streamBuild(List<T>(typename List<T>::SharedNode(nullptr)), s))
{
}
};
template<typename S>
List<typename S::ValueType> operator | (S left, const CollectList& right)
{
return CollectListInstance<typename S::ValueType, S>(left);
}
//SFINAE to detect if we're collecting a list or what
template <typename T,
typename = std::enable_if<
std::is_same<
typename List<T>::DataType, T
>::value
>
>
CollectList Collect (void)
{
return CollectList();
}