-
Notifications
You must be signed in to change notification settings - Fork 2
/
monad.rs
66 lines (58 loc) · 2.06 KB
/
monad.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
pub fn run() {
let my_monad = MyMonad::of("hello monad");
println!("Try:{}", my_monad.get().to_string());
let monad_program = MyMonad::of("hello monad".to_string())
.map(|v| v.to_uppercase())
.flat_map(|v| MyMonad { value: v + &"!!!" });
println!("Program:{}", monad_program.get().to_string());
let number_monad_program = MyMonad::of(1981)
.map(|v| v + 1000)
.flat_map(|v| MyMonad { value: v / 10 });
println!("Numeric program:{}", number_monad_program.get().to_string());
let human_monad_program = MyMonad::of(Human { name: "Politrons".to_string(), age: 42 })
.map(|h| Human { name: h.name, age: 30 })
.flat_map(|h| MyMonad { value: Human { name: h.name + &"!!!", age: h.age + 10 } });
println!("{:#?}", human_monad_program.get());
}
//Implementation
//---------------
/**Trait interface like in Scala, where we define functions to implement*/
trait Monad<T> {
fn of(v: T) -> MyMonad<T>;
fn get(self) -> T;
fn map(self, func: fn(T) -> T) -> MyMonad<T>;
fn flat_map(self, func: fn(T) -> MyMonad<T>) -> MyMonad<T>;
}
/**Type to be used as implementation type for [Monad] trait*/
struct MyMonad<T> {
value: T,
}
/**
Same syntax like in goLang where we define [impl] of the trait type, and then we use
[for] operator to specify over which type class we implement the trait,
*/
impl<T> Monad<T> for MyMonad<T> {
//Constructor function to create the monad
fn of(v: T) -> MyMonad<T> {
MyMonad { value: v }
}
//Supplier function to extract the value from the monad
fn get(self) -> T {
self.value
}
//Transformation operator to get the value from the monad and transform using the function
fn map(self, func: fn(T) -> T) -> MyMonad<T> {
MyMonad { value: func(self.value) }
}
//Composition operator to compose two monad try
fn flat_map(self, func: fn(T) -> MyMonad<T>) -> MyMonad<T> {
func(self.value)
}
}
/**
Adding this tag, we allow this [struct] to be printed.*/
#[derive(Debug)]
struct Human {
name: String,
age: u64,
}