Skip to content

Instantly share code, notes, and snippets.

@beeleebow
Last active March 20, 2019 21:37
Show Gist options
  • Save beeleebow/9a1093d625d02723196e02260d6388ac to your computer and use it in GitHub Desktop.
Save beeleebow/9a1093d625d02723196e02260d6388ac to your computer and use it in GitHub Desktop.
Lifting functions that deal with non-optional values into an Optional context.
using System;
using Blackbaud.Registration.Tests.Common.Extensions;
using LanguageExt.UnitTesting;
using Xunit;
using static LanguageExt.Prelude;
namespace Blackbaud.Registration.MessageHandlers.UnitTests.CommandHandlers
{
public class UsingApplyOnOption
{
private class NonFictionBook
{
public string Title { get; }
public string Author { get; }
public NonFictionBook(string title, string author)
{
Title = title;
Author = author;
}
}
private class FictionBook
{
public string BookTitle { get; }
public string AuthorName { get; }
public FictionBook(string bookTitle, string authorName)
{
BookTitle = bookTitle;
AuthorName = authorName;
}
}
[Fact]
public void LiftingNonOptionalFunctionsIntoOptionExample()
{
Func<NonFictionBook, FictionBook, bool> writtenBySameAuthor =
(nonFictionBook, fictionBook) => nonFictionBook.Author == fictionBook.AuthorName;
writtenBySameAuthor.Apply(
Some(new NonFictionBook("Cooking for Dummies", "Sarah Smith")),
Some(new FictionBook("1984", "George Orwell"))
).ShouldBeSome(Assert.False);
writtenBySameAuthor.Apply(
Some(new NonFictionBook("The Age of Mass Surveillance", "George Orwell")),
Some(new FictionBook("1984", "George Orwell"))
).ShouldBeSome(Assert.True);
writtenBySameAuthor.Apply(
Some(new NonFictionBook("The Age of Mass Surveillance", "George Orwell")),
None
).ShouldBeNone();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment