Skip to content

Instantly share code, notes, and snippets.

@loosechainsaw
Created March 9, 2015 15:00
Show Gist options
  • Save loosechainsaw/234116b8f0812f139a71 to your computer and use it in GitHub Desktop.
Save loosechainsaw/234116b8f0812f139a71 to your computer and use it in GitHub Desktop.
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