/
challenge_08_09_04.rs
173 lines (138 loc) · 4.45 KB
/
challenge_08_09_04.rs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::concepts::bifunctor::Bifunctor;
use fn_traits::FnMut;
use std::marker::PhantomData;
pub struct K2<C, A, B>(pub C, pub PhantomData<(A, B)>);
pub struct K2BiFunctor;
impl<C, A, B> Bifunctor<K2BiFunctor, A, B> for K2<C, A, B> {
type BiMap<F, G> = K2<C, F::Output, G::Output>
where
F: FnMut<(A,)>,
G: FnMut<(B,)>;
fn bimap<F, G>(self, _f: F, _g: G) -> Self::BiMap<F, G>
where
F: FnMut<(A,)>,
G: FnMut<(B,)>,
{
K2(self.0, PhantomData)
}
}
pub struct Fst<A, B>(pub A, pub PhantomData<B>);
pub struct FstBiFunctor;
impl<A, B> Bifunctor<FstBiFunctor, A, B> for Fst<A, B> {
type BiMap<F, G> = Fst<F::Output, G::Output>
where
F: FnMut<(A,)>,
G: FnMut<(B,)>;
fn bimap<F, G>(self, mut f: F, _g: G) -> Self::BiMap<F, G>
where
F: FnMut<(A,)>,
G: FnMut<(B,)>,
{
Fst(f.call_mut((self.0,)), PhantomData)
}
}
pub struct Snd<A, B>(pub B, pub PhantomData<A>);
pub struct SndBiFunctor;
impl<A, B> Bifunctor<SndBiFunctor, A, B> for Snd<A, B> {
type BiMap<F, G> = Snd<F::Output, G::Output>
where
F: FnMut<(A,)>,
G: FnMut<(B,)>;
fn bimap<F, G>(self, _f: F, mut g: G) -> Self::BiMap<F, G>
where
F: FnMut<(A,)>,
G: FnMut<(B,)>,
{
Snd(g.call_mut((self.0,)), PhantomData)
}
}
#[cfg(test)]
mod tests {
use super::{Fst, Snd, K2};
use crate::concepts::bifunctor::Bifunctor;
use fn_traits::{fns, FnMut};
use std::convert;
use std::marker::PhantomData;
type MyK2<T, U> = K2<u32, T, U>;
#[test]
fn test_k2_preservation_of_composition() {
let f_1 = |x: u8| u16::from(x + 2);
let f_2 = |x: u16| u32::from(x * 3);
let g_1 = |x: u64| u128::from(x + 5);
let g_2 = |x: u128| x * 7;
let compose_then_map = |x| MyK2::bimap(x, fns::compose(f_1, f_2), fns::compose(g_1, g_2));
let map_then_compose = |x| {
fns::compose(|x| MyK2::bimap(x, f_1, g_1), |x| MyK2::bimap(x, f_2, g_2)).call_mut((x,))
};
assert!(matches!(
compose_then_map(K2(7, PhantomData)),
K2(7, PhantomData)
));
assert!(matches!(
map_then_compose(K2(7, PhantomData)),
K2(7, PhantomData)
));
}
#[test]
fn test_k2_preservation_of_identity() {
let id = |x| MyK2::bimap(x, convert::identity, convert::identity);
assert!(matches!(
id(K2::<_, u32, u64>(7, PhantomData)),
K2(7, PhantomData),
));
}
#[test]
fn test_fst_preservation_of_composition() {
let f_1 = |x: u8| u16::from(x + 2);
let f_2 = |x: u16| u32::from(x * 3);
let g_1 = |x: u64| u128::from(x + 5);
let g_2 = |x: u128| x * 7;
let compose_then_map = |x| Fst::bimap(x, fns::compose(f_1, f_2), fns::compose(g_1, g_2));
let map_then_compose = |x| {
fns::compose(|x| Fst::bimap(x, f_1, g_1), |x| Fst::bimap(x, f_2, g_2)).call_mut((x,))
};
assert!(matches!(
compose_then_map(Fst(7, PhantomData)),
Fst(27, PhantomData),
));
assert!(matches!(
map_then_compose(Fst(7, PhantomData)),
Fst(27, PhantomData),
));
}
#[test]
fn test_fst_preservation_of_identity() {
let id = |x| Fst::bimap(x, convert::identity, convert::identity);
assert!(matches!(
id(Fst::<_, u32>(7, PhantomData)),
Fst(7, PhantomData),
));
}
#[test]
fn test_snd_preservation_of_composition() {
let f_1 = |x: u8| u16::from(x + 2);
let f_2 = |x: u16| u32::from(x * 3);
let g_1 = |x: u64| u128::from(x + 5);
let g_2 = |x: u128| x * 7;
let compose_then_map = |x| Snd::bimap(x, fns::compose(f_1, f_2), fns::compose(g_1, g_2));
let map_then_compose = |x| {
fns::compose(|x| Snd::bimap(x, f_1, g_1), |x| Snd::bimap(x, f_2, g_2)).call_mut((x,))
};
assert!(matches!(
compose_then_map(Snd(7, PhantomData)),
Snd(84, PhantomData),
));
assert!(matches!(
map_then_compose(Snd(7, PhantomData)),
Snd(84, PhantomData),
));
}
#[test]
fn test_snd_preservation_of_identity() {
let id = |x| Snd::bimap(x, convert::identity, convert::identity);
assert!(matches!(
id(Snd::<u32, _>(7, PhantomData)),
Snd(7, PhantomData),
));
}
}