Skip to content

Instantly share code, notes, and snippets.

@kevingessner
Created April 11, 2012 20:27
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 kevingessner/2362249 to your computer and use it in GitHub Desktop.
Save kevingessner/2362249 to your computer and use it in GitHub Desktop.
Func
using System;
using System.Linq;
namespace Kiln.Utils
{
public static class Func
{
public static Func<T, bool> Not<T>(Func<T, bool> predicate)
{
return o => !predicate(o);
}
public static Func<T, bool> Or<T>(params Func<T, bool>[] clauses)
{
return o => clauses.Any(c => c(o));
}
}
}
using Kiln.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Kiln.Tests
{
[TestClass]
public class FuncTest
{
[TestMethod]
public void TestNot()
{
Assert.IsTrue(Func.Not<string>(string.IsNullOrEmpty)("a"));
Assert.IsFalse(Func.Not<string>(string.IsNullOrEmpty)(""));
}
[TestMethod]
public void TestOr()
{
var f = Func.Or(string.IsNullOrEmpty, (string s) => s == "a");
Assert.IsTrue(f(null));
Assert.IsTrue(f(""));
Assert.IsTrue(f("a"));
Assert.IsFalse(f("b"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment