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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use {List, Queue};
#[cfg(not(feature="nightly"))]
use HetFnMut as FnMut;
pub trait Functor<F> {
type Output;
fn fmap(self, F) -> Self::Output;
}
impl<F> Functor<F> for List<()> {
type Output = List<()>;
fn fmap(self, _: F) -> List<()> {
self
}
}
impl<'a, F> Functor<F> for &'a List<()> {
type Output = List<()>;
fn fmap(self, _: F) -> List<()> {
List::new()
}
}
impl<F> Functor<F> for Queue<()> {
type Output = Queue<()>;
fn fmap(self, _: F) -> Queue<()> {
self
}
}
impl<'a, F> Functor<F> for &'a Queue<()> {
type Output = Queue<()>;
fn fmap(self, _: F) -> Queue<()> {
Queue::new()
}
}
impl<F, H, T, O, U> Functor<F> for List<(H, T)>
where T: Functor<F, Output=U>,
F: FnMut<(H,), Output=O>,
{
type Output = List<(O, U)>;
fn fmap(self, mut f: F) -> Self::Output {
let List((head, tail)) = self;
let head = f.call_mut((head,));
List((head, tail.fmap(f)))
}
}
impl<'a, F, H, T, O, U> Functor<F> for &'a mut List<(H, T)>
where &'a mut T: Functor<F, Output=U>,
F: FnMut<(&'a mut H,), Output=O>,
{
type Output = List<(O, U)>;
fn fmap(self, mut f: F) -> Self::Output {
let List((ref mut head, ref mut tail)) = *self;
let head = f.call_mut((head,));
List((head, tail.fmap(f)))
}
}
impl<'a, F, H, T, O, U> Functor<F> for &'a List<(H, T)>
where &'a T: Functor<F, Output=U>,
F: FnMut<(&'a H,), Output=O>,
{
type Output = List<(O, U)>;
fn fmap(self, mut f: F) -> Self::Output {
let List((ref head, ref tail)) = *self;
let head = f.call_mut((head,));
List((head, tail.fmap(f)))
}
}
impl<F, H, T, O, U> Functor<F> for Queue<(H, T)>
where H: for<'a> Functor<&'a mut F, Output=U>,
F: FnMut<(T,), Output=O>,
{
type Output = Queue<(U, O)>;
fn fmap(self, mut f: F) -> Self::Output {
let Queue((head, tail)) = self;
Queue((head.fmap(&mut f), f.call_mut((tail,))))
}
}
impl<'a, F, H, T, O, U> Functor<F> for &'a mut Queue<(H, T)>
where &'a mut H: for<'b> Functor<&'b mut F, Output=U>,
F: FnMut<(&'a mut T,), Output=O>,
{
type Output = Queue<(U, O)>;
fn fmap(self, mut f: F) -> Self::Output {
let Queue((ref mut head, ref mut tail)) = *self;
Queue((head.fmap(&mut f), f.call_mut((tail,))))
}
}
impl<'a, F, H, T, O, U> Functor<F> for &'a Queue<(H, T)>
where &'a H: for<'b> Functor<&'b mut F, Output=U>,
F: FnMut<(&'a T,), Output=O>,
{
type Output = Queue<(U, O)>;
fn fmap(self, mut f: F) -> Self::Output {
let Queue((ref head, ref tail)) = *self;
Queue((head.fmap(&mut f), f.call_mut((tail,))))
}
}