Skip to content

Instantly share code, notes, and snippets.

@ascjones
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ascjones/7e1fa0f654f7aa1c0438 to your computer and use it in GitHub Desktop.
Save ascjones/7e1fa0f654f7aa1c0438 to your computer and use it in GitHub Desktop.
Yes Or No
class YesOrNo<TState>
{
private readonly TState state;
private readonly bool keepGoing;
public bool Success { get { return keepGoing; } }
public YesOrNo(TState state = default (TState), bool keepGoing = true)
{
this.state = state;
this.keepGoing = keepGoing;
}
public YesOrNo<TState> Question(string question, Func<TState, TState> onYes, Action<TState> onNo)
{
if (keepGoing)
{
Console.Write(question + " ");
var key = Console.ReadKey();
if (key.KeyChar.ToString().ToLower() == "y")
{
Console.WriteLine();
var newState = onYes(state);
return new YesOrNo<TState>(newState);
}
Console.WriteLine();
onNo(state);
return new YesOrNo<TState>(state, false);
}
return new YesOrNo<TState>(state, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment