Skip to content

Instantly share code, notes, and snippets.

@ythirion
Created December 18, 2019 15:48
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 ythirion/fac16557b5254fecb407265c6359229b to your computer and use it in GitHub Desktop.
Save ythirion/fac16557b5254fecb407265c6359229b to your computer and use it in GitHub Desktop.
Try
public void file_monad_example()
{
GetLine()
.Bind(ReadFile)
.Bind(PrintStrln)
.Match(success => Console.WriteLine("SUCCESS"),
failure => Console.WriteLine("FAILURE"));
}
static Try<string> GetLine()
{
Console.Write("File:");
return Try(() => Console.ReadLine());
}
static Try<string> ReadFile(string filePath) => Try(() => File.ReadAllText(filePath));
static Try<bool> PrintStrln(string line)
{
Console.WriteLine(line);
return Try(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment