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
use std::collections::{LinkedList, VecDeque};
use crate::{Ap, Functor};
use higher::{Lift, Lift3};
pub trait Apply<A, F, B>: Functor<A, B> + Lift3<A, F, B>
where
F: Fn(A) -> B,
{
fn apply(self, f: <Self as Lift3<A, F, B>>::Target2) -> <Self as Lift<A, B>>::Target1;
}
impl<A, F, B> Apply<A, F, B> for Option<A>
where
F: Fn(A) -> B,
{
fn apply(self, f: <Self as Lift3<A, F, B>>::Target2) -> <Self as Lift<A, B>>::Target1 {
self.and_then(|v| f.map(|f| f(v)))
}
}
impl<A, F, B, E> Apply<A, F, B> for Result<A, E>
where
F: Fn(A) -> B,
{
fn apply(self, f: <Self as Lift3<A, F, B>>::Target2) -> <Self as Lift<A, B>>::Target1 {
self.and_then(|v| f.map(|f| f(v)))
}
}
impl<A, F, B> Apply<A, F, B> for Vec<A>
where
A: Clone,
F: Fn(A) -> B + Clone,
{
fn apply(self, f: <Self as Lift3<A, F, B>>::Target2) -> <Self as Lift<A, B>>::Target1 {
self.ap(f)
}
}
impl<A, F, B> Apply<A, F, B> for VecDeque<A>
where
A: Clone,
F: Fn(A) -> B + Clone,
{
fn apply(self, f: <Self as Lift3<A, F, B>>::Target2) -> <Self as Lift<A, B>>::Target1 {
self.ap(f)
}
}
impl<A, F, B> Apply<A, F, B> for LinkedList<A>
where
A: Clone,
F: Fn(A) -> B + Clone,
{
fn apply(self, f: <Self as Lift3<A, F, B>>::Target2) -> <Self as Lift<A, B>>::Target1 {
self.ap(f)
}
}