Created
March 9, 2015 15:00
-
-
Save loosechainsaw/234116b8f0812f139a71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class State<TS,TR> | |
{ | |
public State(Func<TS, Tuple<TS, TR>> f){ | |
Run = f; | |
} | |
public State<TS,TN> Map<TN>(Func<TR,TN> f){ | |
return new State<TS,TN>( x => { | |
var t = Run(x); | |
return Tuple.Create(t.Item1, f(t.Item2)); | |
}); | |
} | |
public State<TS,TN> FlatMap<TN>(Func<TR, State<TS,TN>> f){ | |
return new State<TS,TN>( x => { | |
var t = Run(x); | |
var u = f(t.Item2).Run(t.Item1); | |
return Tuple.Create(u.Item1, u.Item2); | |
}); | |
} | |
public readonly Func<TS,Tuple<TS,TR>> Run; | |
} | |
public static class State{ | |
public static State<S,V> ToState<S,V>(V value){ | |
return new State<S, V>(x => Tuple.Create(x,value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment