Skip to content

Instantly share code, notes, and snippets.

@caleb-vear
Created October 13, 2011 02:48
Show Gist options
  • Save caleb-vear/1283208 to your computer and use it in GitHub Desktop.
Save caleb-vear/1283208 to your computer and use it in GitHub Desktop.
Experiment with returning a normal value except under unusual conditions
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;
}
}
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