Created
July 30, 2014 19:27
-
-
Save sparkertime/3ba08c9739e288da3d76 to your computer and use it in GitHub Desktop.
When a Rubyist writes C#...
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; | |
public static class GlobalExtensions { | |
public static bool IsNull<T>(this T self) { | |
return self == null; | |
} | |
public static bool IsPresent<T>(this T self) { | |
return !self.IsNull(); | |
} | |
public static void Try<T>(this T self, Action<T> action) { | |
if(self.IsPresent()) { | |
action(self); | |
} | |
} | |
public static X Try<T,X>(this T self, Func<T,X> function) { | |
return self.Try(function, default(X)); | |
} | |
public static X Try<T,X>(this T self, Func<T,X> function, X defaultValue) { | |
if(self.IsPresent()) { | |
return function(self); | |
} | |
return defaultValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment