CSharp · Console
namespace MaybePloeh
{
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
var nonEmptyTitle = "The Old Man and the Sea";
var nonEmptyAuthor = "Ernest Hemingway";
var bookController = new BookController();
Console.WriteLine(string.Format("empty title + empty author + GetBookFunctional = {0}", bookController.GetBookFunctional(string.Empty, string.Empty)));
Console.WriteLine(string.Format("empty title + empty author + GetBookImperative = {0}", bookController.GetBookImperative(string.Empty, string.Empty)));
Console.WriteLine(string.Format("empty title + empty author + GetBookImperativeSpecial = {0}", bookController.GetBookImperativeWithoutElses(string.Empty, string.Empty)));
Console.WriteLine(string.Format("empty title + non empty author + GetBookFunctional = {0}", bookController.GetBookFunctional(string.Empty, nonEmptyAuthor)));
Console.WriteLine(string.Format("empty title + non empty author + GetBookImperative = {0}", bookController.GetBookImperative(string.Empty, nonEmptyAuthor)));
Console.WriteLine(string.Format("empty title + non empty author + GetBookImperativeSpecial = {0}", bookController.GetBookImperativeWithoutElses(string.Empty, nonEmptyAuthor)));
Console.WriteLine(string.Format("non empty title + empty author + GetBookFunctional = {0}", bookController.GetBookFunctional(nonEmptyTitle, string.Empty)));
Console.WriteLine(string.Format("non empty title + empty author + GetBookImperative = {0}", bookController.GetBookImperative(nonEmptyTitle, string.Empty)));
Console.WriteLine(string.Format("non empty title + empty author + GetBookImperativeSpecial = {0}", bookController.GetBookImperativeWithoutElses(nonEmptyTitle, string.Empty)));
Console.WriteLine(string.Format("non empty title + non empty author + GetBookFunctional = {0}", bookController.GetBookFunctional(nonEmptyTitle, nonEmptyAuthor)));
Console.WriteLine(string.Format("non empty title + non empty author + GetBookImperative = {0}", bookController.GetBookImperative(nonEmptyTitle, nonEmptyAuthor)));
Console.WriteLine(string.Format("non empty title + non empty author + GetBookImperativeSpecial = {0}", bookController.GetBookImperativeWithoutElses(nonEmptyTitle, nonEmptyAuthor)));
}
}
internal class BookController : ApiController
{
/// <returns>
/// 300 BadRequest with a list of causes for failure
/// 200 Ok otherwise
/// </returns>
public IHttpActionResult GetBookFunctional(string title, string author)
{
var titleOption = NonEmptyString.Create(title);
var authorOption = NonEmptyString.Create(author);
var result = titleOption.Match(
t => authorOption.Match<IHttpActionResult>(
a => new Ok(BookService.GetBook(t, a)),
() => new BadRequest("Invalid author")),
() => authorOption.Match(
a => new BadRequest("Invalid title"),
() => new BadRequest(string.Format("Invalid title{0}Invalid author", Environment.NewLine))));
return result;
}
/// <returns>
/// 300 BadRequest with a list of causes for failure
/// 200 Ok otherwise
/// </returns>
public IHttpActionResult GetBookImperative(string title, string author)
{
var titleOption = NonEmptyString.Create(title);
var authorOption = NonEmptyString.Create(author);
IHttpActionResult result;
if (titleOption.IsSome)
if (authorOption.IsSome)
result = new Ok(BookService.GetBook(titleOption.SingleOrDefault(), authorOption.SingleOrDefault()));
else
result = new BadRequest("Invalid author");
else
if (authorOption.IsSome)
result = new BadRequest("Invalid title");
else
result = new BadRequest(string.Format("Invalid title{0}Invalid author", Environment.NewLine));
return result;
}
/// <summary>
/// Due to the fact the Option/Maybe Match function is binary, i.e. you must have a return value
/// for each case, this approach below (statement based) cannot be reproduced functionally (expression based).
/// </summary>
/// <returns>
/// 300 BadRequest with a list of causes for failure
/// 200 Ok otherwise
/// </returns>
public IHttpActionResult GetBookImperativeWithoutElses(string title, string author)
{
var titleOption = NonEmptyString.Create(title);
var authorOption = NonEmptyString.Create(author);
var reasons = new List<string>();
if (titleOption.IsNone)
reasons.Add("Invalid title");
if (authorOption.IsNone)
reasons.Add("Invalid author");
IHttpActionResult result;
if (reasons.Any())
result = new BadRequest(string.Join(Environment.NewLine, reasons));
else
result = new Ok(new Book());
return result;
}
}
// ******************************************************************************************************************************
// Below this is stuff that I faked to resemble the .NET WebApi, NonEmptyString and Option since importing their nuget into
// dotnetfiddle doesn't seem to work. Not very relevant to the discussion about Maybe<Semigroup>
// ******************************************************************************************************************************
internal class NonEmptyString
{
private string input;
private NonEmptyString(string input)
{
this.input = input;
}
internal static Option<NonEmptyString> Create(string input)
{
if (string.IsNullOrWhiteSpace(input))
{
return new Option<NonEmptyString>();
}
else
{
return new Option<NonEmptyString>(new NonEmptyString(input));
}
}
}
internal class Option<T>
{
private T input;
public Option()
{
IsSome = false;
IsNone = true;
}
public Option(T input)
{
IsSome = true;
IsNone = false;
this.input = input;
}
public bool IsNone { get; internal set; }
public bool IsSome { get; internal set; }
internal TResult Match<TResult>(Func<T, TResult> p1, Func<TResult> p2)
{
if (IsSome)
return p1(input);
else
return p2();
}
internal T SingleOrDefault()
{
if (this.IsNone)
{
throw new Exception();
}
else return this.input;
}
}
internal class Ok : IHttpActionResult
{
private Book book;
public Ok(Book book)
{
this.book = book;
}
public static implicit operator string(Ok ok)
{
return "Some book";
}
}
internal class BadRequest : IHttpActionResult
{
private string v;
public BadRequest(string v)
{
this.v = v;
}
public static implicit operator string(BadRequest badRequest)
{
return badRequest.v;
}
}
internal class ApiController
{
}
public interface IHttpActionResult
{
}
internal class BookService
{
internal static Book GetBook(NonEmptyString title, NonEmptyString author)
{
return new Book();
}
}
internal class Book
{
}
}