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
use std::rc::Rc;
use hkt::HKT;
pub trait Bind<A>: HKT<A> {
fn bind<F>(self, f: F) -> Self::T
where
F: Fn(&Self::C) -> Self::T;
}
impl<A, B> Bind<B> for Rc<A> {
fn bind<F>(self, f: F) -> Self::T
where
F: FnOnce(&Self::C) -> Self::T,
{
f(&self)
}
}
impl<A, B> Bind<B> for Box<A> {
fn bind<F>(self, f: F) -> Self::T
where
F: FnOnce(&Self::C) -> Self::T,
{
f(&self)
}
}
impl<A, B> Bind<B> for Option<A> {
fn bind<F>(self, f: F) -> Option<B>
where
F: FnOnce(&A) -> Option<B>,
{
match self {
Some(ref value) => f(value),
None => None,
}
}
}
impl<A, B, E: Clone> Bind<B> for Result<A, E> {
fn bind<F>(self, f: F) -> Result<B, E>
where
F: FnOnce(&Self::C) -> Result<B, E>,
{
match self {
Ok(v) => f(&v),
Err( e) => Err(e),
}
}
}
impl<A, B> Bind<B> for Vec<A> {
fn bind<F>(self, f: F) -> Vec<B>
where
F: Fn(&Self::C) -> Vec<B>,
{
self.iter().flat_map(f).collect()
}
}