Created
October 13, 2011 02:48
-
-
Save caleb-vear/1283208 to your computer and use it in GitHub Desktop.
Experiment with returning a normal value except under unusual conditions
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
public static Alternate<T> Or<T>(this T normal, Func<T> provideAlternate) | |
{ | |
return new Alternate<T>(normal, provideAlternate); | |
} | |
public static Alternate<T> Or<T>(this T normal, T alternateValue) | |
{ | |
return new Alternate<T>(normal, () => alternateValue); | |
} | |
public class Alternate<T> | |
{ | |
readonly T _normal; | |
readonly Func<T> _provideAlternate; | |
public Alternate(T normal, Func<T> provideAlternate) | |
{ | |
_normal = normal; | |
_provideAlternate = provideAlternate; | |
} | |
public T When(bool alternateCondition) | |
{ | |
if (alternateCondition) | |
return _provideAlternate(); | |
return _normal; | |
} | |
} |
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
var endDate = localTime.AddDays(7 * 4); | |
endDate = endDate.Or(endDate.Previous(DayOfWeek.Sunday)).When(endDate.IsNotA(DayOfWeek.Sunday)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment