Do.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. namespace PaintDotNet
  3. {
  4. public static class Do
  5. {
  6. public static void Test<T>(
  7. T value,
  8. Function<bool, T> testFn,
  9. Procedure<T> ifTrueFn,
  10. Procedure<T> ifFalseFn)
  11. {
  12. (testFn(value) ? ifTrueFn : ifFalseFn)(value);
  13. }
  14. public static void GenerateTest<T>(
  15. Function<T> generate,
  16. Function<bool, T> test,
  17. Procedure<T> ifTrue,
  18. Procedure<T> ifFalse)
  19. {
  20. Test(generate(), test, ifTrue, ifFalse);
  21. }
  22. public static bool TryBool(Procedure actionProcedure)
  23. {
  24. try
  25. {
  26. actionProcedure();
  27. return true;
  28. }
  29. catch (Exception)
  30. {
  31. return false;
  32. }
  33. }
  34. public static T TryCatch<T>(
  35. Function<T> actionFunction,
  36. Function<T, Exception> catchClause)
  37. {
  38. T returnVal;
  39. try
  40. {
  41. returnVal = actionFunction();
  42. }
  43. catch (Exception ex)
  44. {
  45. returnVal = catchClause(ex);
  46. }
  47. return returnVal;
  48. }
  49. }
  50. }