Created
April 11, 2012 20:27
-
-
Save kevingessner/2362249 to your computer and use it in GitHub Desktop.
Func
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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