Last active
August 29, 2015 14:00
-
-
Save hidori/e49e67473c969214583d to your computer and use it in GitHub Desktop.
Ruby の unlessぽいヤツ
This file contains 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
void Main() | |
{ | |
var x = 1; | |
Unless(x == 0, () => Console.WriteLine("A")); | |
Unless(x == 0, () => Console.WriteLine("A"), () => Console.WriteLine("B")); | |
Console.WriteLine(Unless(x == 0, () => "A")); | |
Console.WriteLine(Unless(x == 0, () => "A", () => "B")); | |
} | |
public static void Unless(bool condition, Action then) | |
{ | |
if (!condition) | |
{ | |
then(); | |
} | |
} | |
public static void Unless(bool condition, Action then, Action @else) | |
{ | |
if (!condition) | |
{ | |
then(); | |
} | |
else | |
{ | |
@else(); | |
} | |
} | |
public static T Unless<T>(bool condition, Func<T> then) | |
{ | |
return (!condition) | |
? then() | |
: default(T); | |
} | |
public static T Unless<T>(bool condition, Func<T> then, Func<T> @else) | |
{ | |
return (!condition) | |
? then() | |
: @else(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment