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
pub trait Pure<A> {
fn pure(value: A) -> Self;
}
impl<A> Pure<A> for Option<A> {
fn pure(value: A) -> Self {
Some(value)
}
}
impl<A, E> Pure<A> for Result<A, E> {
fn pure(value: A) -> Self {
Ok(value)
}
}
#[cfg(feature = "std")]
impl<A> Pure<A> for Vec<A> {
fn pure(value: A) -> Self {
vec![value]
}
}
#[cfg(test)]
mod test {
use crate::{Functor, Pure};
#[test]
fn pure_vec() {
let a = Vec::pure(31337);
assert_eq!(a, vec![31337]);
let b = a.fmap(|x| x.to_string());
assert_eq!(b, vec!["31337".to_string()]);
}
}