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
use std::marker::PhantomData;

pub struct State<'state, S, A> {
    run: Box<Fn(S) -> (S, A) + 'state>,
    state_type: PhantomData<S>,
    content_type: PhantomData<A>,
}

impl<'state, S: 'state + Clone + Copy, A: 'state> State<'state, S, A> {
    pub fn new<F>(f: F) -> State<'state, S, A>
    where
        F: Fn(S) -> (S, A) + 'state,
    {
        State {
            run: Box::new(f),
            state_type: PhantomData,
            content_type: PhantomData,
        }
    }

    pub fn run(&self, s: S) -> (S, A) {
        (self.run)(s)
    }

    pub fn map<B: 'state, G>(self, f: G) -> State<'state, S, B>
    where
        G: Fn(A) -> B + 'state,
    {
        let h = move |s: S| {
            let (s1, a) = (self.run)(s);
            (s1, f(a))
        };
        State::new(h)
    }

    pub fn flat_map<B: 'state, G>(self, f: G) -> State<'state, S, B>
    where
        G: Fn(A) -> State<'state, S, B> + 'state,
    {
        let h = move |s: S| {
            let (s1, a) = (self.run)(s);
            (f(a).run)(s1)
        };
        State::new(h)
    }

    pub fn get(self) -> State<'state, S, S> {
        let f = move |s: S| {
            let s2 = s.clone();
            (s, s2)
        };
        State::new(f)
    }

    pub fn gets<F>(self, f: F) -> State<'state, S, A>
    where
        F: Fn(S) -> A + 'state,
    {
        let g = move |s: S| {
            let s2 = s.clone();
            (s, f(s2))
        };
        State::new(g)
    }

    pub fn put(self, s: S) -> State<'state, S, ()> {
        let s2 = s.clone();
        let f = move |_| (s2, ());
        State::new(f)
    }

    pub fn modify<F>(self, f: F) -> State<'state, S, ()>
    where
        F: Fn(S) -> S + 'state,
    {
        let g = move |s| (f(s), ());
        State::new(g)
    }
}


#[cfg(test)]
mod tests {

    //use super::state::State;
    use super::State;

    #[derive(Debug, Clone, Copy)]
    struct Account {
        balance: i32,
    }

    fn deduct(d: i32) -> State<'static, Account, i32> {
        State::new(move |a: Account| (Account { balance: a.balance - d }, 0))
    }
    fn contribute(d: i32) -> State<'static, Account, i32> {
        State::new(move |a: Account| (Account { balance: a.balance + d }, 0))
    }
    // https://youtu.be/9uRXjxy7JDE?t=10m39s
    //

    #[test]
    fn it_works() {

        let account = Account { balance: 0 };

        let x = contribute(10).flat_map(move |_: i32| deduct(5));

        //let q = |x: i32| State::new(move |a: Account| (Account { balance: 111 + x }, 500));

        //let s = State::new(|a: Account| (Account { balance: 1 }, 5))
        //    .flat_map(q)
        //    .map(|a4| 100);
        let (account2, fee) = (x.run)(account);

        println!("____ Balance: {:?} Fee {:?}", account2.balance, fee);

        assert!(true);
    }

}